示例#1
0
    def patch(self, request):
        data = validate_data(UpdateAuthenticatedUserSettingsSerializer,
                             request.data)

        user = request.user

        with transaction.atomic():
            has_password = "******" in data
            if has_password:
                current_password = data.get("current_password")
                new_password = data.get("new_password")
                if user.check_password(current_password):
                    user.update_password(password=new_password)
                else:
                    return Response(
                        "Your current password invalid!",
                        status=status.HTTP_400_BAD_REQUEST,
                    )

            if not has_password:
                return Response(
                    "Please specify password to update",
                    status=status.HTTP_400_BAD_REQUEST,
                )

        user_serializer = UserInfoSerializer(user,
                                             context={"request": request})
        return Response(user_serializer.data, status=status.HTTP_200_OK)
示例#2
0
    def patch(self, request):
        data = validate_data(UpdateAuthenticatedUserSerializer, request.data)

        user = request.user

        with transaction.atomic():
            user.update(
                username=data.get("username"),
                email=data.get("email"),
                birthday=data.get("birthday"),
                gender=data.get("gender"),
                occupation=data.get("occupation"),
                save=False,
            )

            has_avatar = "avatar" in data
            if has_avatar:
                avatar = data.get("avatar")
                if avatar is None:
                    user.delete_avatar(save=False)
                else:
                    user.update_avatar(avatar, save=False)

            user.save()

        user_serializer = AuthenticatedUserInfoSerializer(
            user, context={"request": request})
        return Response(user_serializer.data, status=status.HTTP_200_OK)
示例#3
0
    def get(self, request, movie_slug):
        data = validate_data(GetMovieSerializer,
                             data={"movie_slug": movie_slug})
        movie_id = data.get("movie_slug")

        Movie = get_movie_model()
        movie = Movie.get_movie_with_slug(movie_slug)

        movie_serializer = MovieSerializer(movie, context={"request": request})
        return Response(movie_serializer.data, status=status.HTTP_200_OK)
示例#4
0
    def post(self, request):
        data = validate_data(DeleteAuthenticatedUserSerializer, request.data)

        user = request.user
        password = data.get("password")

        with transaction.atomic():
            user.delete_with_password(password=password)

        return ApiMessageResponse("Goodbye 😔", status=status.HTTP_200_OK)
示例#5
0
    def get(self, request, rating_id):
        data = validate_data(
            GetRatingItemSerializer, data={"rating_id": rating_id}
        )
        rating_id = data.get("rating_id")

        Rating = get_rating_model()
        rating = Rating.get_with_id(rating_id)

        rating_serializer = RatingSerializer(
            rating, context={"request": request}
        )
        return Response(rating_serializer.data, status=status.HTTP_200_OK)
示例#6
0
    def delete(self, request, rating_id):
        data = validate_data(
            GetRatingItemSerializer, data={"rating_id": rating_id}
        )
        rating_id = data.get("rating_id")

        Rating = get_rating_model()

        with transaction.atomic():
            rating = Rating.delete_with_id(rating_id)

        return ApiMessageResponse(
            "Delete rating successfully.", status=status.HTTP_200_OK
        )
示例#7
0
    def get_queryset(self):
        qs = super(Ratings, self).get_queryset()

        data = validate_data(
            GetRatingsSerializer,
            data={
                "movie_slug": self.request.query_params.get("movie_slug", None)
            },
        )
        movie_slug = data.get("movie_slug")

        if movie_slug is not None:
            return qs.filter(movie__slug=movie_slug)

        return qs
示例#8
0
    def get(self, request, username):
        data = validate_data(GetUserDataSerializer,
                             data={"username": username})
        username = data.get("username")

        user = request.user

        if user.username == username:
            target_user = user
        else:
            target_user = User.get_with_username(username=username)

        user_serializer = UserInfoSerializer(target_user,
                                             context={"request": request})

        return Response(user_serializer.data, status=status.HTTP_200_OK)
示例#9
0
    def post(self, request):
        request_data = get_request_data(request)

        data = validate_data(PostRatingSerializer, request_data)
        movie_slug = data.get("movie_slug")
        rating = data.get("rating")

        user = request.user

        with transaction.atomic():
            movie_rating = user.rating_with_movie_slug(
                movie_slug=movie_slug, user=user, rating=rating
            )

        movie_rating_serializer = RatingSerializer(
            movie_rating, context={"request": request}
        )
        return Response(
            movie_rating_serializer.data, status=status.HTTP_200_OK
        )