def post_rating(request): """ Parameters: rating, movie_id """ rating_val = request.query_params.get('rating', None) movie_id = request.query_params.get('movie_id', None) if rating_val == None or movie_id == None: return Response({'Error': 'A parameter is missing'}, status=status.HTTP_400_BAD_REQUEST) user = request.user try: movie = Movie.objects.get(tmdb_id=movie_id) except Movie.DoesNotExist: movie = Movie.objects.create(tmdb_id=movie_id) data = ApiM.get_movie_details(movie_id) movie.imdb_id = data['imdb_id'] movie.vote_average = data['vote_average'] movie.vote_count = data['vote_count'] movie.popularity = data['popularity'] movie.save() try: rating = Rating.objects.get(user=user, movie=movie) except Rating.DoesNotExist: rating = Rating.objects.create(user=user, movie=movie) rating.rating = rating_val rating.save() return Response(status=status.HTTP_201_CREATED)
def get_movie_details(request, movie_id): try: movie = Movie.objects.get(tmdb_id=movie_id) status_ = status.HTTP_200_OK except Movie.DoesNotExist: movie = Movie.objects.create(tmdb_id=movie_id) movie.save() status_ = status.HTTP_201_CREATED data = ApiM.get_movie_details(movie_id) return Response(data, status=status_)
def get_or_post_comment(request, movie_id): """ Parameters: tmdb_id, content(if POST) """ try: movie = Movie.objects.get(tmdb_id=movie_id) print(movie) except Movie.DoesNotExist: movie = Movie.objects.create(tmdb_id=movie_id) data = ApiM.get_movie_details(movie_id) movie.imdb_id = data['imdb_id'] movie.vote_average = data['vote_average'] movie.vote_count = data['vote_count'] movie.popularity = data['popularity'] movie.save() if request.method == 'POST': data = request.data comment = Comment.objects.create(content=data['content'], movie=movie, user=request.user) if comment: return Response({'success': 'successfully posted'}, status=status.HTTP_201_CREATED) else: return Response({'failure': 'failed to create comment'}, status=status.HTTP_400_BAD_REQUEST) else: """ Incoming args: movie id """ try: comments = Comment.objects.filter(movie=movie.pk) except Comment.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) serializer = CommentSerializer(comments, many=True) return Response(serializer.data)