From 8346482d47f98be745466087b0bd5dc954f46cf4 Mon Sep 17 00:00:00 2001 From: holyantony Date: Tue, 14 Jul 2015 16:45:10 +0530 Subject: Subject: Voting Validation(Questions) Description: 1. User cannot vote his/her own question --- forums/urls.py | 3 +- static/website/templates/search.html | 2 - website/models.py | 4 ++ website/views.py | 74 ++++++++++++++++++++---------------- 4 files changed, 48 insertions(+), 35 deletions(-) diff --git a/forums/urls.py b/forums/urls.py index 4584b66..13ea5be 100644 --- a/forums/urls.py +++ b/forums/urls.py @@ -23,5 +23,6 @@ urlpatterns = patterns('', url(r"^accounts/confirm/(?P\w+)/(?P[\w. @-]+)/$", 'forums.views.confirm', name='confirm'), url(r"^accounts/profile/(?P[\w. @-]+)/$", 'forums.views.account_profile', name='profile'), url(r"^accounts/view-profile/(?P[\w. @-]+)/$", 'forums.views.account_view_profile', name='view_profile'), - url(r'^search/', include('googlesearch.urls')), + + ) diff --git a/static/website/templates/search.html b/static/website/templates/search.html index c374b47..673274d 100644 --- a/static/website/templates/search.html +++ b/static/website/templates/search.html @@ -1,7 +1,5 @@ {% extends 'website/templates/base.html' %} -{% load i18n googlesearch_inclusion_tags %} {% load static %} - {% block content %} diff --git a/website/models.py b/website/models.py index b15f1cd..b195f3f 100644 --- a/website/models.py +++ b/website/models.py @@ -31,8 +31,12 @@ class Question(models.Model): userUpVotes = models.ManyToManyField(User, blank=True, related_name='postUpVotes') userDownVotes = models.ManyToManyField(User, blank=True, related_name='postDownVotes') num_votes = models.IntegerField(default=0) + + def __unicode__(self): return '%s' % (self.user) + + class Meta: get_latest_by = "date_created" diff --git a/website/views.py b/website/views.py index b86fdbf..8fd6b11 100644 --- a/website/views.py +++ b/website/views.py @@ -321,46 +321,56 @@ def vote_post(request): vote_type = request.POST.get('type') vote_action = request.POST.get('action') + question_id = request.POST.get('id') + print question_id, "question_id" + + question = get_object_or_404(Question, id=question_id) cur_post = get_object_or_404(Question, id=post_id) - thisuserupvote = cur_post.userUpVotes.filter(id=request.user.id).count() thisuserdownvote = cur_post.userDownVotes.filter(id=request.user.id).count() - initial_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() - # print "User Initial Upvote and Downvote: %d %d %s " % (thisuserupvote, thisuserdownvote, vote_action) + #print "IDDD" + print "logged in user_id:", request.user.id,"Person who asked(user_id)", question.user_id - #This loop is for voting - if vote_action == 'vote': - if (thisuserupvote == 0) and (thisuserdownvote == 0): - if vote_type == 'up': - cur_post.userUpVotes.add(request.user) - elif vote_type == 'down': - cur_post.userDownVotes.add(request.user) - else: - return HttpResponse("Error: Unknown vote-type passed.") - else: - return HttpResponse(initial_votes) - #This loop is for canceling vote - elif vote_action == 'recall-vote': - if (vote_type == 'up') and (thisuserupvote == 1): - cur_post.userUpVotes.remove(request.user) - elif (vote_type == 'down') and (thisuserdownvote == 1): - cur_post.userDownVotes.remove(request.user) - else: - # "Error - Unknown vote type or no vote to recall" - return HttpResponse(initial_votes) + if request.user.id != question.user_id: + #if request.user.id == question_id: + print "in" + + if vote_action == 'vote': + if (thisuserupvote == 0) and (thisuserdownvote == 0): + if vote_type == 'up': + cur_post.userUpVotes.add(request.user) + elif vote_type == 'down': + cur_post.userDownVotes.add(request.user) + else: + return HttpResponse("Error: Unknown vote-type passed.") + else: + return HttpResponse(initial_votes) + #This loop is for canceling vote + elif vote_action == 'recall-vote': + if (vote_type == 'up') and (thisuserupvote == 1): + cur_post.userUpVotes.remove(request.user) + elif (vote_type == 'down') and (thisuserdownvote == 1): + cur_post.userDownVotes.remove(request.user) + else: + # "Error - Unknown vote type or no vote to recall" + return HttpResponse(initial_votes) + else: + return HttpResponse("Error: Bad Action.") + + num_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() + cur_post.num_votes = num_votes + cur_post.save() + + print "Num Votes: %s" % num_votes + + return HttpResponse(num_votes) + else: - return HttpResponse("Error: Bad Action.") - - num_votes = cur_post.userUpVotes.count() - cur_post.userDownVotes.count() - cur_post.num_votes = num_votes - cur_post.save() - - print "Num Votes: %s" % num_votes - - return HttpResponse(num_votes) + print "out" + return HttpResponse(initial_votes) def ans_vote_post(request): -- cgit