From da9266e31d72e96706b5a8be464885480cb78c72 Mon Sep 17 00:00:00 2001 From: KyryloKireiev Date: Wed, 8 Nov 2023 17:43:57 +0200 Subject: [PATCH] refactor: [FC-0031] Move get_profile_image method to api --- lms/djangoapps/discussion/rest_api/serializers.py | 4 ++-- openedx/core/djangoapps/user_api/accounts/api.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lms/djangoapps/discussion/rest_api/serializers.py b/lms/djangoapps/discussion/rest_api/serializers.py index f080ae25a1..5f18281c42 100644 --- a/lms/djangoapps/discussion/rest_api/serializers.py +++ b/lms/djangoapps/discussion/rest_api/serializers.py @@ -41,8 +41,8 @@ from openedx.core.djangoapps.django_comment_common.comment_client.thread import from openedx.core.djangoapps.django_comment_common.comment_client.user import User as CommentClientUser from openedx.core.djangoapps.django_comment_common.comment_client.utils import CommentClientRequestError from openedx.core.djangoapps.django_comment_common.models import CourseDiscussionSettings +from openedx.core.djangoapps.user_api.accounts.api import get_profile_images from openedx.core.lib.api.serializers import CourseKeyField -from openedx.core.djangoapps.user_api.accounts.serializers import AccountLegacyProfileSerializer User = get_user_model() @@ -588,7 +588,7 @@ class CommentSerializer(_ContentSerializer): def get_profile_image(self, obj): request = self.context["request"] - return AccountLegacyProfileSerializer.get_profile_image(request.user.profile, request.user, request) + return get_profile_images(request.user.profile, request.user, request) def validate(self, attrs): """ diff --git a/openedx/core/djangoapps/user_api/accounts/api.py b/openedx/core/djangoapps/user_api/accounts/api.py index bc4fd1d1a7..786a903c64 100644 --- a/openedx/core/djangoapps/user_api/accounts/api.py +++ b/openedx/core/djangoapps/user_api/accounts/api.py @@ -33,6 +33,8 @@ from openedx.core.djangoapps.user_api.errors import ( AccountValidationError, PreferenceValidationError ) +from openedx.core.djangoapps.user_api.accounts.image_helpers import get_profile_image_urls_for_user +from openedx.core.djangoapps.user_api.accounts.serializers import PROFILE_IMAGE_KEY_PREFIX from openedx.core.djangoapps.user_api.preferences.api import update_user_preferences from openedx.core.djangoapps.user_authn.utils import check_pwned_password from openedx.core.djangoapps.user_authn.views.registration_form import validate_name, validate_username @@ -524,6 +526,19 @@ def get_email_existence_validation_error(email): return _validate(_validate_email_doesnt_exist, errors.AccountEmailAlreadyExists, email) +def get_profile_images(user_profile, user, request=None): + """ + Returns metadata about a user's profile image. + """ + data = {'has_image': user_profile.has_profile_image} + urls = get_profile_image_urls_for_user(user, request) + data.update({ + f'{PROFILE_IMAGE_KEY_PREFIX}_{size_display_name}': url + for size_display_name, url in urls.items() + }) + return data + + def _get_user_and_profile(username): """ Helper method to return the legacy user and profile objects based on username.