From 57ad0f20e04f039cb032d13ba091ff5731e070ad Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 21:16:13 +0200 Subject: [PATCH 1/8] Return helpful/unhelpful votes from the Review model --- backend/postings/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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): From a59b88a5a6bc38a9593c922fc444d63638e8ba54 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 21:26:40 +0200 Subject: [PATCH 2/8] Show helpful/unhelpful votes in review --- backend/postings/templates/postings/frontend/review.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backend/postings/templates/postings/frontend/review.html b/backend/postings/templates/postings/frontend/review.html index 00d7edf..4907d93 100644 --- a/backend/postings/templates/postings/frontend/review.html +++ b/backend/postings/templates/postings/frontend/review.html @@ -11,10 +11,13 @@
- -
{{ review.title }}
{{ review.content }}
+ +
+ {{ review.getHelpfulVoteCount }} / + {{ review.getUnhelpfulVoteCount }} +
\ No newline at end of file From a119b9c85de7074b94fdfafcfdd51e54a11401fb Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 21:44:32 +0200 Subject: [PATCH 3/8] Add helpful/unhelpful buttons to reviews --- backend/postings/templates/postings/frontend/review.html | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/postings/templates/postings/frontend/review.html b/backend/postings/templates/postings/frontend/review.html index 4907d93..2da4b29 100644 --- a/backend/postings/templates/postings/frontend/review.html +++ b/backend/postings/templates/postings/frontend/review.html @@ -16,6 +16,10 @@
{{ review.content }}
+

Was this review helpful? + Yes + No +

{{ review.getHelpfulVoteCount }} / {{ review.getUnhelpfulVoteCount }}
From 7f481afdd8425b6771451b1fe3ac2b956adce6b4 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 21:45:26 +0200 Subject: [PATCH 4/8] Add voting javascript (doesn't do anything yet) --- .../postings/templates/postings/frontend/entity.html | 1 + frontpage/rateables/js/voting.js | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 frontpage/rateables/js/voting.js diff --git a/backend/postings/templates/postings/frontend/entity.html b/backend/postings/templates/postings/frontend/entity.html index 2545745..1db3136 100644 --- a/backend/postings/templates/postings/frontend/entity.html +++ b/backend/postings/templates/postings/frontend/entity.html @@ -193,5 +193,6 @@ + \ 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..f2a3c41 --- /dev/null +++ b/frontpage/rateables/js/voting.js @@ -0,0 +1,10 @@ +$(function() { + $(".vote-up").click(function() { + var review = $(this).attr("data-review-id"); + // Vote up + }); + $(".vote-down").click(function() { + var review = $(this).attr("data-review-id"); + // Vote down + }); +}); \ No newline at end of file From 45dc9e0fa851753b6b1280c09f41879eac823d40 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 21:57:39 +0200 Subject: [PATCH 5/8] Retrieve CSRF token in voting script --- frontpage/rateables/js/voting.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/frontpage/rateables/js/voting.js b/frontpage/rateables/js/voting.js index f2a3c41..fddc9bd 100644 --- a/frontpage/rateables/js/voting.js +++ b/frontpage/rateables/js/voting.js @@ -1,6 +1,23 @@ $(function() { + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + var csrftoken = getCookie('csrftoken'); + $(".vote-up").click(function() { - var review = $(this).attr("data-review-id"); + var reviewId = $(this).attr("data-review-id"); // Vote up }); $(".vote-down").click(function() { From b798898076e4d757426056859f5fb963d6947368 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 22:12:41 +0200 Subject: [PATCH 6/8] Implement easier way to retrieve CSRF token --- .../templates/postings/frontend/entity.html | 2 +- frontpage/rateables/js/voting.js | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/backend/postings/templates/postings/frontend/entity.html b/backend/postings/templates/postings/frontend/entity.html index 1db3136..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 %}
diff --git a/frontpage/rateables/js/voting.js b/frontpage/rateables/js/voting.js index fddc9bd..55247aa 100644 --- a/frontpage/rateables/js/voting.js +++ b/frontpage/rateables/js/voting.js @@ -1,20 +1,5 @@ $(function() { - function getCookie(name) { - var cookieValue = null; - if (document.cookie && document.cookie != '') { - var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { - var cookie = jQuery.trim(cookies[i]); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) == (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; - } - var csrftoken = getCookie('csrftoken'); + var csrftoken = $("#csrf-token input").val(); $(".vote-up").click(function() { var reviewId = $(this).attr("data-review-id"); From 83c2390f729a8e4fe250e70297f47d127f699b2a Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Tue, 9 Oct 2018 22:32:16 +0200 Subject: [PATCH 7/8] Send POST request to imaginary endpoint for helpful votes --- frontpage/rateables/js/voting.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/frontpage/rateables/js/voting.js b/frontpage/rateables/js/voting.js index 55247aa..8b68f90 100644 --- a/frontpage/rateables/js/voting.js +++ b/frontpage/rateables/js/voting.js @@ -3,10 +3,26 @@ $(function() { $(".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 + ); }); $(".vote-down").click(function() { var review = $(this).attr("data-review-id"); + var data = { + 'csrfmiddlewaretoken': csrftoken, + 'helpful': false + }; // Vote down + $.post( + '/api/postings/reviews/' + reviewId + '/helpful_vote', + data + ); }); }); \ No newline at end of file From 3bb94ccccf3740c95f7a246c7b8164c369701c08 Mon Sep 17 00:00:00 2001 From: Koen Bolhuis Date: Wed, 10 Oct 2018 16:27:04 +0200 Subject: [PATCH 8/8] 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