def put(self, request, login): receiver = get_profile_by_user_login_or_404(login).user if check_if_user_is_banned(receiver): instance = get_ban_object_by_login_or_404(login) serializer = BanSerializer(instance, request.data) if serializer.is_valid(): instance = serializer.save() return Response(BannedUserSerializer(instance).data) raise_400_based_on_serializer(serializer) if receiver.is_staff: raise Forbidden403("Admins cannot be banned") serializer = BanSerializer(data=request.data) if serializer.is_valid(): ban_object = ban_user(receiver=receiver, creator=self.request.user, reason=serializer.validated_data["reason"]) return Response(BannedUserSerializer(ban_object).data, status=HTTP_201_CREATED) raise_400_based_on_serializer(serializer)
def delete(self, request, login): target = get_profile_by_user_login_or_404(login).user if not is_following(request.user, target): raise NotFound404("You are not yet followed this user") unfollow(request.user, target) return Response( data={"isFollowed": is_following(request.user, target)})
def put(self, request, login): target = get_profile_by_user_login_or_404(login).user if login == request.user.login: raise BadRequest400("You cannot follow yourself") if not is_following(request.user, target): follow(request.user, target) return Response( data={"isFollowed": is_following(request.user, target)})
def get(self, request, login): target = get_profile_by_user_login_or_404(login).user return Response( data={"isFollowed": is_following(request.user, target)})
def filter_queryset(self, queryset, kwargs): target_user = get_profile_by_user_login_or_404(kwargs["login"]).user posts = queryset.filter(author=target_user) return super().filter_queryset(posts, kwargs)
def retrieve(self, request, *args, **kwargs): instance = get_profile_by_user_login_or_404(kwargs["login"]) serializer = ProfileSerializer(instance) return Response(serializer.data)
def filter_queryset(self, queryset, kwargs): target_user = get_profile_by_user_login_or_404(kwargs["login"]).user followings_ids = get_user_followings_ids_list(target_user) queryset_of_followings = queryset.filter(id__in=followings_ids) return super().filter_queryset(queryset_of_followings, kwargs)