diff options
Diffstat (limited to 'website')
-rw-r--r-- | website/models.py | 4 | ||||
-rw-r--r-- | website/urls.py | 2 | ||||
-rw-r--r-- | website/views.py | 55 |
3 files changed, 55 insertions, 6 deletions
diff --git a/website/models.py b/website/models.py index b0a9fc3..b15f1cd 100644 --- a/website/models.py +++ b/website/models.py @@ -54,8 +54,8 @@ class Answer(models.Model): body = models.TextField() date_created = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) - #userUpVotes = models.ManyToManyField(User, blank=True, related_name='postUpVotes') - #userDownVotes = models.ManyToManyField(User, blank=True, related_name='postDownVotes') + userUpVotes = models.ManyToManyField(User, blank=True, related_name='postAnswerUpVotes') + userDownVotes = models.ManyToManyField(User, blank=True, related_name='postAnswerDownVotes') upvotes = models.IntegerField(default=0) num_votes = models.IntegerField(default=0) diff --git a/website/urls.py b/website/urls.py index 71588af..72b4751 100644 --- a/website/urls.py +++ b/website/urls.py @@ -21,6 +21,8 @@ urlpatterns = patterns('', url(r'^search/$', 'website.views.search', name='search'), url(r'^unanswered-notification/$', 'website.views.unanswered_notification', name='unanswered_notification'), url(r'^vote_post/$', 'website.views.vote_post', name='vote_post'), + url(r'^ans_vote_post/$', 'website.views.ans_vote_post', name='ans_vote_post'), + # Ajax helpers url(r'^ajax-tutorials/$', 'website.views.ajax_tutorials', name='ajax_tutorials'), url(r'^ajax-duration/$', 'website.views.ajax_duration', name='ajax_duration'), diff --git a/website/views.py b/website/views.py index 5974433..6e84cfa 100644 --- a/website/views.py +++ b/website/views.py @@ -60,9 +60,8 @@ def get_question(request, question_id=None, pretty_url=None): form = AnswerQuestionForm() thisuserupvote = question.userUpVotes.filter(id=request.user.id).count() thisuserdownvote = question.userDownVotes.filter(id=request.user.id).count() - net_count = question.userUpVotes.count() - question.userDownVotes.count() - + context = { 'question': question, 'answers': answers, @@ -307,12 +306,12 @@ def new_question(request): def vote_post(request): - print "post" + post_id = int(request.POST.get('id')) vote_type = request.POST.get('type') vote_action = request.POST.get('action') - print post_id + cur_post = get_object_or_404(Question, id=post_id) thisuserupvote = cur_post.userUpVotes.filter(id=request.user.id).count() @@ -353,6 +352,54 @@ def vote_post(request): return HttpResponse(num_votes) +def ans_vote_post(request): + + + post_id = int(request.POST.get('id')) + + vote_type = request.POST.get('type') + vote_action = request.POST.get('action') + + cur_post = get_object_or_404(Answer, id=post_id) + + userupvote = cur_post.userUpVotes.filter(id=request.user.id).count() + userdownvote = 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) + + #This loop is for voting + if vote_action == 'vote': + if (userupvote == 0) and (userdownvote == 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 (userupvote == 1): + cur_post.userUpVotes.remove(request.user) + elif (vote_type == 'down') and (userdownvote == 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) + # Notification Section @login_required def user_questions(request, user_id): |