def patch(self, request, rating_pk): ratings = get_object_or_404(Ratings, pk=rating_pk) serializer = RatingSerializer(instance=ratings, data=request.data, partial=True) if serializer.is_valid(): serializer.save(rating_date=round(datetime.now().timestamp())) return Response(serializer.data, status=status.HTTP_202_ACCEPTED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def ratings(request): if request.method == 'GET': movieid = request.GET.get('movieid', None) ratings = Rating.objects.all() if movieid: ratings = ratings.filter(movieid=movieid) serializer = RatingSerializer(ratings, many=True) return Response(data=serializer.data, status=status.HTTP_200_OK) if request.method == 'DELETE': rating = Rating.objects.all() rating.delete() return Response(status=status.HTTP_200_OK) if request.method == 'POST': ratings = request.data.get('ratings', None) for item in ratings: userid = item.get('userid', None) movieid = item.get('movieid', None) rating = item.get('rating', None) timestamp = item.get('timestamp', None) if not (userid and movieid and rating and timestamp): continue Rating(userid_id=userid, movieid_id=movieid, rating=rating, timestamp=timestamp).save() return Response(status=status.HTTP_200_OK)\ if request.method == 'PUT': rating = Rating.objects.get(id=request.data.get('id')) serializer = RatingSerializer(rating, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def rating(request): if request.method == "POST": user = request.data['user'] movie = request.data['movie'] if Rating.objects.filter(user=user, movie=movie): return Response(status=status.HTTP_400_BAD_REQUEST) else: serializer = RatingSerializer(data=request.data) if serializer.is_valid(): rating = serializer.save(user=User.objects.get(id=user), movie=Movie.objects.get(id=movie), rating_date=round( datetime.now().timestamp())) return Response(RatingSerializer(rating).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if request.method == "PATCH": user = request.data.get('user') movie = request.data.get('movie') if Rating.objects.filter(user=user, movie=movie): rating = get_object_or_404(Rating, user=user, movie=movie) serializer = RatingSerializer(rating, data=request.data, partial=True) if serializer.is_valid(): rating = serializer.save( rating_date=round(datetime.now().timestamp())) return Response(RatingSerializer(rating).data) else: serializer = RatingSerializer(data=request.data) if serializer.is_valid(): rating = serializer.save(user=User.objects.get(id=user), movie=Movie.objects.get(id=movie), rating_date=round( datetime.now().timestamp())) return Response(RatingSerializer(rating).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer = RatingSerializer(request.data) if serializer.is_valid(): serializer.save(rating_date=round(datetime.now().timestamp())) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)