def patch(self, request): serializer = UpdateAuthenticatedUserSettingsSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = serializer.validated_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: raise AuthenticationFailed(detail='Password is not valid') has_email = 'email' in data if has_email: new_email = data.get('email') confirm_email_token = user.request_email_update(new_email) self.send_confirmation_email(user, new_email, confirm_email_token) if not has_email and not has_password: return Response(_('Please specify email or password to update'), status=status.HTTP_400_BAD_REQUEST) user_serializer = GetAuthenticatedUserSerializer(user, context={"request": request}) return Response(user_serializer.data, status=status.HTTP_200_OK)
def get(self, request, user_username): request_data = request.data.copy() request_data['username'] = user_username serializer = GetUserSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data username = data.get('username') User = get_user_model() user = User.get_user_with_username(username) user_serializer = None if not request.user.is_anonymous: authenticated_user = request.user if authenticated_user.username == user_username: user_serializer = GetAuthenticatedUserSerializer( user, context={"request": request}) if not user_serializer: user_serializer = GetUserUserSerializer( user, context={"request": request}) return Response(user_serializer.data, status=status.HTTP_200_OK)
def patch(self, request): check_user_is_not_suspended(user=request.user) serializer = UpdateAuthenticatedUserSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = serializer.validated_data user = request.user with transaction.atomic(): user.update( username=data.get('username'), name=data.get('name'), location=data.get('location'), bio=data.get('bio'), url=data.get('url'), followers_count_visible=data.get('followers_count_visible'), community_posts_visible=data.get('community_posts_visible'), visibility=data.get('visibility'), save=False) has_avatar = 'avatar' in data if has_avatar: avatar = data.get('avatar') if avatar is None: user.delete_profile_avatar(save=False) else: user.update_profile_avatar(avatar, save=False) has_cover = 'cover' in data if has_cover: cover = data.get('cover') if cover is None: user.delete_profile_cover(save=False) else: user.update_profile_cover(cover, save=False) user.profile.save() user.save() user_serializer = GetAuthenticatedUserSerializer( user, context={"request": request}) return Response(user_serializer.data, status=status.HTTP_200_OK)
def get(self, request, user_username): request_data = request.data.copy() request_data['username'] = user_username serializer = GetUserSerializer(data=request_data) serializer.is_valid(raise_exception=True) data = serializer.validated_data username = data.get('username') user = request.user if user.username == username: user_serializer = GetAuthenticatedUserSerializer( user, context={"request": request}) else: retrieved_user = user.get_user_with_username(username=username) user_serializer = GetUserUserSerializer( retrieved_user, context={"request": request}) return Response(user_serializer.data, status=status.HTTP_200_OK)
def get(self, request): user_serializer = GetAuthenticatedUserSerializer(request.user, context={"request": request}) return Response(user_serializer.data, status=status.HTTP_200_OK)