Example #1
0
def vote(request):
    if request.method == "POST":
        vote = Vote()
        vote.img_a = Image.objects.get(pk=request.POST["img_a"])
        vote.img_b = Image.objects.get(pk=request.POST["img_b"])
        vote.user = request.user
        vote.winner = Image.objects.get(pk=request.POST["winner"])
        vote.save()

    img_a, img_b = contenders(request.user)

    return render(request, 'vote/vote.html', {"img_a": img_a, "img_b": img_b})
Example #2
0
def voteProposition(request):
    resp = {}
    user = VoteUser.objects.filter(userkey=request.GET.get('userkey'))[0]
    if (user is None):
        resp['code'] = 'authentication error'
        return HttpResponse(json.dumps(resp), content_type='application/json')

    proposition = VoteProposition.objects.filter(
        id=request.GET.get('propositionid'))[0]
    votes = Vote.objects.filter(user=user, proposition=proposition)
    if (votes.count() == 0):
        v = Vote()
    else:
        v = votes[0]
        if (v.voteType == 1):
            proposition.strongDisagreeNb -= 1
        if (v.voteType == 2):
            proposition.disagreeNb -= 1
        if (v.voteType == 3):
            proposition.agreeNb -= 1
        if (v.voteType == 4):
            proposition.strongAgreeNb -= 1

    v.proposition = proposition
    v.user = user
    #1-pro, 2-against
    v.voteType = int(request.GET.get('type'))
    if (v.voteType == 1):
        proposition.strongDisagreeNb += 1
    if (v.voteType == 2):
        proposition.disagreeNb += 1
    if (v.voteType == 3):
        proposition.agreeNb += 1
    if (v.voteType == 4):
        proposition.strongAgreeNb += 1
    proposition.save()
    v.save()
    resp['id'] = v.id
    resp['strongDisagreeNb'] = proposition.strongDisagreeNb
    resp['disagreeNb'] = proposition.disagreeNb
    resp['agreeNb'] = proposition.agreeNb
    resp['strongAgreeNb'] = proposition.strongAgreeNb
    return HttpResponse(json.dumps(resp), content_type='application/json')