def put(self, request, pk): movie = self.get_object(pk) serializer = MoviesSerializer(movie, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def __init__(self): with open(FILE) as json_file: data = json.load(json_file) self.movie_serializer = MoviesSerializer(data=data) if self.movie_serializer.is_valid(): self.movie_serializer.save() movie = Movie.objects.get(pk=1) if movie: self.comment = Comment.objects.create(movie_id=movie, body=BODY)
def api_list_movie_view_genres(request, genres): try: movie = MoviesModel.objects.filter(genres__contains=genres) except MoviesModel.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MoviesSerializer(movie, many=True) return Response(serializer.data)
def api_list_movie_view_id(request, id): try: movie = MoviesModel.objects.get(id=id) except MoviesModel.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MoviesSerializer(movie) return Response(serializer.data)
def api_list_movie_view_rating(request, rating): try: movie = MoviesModel.objects.filter(rating__gte=rating) except MoviesModel.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MoviesSerializer(movie, many=True) return Response(serializer.data)
def api_detail_movie_view(request, title): try: movie = MoviesModel.objects.filter(title__contains=title) except MoviesModel.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = MoviesSerializer(movie, many=True) return Response(serializer.data)
def test_get_a_movie(self): """Test movie with id exist""" #self.login_client("*****@*****.**", "test123") response = self.retrieve_movie(self.valid_movie_id) expected = Movies.objects.get(pk=self.valid_movie_id) serialized = MoviesSerializer(expected) self.assertEqual(response.data, serialized.data) self.assertEqual(response.status_code, status.HTTP_200_OK) response = self.retrieve_movie(self.invalid_movie_id) self.assertEqual(response.data["message"], "Movie with id: 50 does not exist") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_get_all_movies(self): """ This test ensures that all movies added in the setUp method exist when we make a GET request to the movies/ endpoint """ #self.login_client("*****@*****.**", "test123") response = self.client.get(reverse("movies-all")) expected = Movies.objects.all() serialized = MoviesSerializer(expected, many=True) self.assertEqual(response.data, serialized.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def post(self, request, *args, **kwargs): fresh_list = Movies.objects.all().order_by('-create_time') pager = DatePager() pager_list = pager.paginate_queryset(queryset=fresh_list, request=request, view=self) pager.get_previous_link() serializer = MoviesSerializer(instance=pager_list, many=True) return JsonResponse({ 'code': 200, 'message': serializer.data, 'safe': False })
def get(self, request, pk): movie = self.get_object(pk) serializer = MoviesSerializer(movie) return Response(serializer.data)
def post(self, request): serializer = MoviesSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): movies = Movies.objects.all() serializer = MoviesSerializer(movies, many=True) return Response(serializer.data)