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)