コード例 #1
0
ファイル: views.py プロジェクト: krdeepak/djmysite
def api_poll_create(request):
    api_result = {"api": "poll_create", "status": "success"}
    try:
        token = request.POST["token"]
        user = get_user_from_token(token)
        if not user:
            api_result["status"] = "failure"
            api_result["error"] = "user not found"
        else:
            question = request.POST["question"]
            choice_text = request.POST["choices"]
            group_id = request.POST["group_id"]
            if group_id == "0":
                group = None
            else:
                group = Group.objects.get(pk=group_id)

            choices = set(choice_text.split("##"))
            if "" in choices:
                choices.remove("")

            try:
                topic = Topic.objects.get(name=request.POST["topic"])
            except:
                topic = Topic.objects.get(name="others")

            if len(question) == 0 or len(choices) < 2:
                raise ValueError("invalid poll arguments")

            q = Question()
            q.question_text = question.strip()
            q.user = user
            q.topic_id = topic.id
            q.group = group
            # q.pub_date = datetime.now(pytz.timezone("Asia/Calcutta"))

            q.save()

            for choice in choices:
                c = Choice()
                c.choice_text = choice
                c.question = q
                c.votes = 0
                c.save()
                print c.id

            print q.id
            api_result["question_id"] = q.id

    except Exception as e:
        api_result["status"] = "failure"
        api_result["error"] = e.message

    return JsonResponse(api_result)
コード例 #2
0
ファイル: views.py プロジェクト: krdeepak/djmysite
def create(request):
    topics = Topic.objects.all()
    context = {}
    context["topics"] = topics
    if request.method == "POST":
        print "inside"
        print request.POST
        try:
            question = request.POST["question"]
            print "question", question
            choices = set(request.POST["choices"].split("##"))
            if "" in choices:
                choices.remove("")
            print "choices", choices
            topic_id = request.POST["topic_id"]
            print "topic_id", topic_id
            if len(question) == 0 or len(choices) < 2:
                raise ValueError("invalid poll arguments")

            q = Question()
            q.question_text = question.strip()
            q.user = request.user
            q.topic_id = topic_id
            # q.pub_date = datetime.now(pytz.timezone("Asia/Calcutta"))

            q.save()

            for choice in choices:
                c = Choice()
                c.choice_text = choice
                c.question = q
                c.votes = 0
                c.save()
                print c.id

            print q.id
            data = {}
            data["question_id"] = q.id
            return JsonResponse(data)

        except Exception as e:
            print e.errno
            print e.strerror

            context["error"] = "Please enter valid question and at least two choices"
            return render(request, "polls/v1_create.html", context)

    else:
        return render(request, "polls/v1_create.html", context)