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_upcoming(request): kwargs = dict() args = ['max_results', 'language', 'page', 'region'] for arg in args: kwargs[arg] = request.query_params.get(arg, None) kwargs['max_results'] = get_max_results(**kwargs) data = ApiM.get_upcoming(**kwargs) return Response(data)
def get_latest_movie(request): kwargs = dict() args = ['language'] for arg in args: kwargs[arg] = request.query_params.get(arg, None) kwargs['max_results'] = get_max_results(**kwargs) data = ApiM.get_latest_movies(**kwargs) return Response(data)
def search_response_with_tmdb_id(request): """ This is different from get_movie_details. Parameters: query, tmdb_id """ kwargs = dict() args = ['query', 'tmdb_id'] for arg in args: kwargs[arg] = request.query_params.get(arg, None) data = ApiM.search_response_with_tmdb_id(**kwargs) return Response(data)
def search_movies(request): kwargs = dict() args = [ 'max_results', 'language', 'query', 'page', 'include_adult', 'region', 'year', 'primary_release_year' ] for arg in args: kwargs[arg] = request.query_params.get(arg, None) kwargs['max_results'] = get_max_results(**kwargs) data = ApiM.search_movies(**kwargs) return Response(data)
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)