Exemple #1
0
def api_poll_vote(request):
    api_result = {"api": "poll_vote", "status": "success"}
    try:
        token = request.GET["token"]
        user = get_user_from_token(token)
        if not user:
            api_result["status"] = "failure"
            api_result["error"] = "user not found"
        else:
            question_id = request.GET["id"]
            choice_id = request.GET["choice"]
            question = Question.objects.get(pk=question_id)
            choice = Choice.objects.get(pk=choice_id)

            # user vote
            Vote.give_vote(user, question, choice)

            api_result["mychoice"] = choice.choice_text
            api_result["choices"] = [
                c.choice_text[:10] + ".." if len(c.choice_text) > 10 else c.choice_text
                for c in question.choice_set.all()
            ]
            api_result["votes"] = [c.votes for c in question.choice_set.all()]

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

    return JsonResponse(api_result)
Exemple #2
0
def _vote(request):
    question_id = request.GET["question_id"]
    choice_id = request.GET["choice_id"]

    print question_id, choice_id

    question = Question.objects.get(pk=question_id)
    choice = Choice.objects.get(pk=choice_id)

    Vote.give_vote(request.user, question, choice)

    context = {"question": question}

    data = {}
    data["mychoice"] = choice.choice_text
    data["choices"] = [
        c.choice_text[:10] + ".." if len(c.choice_text) > 10 else c.choice_text for c in question.choice_set.all()
    ]
    data["votes"] = [c.votes for c in question.choice_set.all()]

    # return  render(request, 'polls/v1_poll_summary_small.html', context)
    return JsonResponse(data)