Al/vote api #13
Binary file not shown.
|
@ -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")
|
|
@ -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',
|
||||
),
|
||||
]
|
|
@ -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):
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -19,14 +19,14 @@
|
|||
<div class="review-block-title">{{ review.title }}</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">
|
||||
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 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_up"><span class="glyphicon glyphicon-thumbs-up"></span> Yes</a>
|
||||
<a class="btn btn-light btn-sm js_vote_down"><span class="glyphicon glyphicon-thumbs-down"></span> No</a>
|
||||
</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>
|
||||
<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.unhelpful_vote_count }}</span> <span class="glyphicon glyphicon-thumbs-down"></span></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in New Issue