Commit 91f1923d by Sofiya Semenova Committed by GitHub

Merge pull request #15266 from edx/ssemenova/EDUCATOR-367

ED-367 log in production logging when it shouldn't be
parents c3a7088a 6acc6fde
...@@ -68,16 +68,11 @@ def get_request_or_stub(): ...@@ -68,16 +68,11 @@ def get_request_or_stub():
request that can be used to build an absolute URI. request that can be used to build an absolute URI.
This is useful in cases where we need to pass in a request object 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() request = crum.get_current_request()
if request is None: 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 # The settings SITE_NAME may contain a port number, so we need to
# parse the full URL. # parse the full URL.
......
...@@ -29,6 +29,15 @@ class UserPreference(models.Model): ...@@ -29,6 +29,15 @@ class UserPreference(models.Model):
class Meta(object): class Meta(object):
unique_together = ("user", "key") 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 @classmethod
def get_value(cls, user, preference_key, default=None): def get_value(cls, user, preference_key, default=None):
"""Gets the user preference value for a given key. """Gets the user preference value for a given key.
......
...@@ -72,18 +72,7 @@ def get_user_preferences(requesting_user, username=None): ...@@ -72,18 +72,7 @@ def get_user_preferences(requesting_user, username=None):
UserAPIInternalError: the operation failed due to an unexpected error. UserAPIInternalError: the operation failed due to an unexpected error.
""" """
existing_user = _get_authorized_user(requesting_user, username, allow_staff=True) existing_user = _get_authorized_user(requesting_user, username, allow_staff=True)
return UserPreference.get_all_preferences(existing_user)
# 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"]
@intercept_errors(UserAPIInternalError, ignore_errors=[UserAPIRequestError]) @intercept_errors(UserAPIInternalError, ignore_errors=[UserAPIRequestError])
......
...@@ -28,7 +28,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): ...@@ -28,7 +28,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
""" """
Returns the set of preferences as a dict for the specified user 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): class Meta(object):
model = User model = User
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment