Пример #1
0
 def put(self, request, pk, format=None):
   movie = self.get_object(pk)
   serializer = MovieSerializer(movie, data=request.data)
   if serializer.is_valid():
     serializer.save()
     return Response(serializer.data)
   return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #2
0
def movie_detail(request, pk):
    """
    Get, update, or delete a specific movie
    """
    try:
        movie = Movie.objects.get(pk=pk)
    except Movie.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = MovieSerializer(movie)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = MovieSerializer(movie, data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(
                serilizer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        movie.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #3
0
def movie_add(request):
    """
    add a movie
    """
    if 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)
Пример #4
0
 def post(self, request, format=None):
   serializer = MovieSerializer(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)
Пример #5
0
def movie_detail(request, movie_id):
    movie = get_object_or_404(Movie, id=movie_id)

    if request.method == 'GET':
        serializer = MovieSerializer(movie)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    if request.method == 'DELETE':
        username = request.data.get("username", None)
        token = request.data.get("token", None)
        user = User.objects.get(username=username)

        if not user.is_staff:
            return Response(
                data={"error": "권한 없음"},
                status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)

        if user.refresh_token == token:
            response = verify_token(token)
            if response.status_code != 200:
                response = refresh_token(token)
                if response.status_code == 200:
                    new_token = json.loads(response.text)["token"]
                    user.refresh_token = new_token

            if response and response.status_code == 200:
                movie.delete()
                return Response(status=status.HTTP_202_ACCEPTED)
        user.refresh_token = ""
        user.save()
        return Response(data={"error": "token"},
                        status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)

    if request.method == 'PUT':
        username = request.data.get("username", None)
        token = request.data.get("token", None)
        user = User.objects.get(username=username)

        if not user.is_staff:
            return Response(
                data={"error": "권한 없음"},
                status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)

        if user.refresh_token == token:
            response = verify_token(token)
            if response.status_code != 200:
                response = refresh_token(token)
                if response.status_code == 200:
                    new_token = json.loads(response.text)["token"]
                    user.refresh_token = new_token

            if response and response.status_code == 200:
                movie_info = request.data.get("movie", None)

                if movie_info:
                    movie.title = movie_info["title"]
                    genres = movie_info["genres"]
                    genres = '|'.join(genres)
                    movie.genres = genres
                    movie.save()
                    return Response(status=status.HTTP_202_ACCEPTED)
                return Response(data={"error": "입력값이 없습니다."},
                                status=status.HTTP_204_NO_CONTENT)

        user.refresh_token = ""
        user.save()
        return Response(data={"error": "token"},
                        status=status.HTTP_203_NON_AUTHORITATIVE_INFORMATION)