Added voting.

This commit is contained in:
Andrew Lalis 2018-10-11 12:16:57 +02:00
parent a2b47ef068
commit e58a3be5e8
6 changed files with 54 additions and 18 deletions

Binary file not shown.

View File

@ -2,6 +2,7 @@ from rest_framework import generics, mixins
from postings.models import * from postings.models import *
from .serializers import * from .serializers import *
from django.db.models import Q from django.db.models import Q
from django.http import *
# The view for listing all generic Review objects. # The view for listing all generic Review objects.
@ -14,13 +15,6 @@ class ReviewView(generics.RetrieveUpdateDestroyAPIView):
queryset = Review.objects.all() queryset = Review.objects.all()
serializer_class = ReviewSerializer 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): def review_helpful_vote(request, review_id):
if request.method == 'POST': if request.method == 'POST':
helpful = request.POST.get('helpful') helpful = request.POST.get('helpful')
@ -38,4 +32,6 @@ def review_helpful_vote(request, review_id):
helpful=helpful helpful=helpful
) )
return HttpResponse(status=201)
return HttpResponseBadRequest("Bad Request") return HttpResponseBadRequest("Bad Request")

View File

@ -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',
),
]

View File

@ -62,12 +62,14 @@ class Review(models.Model):
author = models.ForeignKey('postings.User', on_delete=models.PROTECT, null=True, blank=True) 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'. # Gets the total number of votes which marked this review as 'helpful'.
def getHelpfulVoteCount(self): @property
return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=True).count() 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'. # Gets the total number of votes which marked this review as 'unhelpful'.
def getUnhelpfulVoteCount(self): @property
return ReviewHelpfulVote.objects.filter(pk=self.pk, helpful=False).count() 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. # A vote for a review as either positive or negative.
class ReviewHelpfulVote(models.Model): class ReviewHelpfulVote(models.Model):
@ -75,8 +77,6 @@ class ReviewHelpfulVote(models.Model):
review = models.ForeignKey('postings.Review', on_delete=models.CASCADE) review = models.ForeignKey('postings.Review', on_delete=models.CASCADE)
# Whether or not the referenced review was helpful. # Whether or not the referenced review was helpful.
helpful = models.BooleanField() helpful = models.BooleanField()
# The user who made this vote.
user = models.ForeignKey('postings.User', on_delete=models.CASCADE)
# A RateableEntity for universities. # A RateableEntity for universities.
class University(RateableEntity): class University(RateableEntity):

View File

@ -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 () { function registerEvents () {
$(document.body) $(document.body)
.off('click.js_vote_up') .off('click.js_vote_up')
@ -41,6 +62,7 @@ function registerEvents () {
function (event) { function (event) {
event.preventDefault(); event.preventDefault();
console.log("updoot"); console.log("updoot");
sendVote(event, true);
}); });
$(document.body) $(document.body)
@ -50,6 +72,7 @@ function registerEvents () {
function (event) { function (event) {
event.preventDefault(); event.preventDefault();
console.log("downdoot"); console.log("downdoot");
sendVote(event, false);
}); });
} }

View File

@ -19,14 +19,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 id="review-votes-{{ review.pk }}"> <div id="review-votes-{{ review.pk }}" class="js_votes" data-review_id="{{ review.pk }}">
<div class="review-vote-buttons"> <div class="review-vote-buttons">
Was this review helpful? Was this review helpful?
<a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm js_vote_up"><span class="glyphicon glyphicon-thumbs-up"></span> Yes</a> <a class="btn btn-light btn-sm js_vote_up"><span class="glyphicon glyphicon-thumbs-up"></span> Yes</a>
<a data-review-id="{{ review.pk }}" class="btn btn-light btn-sm js_vote_down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a> <a class="btn btn-light btn-sm js_vote_down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a>
</div> </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.getHelpfulVoteCount }} people found this review helpful"><span class="votes-helpful">{{ review.helpful_vote_count }}</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> <span title="{{ review.getUnhelpfulVoteCount }} people found this review unhelpful"><span class="votes-unhelpful">{{ review.unhelpful_vote_count }}</span> <span class="glyphicon glyphicon-thumbs-down"></span></span>
</div> </div>
</div> </div>
</div> </div>