-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
62 lines (46 loc) · 1.6 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from movies.models import Movie, MovieReview
def index_view(request):
queryset = Movie.objects.all()
data = [movie.serialize_to_json() for movie in queryset]
return JsonResponse(data, safe=False)
def statistics_view(request):
queryset = Movie.objects.all()
data = []
for movie in queryset:
data.append({
'id': movie.id,
'title': movie.title,
'average_rating': movie.get_average_rating(),
})
return JsonResponse(data, safe=False)
@csrf_exempt
@require_http_methods(["POST"])
def review_view(request):
movie_id = request.POST.get('movie')
rating = request.POST.get('rating')
if (movie_id is None or rating is None) or (not movie_id.isdigit() or not rating.isdigit()):
return JsonResponse({
'detail': 'Please provide a `movie` (int) and `rating` (int).',
}, status=400)
movie_id = int(movie_id)
rating = int(rating)
if rating < 1 or rating > 5:
return JsonResponse({
'detail': 'Rating must be between 1 and 5.',
}, status=400)
try:
movie = Movie.objects.get(id=movie_id)
MovieReview.objects.create(
movie=movie,
rating=rating,
)
return JsonResponse({
'detail': 'A review has been successfully posted',
})
except Movie.DoesNotExist:
return JsonResponse({
'detail': 'Movie does not exist.',
}, status=400)