def create(self, request, *args, **kwargs): context = {'request': request} serializer = MovieSerializer(data=request.data, context=context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): """ :param request: request the movie details :return: save the details of a movie in database """ try: # import pdb # pdb.set_trace() data = request.data if 'actor' in data: actor_list = [] actors = data['actor'] for name in actors: obj = Actor.objects.get(actor_name=name) if obj: actor_list.append(obj.id) else: return Response( SMD_Response(message="Something went wrong when" "while adding actors")) data['actor'] = actor_list if 'producer' in data: producer_list = [] producers = data['producer'] for name in producers: obj = Producer.objects.get(producer_name=name) if obj: producer_list.append(obj.id) else: return Response( SMD_Response(message="Something went wrong when" "while adding producers")) data['producer'] = producer_list serializer = MovieSerializer(data=request.data, partial=True) if serializer.is_valid(): serializer.save() logger.info( "Successfully Save the movie details in the database") smd = SMD_Response( status=True, message= "Successfully Save the movie details in the database", data=[serializer.data]) return Response(smd, status=status.HTTP_201_CREATED) else: logger.error("Please Provide Valid Details") smd = SMD_Response(status=False, message="Please Provide Valid Details", data=[serializer.errors]) return Response(smd, status=status.HTTP_400_BAD_REQUEST) except Exception as e: logger.error("Something went wrong-" + str(e)) smd = SMD_Response(message="Something Went Wrong") return Response(smd, status=status.HTTP_400_BAD_REQUEST)
def update(self, request, *args, **kwargs): context = {'request': request} instance = self.get_object() serializer = MovieSerializer(instance, data=request.data, partial=False, context=context) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data)
def movie_list(request): if request.method == 'GET': movies = Movie.objects.all() serializer = MovieSerializer(movies, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = MovieSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def movie_detail(request, movie_id): print('ddddddd') movie = get_object_or_404(Movie, pk=movie_id) if request.method == 'GET': serializer = MovieSerializer(movie) print(serializer.data) return Response(serializer.data) elif request.method == 'PUT': serializer = MovieSerializer(movie, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response({'message': '수정 완료'}) return Response(serializer.error) else: movie.delete()
def put(self, request, id, *args, **kwargs): """ :param request:request for put(update) the Movie Details :param id:Here, we pass an ID for update of a requested Movie :return:It will Update a requested Movie Details in Database """ try: # import pdb # pdb.set_trace() movie_obj = Movie.objects.get(id=id) if movie_obj is not None: data = request.data if 'actor' in data: actor_list = [] actors = data['actor'] for name in actors: obj = Actor.objects.get(actor_name=name) if obj: actor_list.append(obj.id) else: return Response( SMD_Response( message="Something went wrong when" "while adding actors")) data['actor'] = actor_list if 'producer' in data: producer_list = [] producers = data['producer'] for name in producers: obj = Producer.objects.get(producer_name=name) if obj: producer_list.append(obj.id) else: return Response( SMD_Response( message="Something went wrong when" "while adding producers")) data['producer'] = producer_list serializer = MovieSerializer(movie_obj, data=data) if serializer.is_valid(): serializer.save() logger.info( "Successfully Updated the Details of the a Movie") smd = SMD_Response( status=True, message="Movie Details Successfully Updated", data=[serializer.data]) return Response(smd, status=status.HTTP_202_ACCEPTED) else: logger.error("Please provide valid details") smd = SMD_Response( message="Invalid Request/No such query exist", data=[serializer.errors]) return Response(smd, status=status.HTTP_400_BAD_REQUEST) else: logger.info("No DATA Present") return Response(SMD_Response(message="No Data Present"), status=status.HTTP_404_NOT_FOUND) except Exception as e: logger.warning("SOMETHING WENT WRONG" + str(e)) return Response( SMD_Response(message="Please Provide a Valid ID or " "Something Went Wrong"), status=status.HTTP_400_BAD_REQUEST)