From 3bb94ccccf3740c95f7a246c7b8164c369701c08 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Wed, 10 Oct 2018 16:27:04 +0200 Subject: [PATCH] Attempt to implement backend for voting --- backend/postings/api/urls.py | 2 ++ backend/postings/api/views.py | 23 +++++++++++++++++-- .../templates/postings/frontend/review.html | 11 +++++---- frontpage/rateables/js/voting.js | 16 +++++++++---- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/backend/postings/api/urls.py b/backend/postings/api/urls.py index 1b6e317..6939e5f 100644 --- a/backend/postings/api/urls.py +++ b/backend/postings/api/urls.py @@ -10,6 +10,8 @@ urlpatterns = [ # /api/postings/reviews/1/ Returns data for one Review. path('reviews/', ReviewView.as_view(), name='review'), + path('reviews//helpful_vote/', review_helpful_vote, name='review_helpful_vote'), + # /api/postings/universities/ Lists all university objects. path('universities/', UniversitiesView.as_view(), name='universities'), # /api/postings/universities/1/ Returns data for one University. diff --git a/backend/postings/api/views.py b/backend/postings/api/views.py index 70cf3d6..fa706e1 100644 --- a/backend/postings/api/views.py +++ b/backend/postings/api/views.py @@ -1,5 +1,5 @@ from rest_framework import generics, mixins -from postings.models import UniversityReview, Review +from postings.models import UniversityReview, Review, ReviewHelpfulVote from .serializers import * from django.db.models import Q @@ -65,4 +65,23 @@ class ProfessorsView(generics.ListAPIView): # The view for an individual Professor. class ProfessorView(generics.RetrieveUpdateDestroyAPIView): queryset = Professor.objects.all() - serializer_class = ProfessorSerializer \ No newline at end of file + serializer_class = ProfessorSerializer + +def review_helpful_vote(request, review_id): + if request.method == 'POST': + helpful = request.POST.get('helpful') + if helpful is None: + return HttpResponseBadRequest("Bad Request") + helpful = True if helpful == 'true' else False + + try: + review = Review.objects.get(pk=review_id) + except Review.DoesNotExist: + raise HttpResponseBadRequest("Bad Request: Invalid review id.") + + vote = ReviewHelpfulVote.objects.create( + review=review, + helpful=helpful + ) + + return HttpResponseBadRequest("Bad Request") \ No newline at end of file diff --git a/backend/postings/templates/postings/frontend/review.html b/backend/postings/templates/postings/frontend/review.html index 2da4b29..267fd6f 100644 --- a/backend/postings/templates/postings/frontend/review.html +++ b/backend/postings/templates/postings/frontend/review.html @@ -15,13 +15,14 @@
{{ review.title }}
{{ review.content }}
-
-

Was this review helpful? +

+
+ Was this review helpful? Yes No -

- {{ review.getHelpfulVoteCount }} / - {{ review.getUnhelpfulVoteCount }} +
+ {{ review.getHelpfulVoteCount }} / + {{ review.getUnhelpfulVoteCount }}
\ No newline at end of file diff --git a/frontpage/rateables/js/voting.js b/frontpage/rateables/js/voting.js index 8b68f90..73cbd70 100644 --- a/frontpage/rateables/js/voting.js +++ b/frontpage/rateables/js/voting.js @@ -9,20 +9,26 @@ $(function() { }; // Vote up $.post( - '/api/postings/reviews/' + reviewId + '/helpful_vote', - data + '/api/postings/reviews/' + reviewId + '/helpful_vote/', + data, + function(result) { console.log(result); } ); + // Hide vote buttons + $("#review-votes-" + reviewId + " .review-vote-buttons").hide(); }); $(".vote-down").click(function() { - var review = $(this).attr("data-review-id"); + var reviewId = $(this).attr("data-review-id"); var data = { 'csrfmiddlewaretoken': csrftoken, 'helpful': false }; // Vote down $.post( - '/api/postings/reviews/' + reviewId + '/helpful_vote', - data + '/api/postings/reviews/' + reviewId + '/helpful_vote/', + data, + function(result) { console.log(result); } ); + // Hide vote buttons + $("#review-votes-" + reviewId + " .review-vote-buttons").hide(); }); }); \ No newline at end of file