Attempt to implement backend for voting

This commit is contained in:
Koen Bolhuis 2018-10-10 16:27:04 +02:00
parent 83c2390f72
commit 3bb94ccccf
4 changed files with 40 additions and 12 deletions

View File

@ -10,6 +10,8 @@ urlpatterns = [
# /api/postings/reviews/1/ Returns data for one Review. # /api/postings/reviews/1/ Returns data for one Review.
path('reviews/<int:pk>', ReviewView.as_view(), name='review'), path('reviews/<int:pk>', ReviewView.as_view(), name='review'),
path('reviews/<int:review_id>/helpful_vote/', review_helpful_vote, name='review_helpful_vote'),
# /api/postings/universities/ Lists all university objects. # /api/postings/universities/ Lists all university objects.
path('universities/', UniversitiesView.as_view(), name='universities'), path('universities/', UniversitiesView.as_view(), name='universities'),
# /api/postings/universities/1/ Returns data for one University. # /api/postings/universities/1/ Returns data for one University.

View File

@ -1,5 +1,5 @@
from rest_framework import generics, mixins from rest_framework import generics, mixins
from postings.models import UniversityReview, Review from postings.models import UniversityReview, Review, ReviewHelpfulVote
from .serializers import * from .serializers import *
from django.db.models import Q from django.db.models import Q
@ -65,4 +65,23 @@ class ProfessorsView(generics.ListAPIView):
# The view for an individual Professor. # The view for an individual Professor.
class ProfessorView(generics.RetrieveUpdateDestroyAPIView): class ProfessorView(generics.RetrieveUpdateDestroyAPIView):
queryset = Professor.objects.all() queryset = Professor.objects.all()
serializer_class = ProfessorSerializer 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")

View File

@ -15,13 +15,14 @@
<div class="review-block-title">{{ review.title }}</div> <div class="review-block-title">{{ review.title }}</div>
<div class="review-block-description">{{ review.content }}</div> <div class="review-block-description">{{ review.content }}</div>
<div> <div id="review-votes-{{ review.pk }}">
<p>Was this review helpful? <div class="review-vote-buttons">
Was this review helpful?
<a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm vote-up"><span class="glyphicon glyphicon-thumbs-up"></span> Yes</a> <a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm vote-up"><span class="glyphicon glyphicon-thumbs-up"></span> Yes</a>
<a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm vote-down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a> <a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm vote-down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a>
</p> </div>
<span title="{{ review.getHelpfulVoteCount }} people found this review helpful">{{ review.getHelpfulVoteCount }} <span class="glyphicon glyphicon-thumbs-up"></span></span> / <span title="{{ review.getHelpfulVoteCount }} people found this review helpful"><span class="votes-helpful">{{ review.getHelpfulVoteCount }}</span> <span class="glyphicon glyphicon-thumbs-up"></span></span> /
<span title="{{ review.getUnhelpfulVoteCount }} people found this review unhelpful">{{ review.getUnhelpfulVoteCount }} <span class="glyphicon glyphicon-thumbs-down"></span></span> <span title="{{ review.getUnhelpfulVoteCount }} people found this review unhelpful"><span class="votes-unhelpful">{{ review.getUnhelpfulVoteCount }}</span> <span class="glyphicon glyphicon-thumbs-down"></span></span>
</div> </div>
</div> </div>
</div> </div>

View File

@ -9,20 +9,26 @@ $(function() {
}; };
// Vote up // Vote up
$.post( $.post(
'/api/postings/reviews/' + reviewId + '/helpful_vote', '/api/postings/reviews/' + reviewId + '/helpful_vote/',
data data,
function(result) { console.log(result); }
); );
// Hide vote buttons
$("#review-votes-" + reviewId + " .review-vote-buttons").hide();
}); });
$(".vote-down").click(function() { $(".vote-down").click(function() {
var review = $(this).attr("data-review-id"); var reviewId = $(this).attr("data-review-id");
var data = { var data = {
'csrfmiddlewaretoken': csrftoken, 'csrfmiddlewaretoken': csrftoken,
'helpful': false 'helpful': false
}; };
// Vote down // Vote down
$.post( $.post(
'/api/postings/reviews/' + reviewId + '/helpful_vote', '/api/postings/reviews/' + reviewId + '/helpful_vote/',
data data,
function(result) { console.log(result); }
); );
// Hide vote buttons
$("#review-votes-" + reviewId + " .review-vote-buttons").hide();
}); });
}); });