RateMyCourse/backend/postings/api/views.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-09-16 16:52:23 +00:00
from rest_framework import generics, mixins
2018-10-11 09:18:48 +00:00
from postings.models import *
from .serializers import *
2018-09-16 16:52:23 +00:00
from django.db.models import Q
# The view for listing all generic Review objects.
class ReviewsView(mixins.CreateModelMixin, generics.ListAPIView):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
# View for an individual Review object.
class ReviewView(generics.RetrieveUpdateDestroyAPIView):
queryset = Review.objects.all()
serializer_class = ReviewSerializer
2018-10-11 09:18:48 +00:00
class ReviewHelpfulVote(mixins.CreateModelMixin):
lookup_field = 'pk'
serializer_class = ReviewHelpfulVoteSerializer
2018-10-11 09:18:48 +00:00
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')
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")