shellfly / django-vote

Simple vote for django
http://django-vote.readthedocs.io
Apache License 2.0
158 stars 49 forks source link

Check if user voted up or down #51

Closed mihovilm closed 7 years ago

mihovilm commented 7 years ago

Hi! Is it possible to get the info if the user voted up or down?

Even if there isn't a method, is it possible to get all user.ids who voted up? So I can find it in here? I know how to get user_ids, but I've checked models.py and the manager, and I can't find how to get the 'action' the user pressed.

Cause basically I need a reddit/stackoverflow like upvote/downvote, and jQuery to add an 'active' class to the voting button the user already used.

Tnx for the app.

mihovilm commented 7 years ago

I've tried {% if r.vote_exists %} (r is my object)

but whenever I try to pass an argument it gives me the 'unused argument' error.

I've seen that the template tag takes model, user and action (UP/DOWN) ?

shellfly commented 7 years ago

https://github.com/shanbay/django-vote/blob/master/vote/managers.py#L154

user_ids method has the action parameter

mihovilm commented 7 years ago

Thanks!

Okay, so if anyone wants to know how I used it, here's my simple view of an object details

` def rdetail(request, q_id, r_id): reply = get_object_or_404(Reply, id=r_id) other = Reply.objects.filter(question_id=reply.question_id).exclude(id=reply.id)

if request.user.is_authenticated():
    user_votes_up = reply.votes.user_ids(0)
    user_votes_down = reply.votes.user_ids(1)
    check = {'user_id': request.user.id}

    if check in user_votes_up.values('user_id'):
        action = 1

    elif check in user_votes_down.values('user_id'):
        action = -1

    else:
        action = 0  # hasn't voted, but I don't want to query the DB for votes.exists again

else:
    action = 0  # anonymous user

context = {
    'r': reply,
    'other': other,
    'action': action
}
return render(request, 'feed/rdetail.html', context)`

And now in my template (I'm using bootstrap etc.) I have <span class="glyphicon glyphicon-chevron-up {% if action == 1 %}glyphicon-pressed{% endif %}"></span> <span class="score">{{ r.vote_score }}</span> <span class="glyphicon glyphicon-chevron-down {% if action == -1 %}glyphicon-pressed{% endif %}"></span>

wdifruscio commented 6 years ago

I did not see that action in user_ids either haha... thanks for this awesome package!