RateMyCourse/backend/postings/api/views.py

37 lines
1.0 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
2018-10-11 10:16:57 +00:00
from django.http import *
2018-09-16 16:52:23 +00:00
# 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
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
)
2018-10-11 10:16:57 +00:00
return HttpResponse(status=201)
return HttpResponseBadRequest("Bad Request")