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.
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.
path('universities/', UniversitiesView.as_view(), name='universities'),
# /api/postings/universities/1/ Returns data for one University.

View File

@ -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
@ -66,3 +66,22 @@ class ProfessorsView(generics.ListAPIView):
class ProfessorView(generics.RetrieveUpdateDestroyAPIView):
queryset = Professor.objects.all()
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-description">{{ review.content }}</div>
<div>
<p>Was this review helpful?
<div id="review-votes-{{ review.pk }}">
<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-down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a>
</p>
<span title="{{ review.getHelpfulVoteCount }} people found this review helpful">{{ review.getHelpfulVoteCount }} <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>
</div>
<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"><span class="votes-unhelpful">{{ review.getUnhelpfulVoteCount }}</span> <span class="glyphicon glyphicon-thumbs-down"></span></span>
</div>
</div>
</div>

View File

@ -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();
});
});