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/models.py b/backend/postings/models.py index d998984..d660248 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -63,11 +63,11 @@ class Review(models.Model): # Gets the total number of votes which marked this review as 'helpful'. def getHelpfulVoteCount(self): - ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=True).count() + return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=True).count() # Gets the total number of votes which marked this review as 'unhelpful'. def getUnhelpfulVoteCount(self): - ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=False).count() + return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=False).count() # A vote for a review as either positive or negative. class ReviewHelpfulVote(models.Model): diff --git a/backend/postings/templates/postings/frontend/entity.html b/backend/postings/templates/postings/frontend/entity.html index 2545745..7b8d11b 100644 --- a/backend/postings/templates/postings/frontend/entity.html +++ b/backend/postings/templates/postings/frontend/entity.html @@ -162,7 +162,7 @@ - {% csrf_token %} +
{% csrf_token %}
@@ -193,5 +193,6 @@ + \ 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 00d7edf..267fd6f 100644 --- a/backend/postings/templates/postings/frontend/review.html +++ b/backend/postings/templates/postings/frontend/review.html @@ -11,10 +11,18 @@
- -
{{ review.title }}
{{ review.content }}
+ +
+
+ Was this review helpful? + Yes + No +
+ {{ review.getHelpfulVoteCount }} / + {{ review.getUnhelpfulVoteCount }} +
\ No newline at end of file diff --git a/frontpage/rateables/js/voting.js b/frontpage/rateables/js/voting.js new file mode 100644 index 0000000..73cbd70 --- /dev/null +++ b/frontpage/rateables/js/voting.js @@ -0,0 +1,34 @@ +$(function() { + var csrftoken = $("#csrf-token input").val(); + + $(".vote-up").click(function() { + var reviewId = $(this).attr("data-review-id"); + var data = { + 'csrfmiddlewaretoken': csrftoken, + 'helpful': true + }; + // Vote up + $.post( + '/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 reviewId = $(this).attr("data-review-id"); + var data = { + 'csrfmiddlewaretoken': csrftoken, + 'helpful': false + }; + // Vote down + $.post( + '/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