From 20caf52bea84ca47b5ab81e0c242ab2e7cbdd152 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 26 Apr 2017 13:09:59 -0400 Subject: [PATCH] Use the existing user object in the case where a user is making changes to their own preferences --- openedx/core/djangoapps/user_api/preferences/api.py | 8 +++++++- .../djangoapps/user_api/preferences/tests/test_api.py | 5 ++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/openedx/core/djangoapps/user_api/preferences/api.py b/openedx/core/djangoapps/user_api/preferences/api.py index 12704eb9a8..f8e0c7c6c4 100644 --- a/openedx/core/djangoapps/user_api/preferences/api.py +++ b/openedx/core/djangoapps/user_api/preferences/api.py @@ -310,7 +310,13 @@ def _get_authorized_user(requesting_user, username=None, allow_staff=False): If username is not provided, requesting_user.username is assumed. """ if username is None: - username = requesting_user.username + # If the user is one that has already been stored to the database, use that + if requesting_user.pk: + return requesting_user + else: + # Otherwise, treat this as a request against a separate user + username = requesting_user.username + try: existing_user = User.objects.get(username=username) except ObjectDoesNotExist: diff --git a/openedx/core/djangoapps/user_api/preferences/tests/test_api.py b/openedx/core/djangoapps/user_api/preferences/tests/test_api.py index 3f7b6615ef..297498b64d 100644 --- a/openedx/core/djangoapps/user_api/preferences/tests/test_api.py +++ b/openedx/core/djangoapps/user_api/preferences/tests/test_api.py @@ -53,9 +53,8 @@ class TestPreferenceAPI(CacheIsolationTestCase): super(TestPreferenceAPI, self).setUp() self.user = UserFactory.create(password=self.password) self.different_user = UserFactory.create(password=self.password) - self.staff_user = UserFactory(is_staff=True, password=self.password) - self.no_such_user = UserFactory.create(password=self.password) - self.no_such_user.username = "no_such_user" + self.staff_user = UserFactory.create(is_staff=True, password=self.password) + self.no_such_user = UserFactory.build(password=self.password, username="no_such_user") self.test_preference_key = "test_key" self.test_preference_value = "test_value" set_user_preference(self.user, self.test_preference_key, self.test_preference_value)