diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index b64c046..91e430b 100644 Binary files a/backend/db.sqlite3 and b/backend/db.sqlite3 differ diff --git a/backend/postings/api/views.py b/backend/postings/api/views.py index e8c228b..c21d82f 100644 --- a/backend/postings/api/views.py +++ b/backend/postings/api/views.py @@ -2,6 +2,7 @@ from rest_framework import generics, mixins from postings.models import * from .serializers import * from django.db.models import Q +from django.http import * # The view for listing all generic Review objects. @@ -14,13 +15,6 @@ class ReviewView(generics.RetrieveUpdateDestroyAPIView): queryset = Review.objects.all() serializer_class = ReviewSerializer -class ReviewHelpfulVote(mixins.CreateModelMixin): - lookup_field = 'pk' - serializer_class = ReviewHelpfulVoteSerializer - - def post(self, request, *args, **kwargs): - return self.create(request, *args, **kwargs) - def review_helpful_vote(request, review_id): if request.method == 'POST': helpful = request.POST.get('helpful') @@ -38,4 +32,6 @@ def review_helpful_vote(request, review_id): helpful=helpful ) + return HttpResponse(status=201) + return HttpResponseBadRequest("Bad Request") \ No newline at end of file diff --git a/backend/postings/migrations/0004_remove_reviewhelpfulvote_user.py b/backend/postings/migrations/0004_remove_reviewhelpfulvote_user.py new file mode 100644 index 0000000..2f1223a --- /dev/null +++ b/backend/postings/migrations/0004_remove_reviewhelpfulvote_user.py @@ -0,0 +1,17 @@ +# Generated by Django 2.1.1 on 2018-10-11 09:27 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('postings', '0003_auto_20181002_1355'), + ] + + operations = [ + migrations.RemoveField( + model_name='reviewhelpfulvote', + name='user', + ), + ] diff --git a/backend/postings/models.py b/backend/postings/models.py index d660248..cf9fd89 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -62,12 +62,14 @@ class Review(models.Model): author = models.ForeignKey('postings.User', on_delete=models.PROTECT, null=True, blank=True) # Gets the total number of votes which marked this review as 'helpful'. - def getHelpfulVoteCount(self): - return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=True).count() + @property + def helpful_vote_count(self): + return ReviewHelpfulVote.objects.filter(review=self.pk, helpful=True).count() # Gets the total number of votes which marked this review as 'unhelpful'. - def getUnhelpfulVoteCount(self): - return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=False).count() + @property + def unhelpful_vote_count(self): + return ReviewHelpfulVote.objects.filter(review=self.pk, helpful=False).count() # A vote for a review as either positive or negative. class ReviewHelpfulVote(models.Model): @@ -75,8 +77,6 @@ class ReviewHelpfulVote(models.Model): review = models.ForeignKey('postings.Review', on_delete=models.CASCADE) # Whether or not the referenced review was helpful. helpful = models.BooleanField() - # The user who made this vote. - user = models.ForeignKey('postings.User', on_delete=models.CASCADE) # A RateableEntity for universities. class University(RateableEntity): diff --git a/backend/postings/static/postings/js/voting.js b/backend/postings/static/postings/js/voting.js index c65915b..26a6a6c 100644 --- a/backend/postings/static/postings/js/voting.js +++ b/backend/postings/static/postings/js/voting.js @@ -33,6 +33,27 @@ // }); // }); +// Sends either an up- or down-vote for a particular review. +function sendVote (event, is_helpful) { + var csrf_token = $('#csrf-token input').val(); + var review_id = $(event.target).closest('.js_votes').data('review_id'); + var data = { + 'csrfmiddlewaretoken': csrf_token, + 'helpful': is_helpful + }; + $.post( + '/api/postings/reviews/' + review_id + '/helpful_vote/', + data, + ) + .done(function (response) { + console.log(response); + }) + .fail(function (response) { + console.log(response); + }); +} + +// Registers all events to the document. function registerEvents () { $(document.body) .off('click.js_vote_up') @@ -41,6 +62,7 @@ function registerEvents () { function (event) { event.preventDefault(); console.log("updoot"); + sendVote(event, true); }); $(document.body) @@ -50,6 +72,7 @@ function registerEvents () { function (event) { event.preventDefault(); console.log("downdoot"); + sendVote(event, false); }); } diff --git a/backend/postings/templates/postings/frontend/review.html b/backend/postings/templates/postings/frontend/review.html index 41ab8bc..0cbb7d6 100644 --- a/backend/postings/templates/postings/frontend/review.html +++ b/backend/postings/templates/postings/frontend/review.html @@ -19,14 +19,14 @@