From 6acc6fde95745520c8622d2b65fccb96bbdc1438 Mon Sep 17 00:00:00 2001 From: Sofiya Semenova Date: Wed, 7 Jun 2017 17:57:09 +0000 Subject: [PATCH] ED-367 log in production --- common/djangoapps/request_cache/__init__.py | 7 +------ openedx/core/djangoapps/user_api/models.py | 9 +++++++++ openedx/core/djangoapps/user_api/preferences/api.py | 13 +------------ openedx/core/djangoapps/user_api/serializers.py | 2 +- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/common/djangoapps/request_cache/__init__.py b/common/djangoapps/request_cache/__init__.py index 419508fd74..4e64747f9b 100644 --- a/common/djangoapps/request_cache/__init__.py +++ b/common/djangoapps/request_cache/__init__.py @@ -68,16 +68,11 @@ def get_request_or_stub(): request that can be used to build an absolute URI. This is useful in cases where we need to pass in a request object - but don't have an active request (for example, in test cases). + but don't have an active request (for example, in tests, celery tasks, and XBlocks). """ request = crum.get_current_request() if request is None: - log.warning( - "Could not retrieve the current request. " - "A stub request will be created instead using settings.SITE_NAME. " - "This should be used *only* in test cases, never in production!" - ) # The settings SITE_NAME may contain a port number, so we need to # parse the full URL. diff --git a/openedx/core/djangoapps/user_api/models.py b/openedx/core/djangoapps/user_api/models.py index 5729d92702..6ebac66ec2 100644 --- a/openedx/core/djangoapps/user_api/models.py +++ b/openedx/core/djangoapps/user_api/models.py @@ -29,6 +29,15 @@ class UserPreference(models.Model): class Meta(object): unique_together = ("user", "key") + @staticmethod + def get_all_preferences(user): + """ + Gets all preferences for a given user + + Returns: Set of (preference type, value) pairs for each of the user's preferences + """ + return dict([(pref.key, pref.value) for pref in user.preferences.all()]) + @classmethod def get_value(cls, user, preference_key, default=None): """Gets the user preference value for a given key. diff --git a/openedx/core/djangoapps/user_api/preferences/api.py b/openedx/core/djangoapps/user_api/preferences/api.py index ed6e530a66..58dcb28532 100644 --- a/openedx/core/djangoapps/user_api/preferences/api.py +++ b/openedx/core/djangoapps/user_api/preferences/api.py @@ -72,18 +72,7 @@ def get_user_preferences(requesting_user, username=None): UserAPIInternalError: the operation failed due to an unexpected error. """ existing_user = _get_authorized_user(requesting_user, username, allow_staff=True) - - # Django Rest Framework V3 uses the current request to version - # hyperlinked URLS, so we need to retrieve the request and pass - # it in the serializer's context (otherwise we get an AssertionError). - # We're retrieving the request from the cache rather than passing it in - # as an argument because this is an implementation detail of how we're - # serializing data, which we want to encapsulate in the API call. - context = { - "request": get_request_or_stub() - } - user_serializer = UserSerializer(existing_user, context=context) - return user_serializer.data["preferences"] + return UserPreference.get_all_preferences(existing_user) @intercept_errors(UserAPIInternalError, ignore_errors=[UserAPIRequestError]) diff --git a/openedx/core/djangoapps/user_api/serializers.py b/openedx/core/djangoapps/user_api/serializers.py index 2ba351f4f2..d879c91c03 100644 --- a/openedx/core/djangoapps/user_api/serializers.py +++ b/openedx/core/djangoapps/user_api/serializers.py @@ -28,7 +28,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): """ Returns the set of preferences as a dict for the specified user """ - return dict([(pref.key, pref.value) for pref in user.preferences.all()]) + return UserPreference.get_all_preferences(user) class Meta(object): model = User