Commit 94100f59 by Kevin Kim

Convert ORA due dates to user preferred time zone

parent 82c612f1
...@@ -25,6 +25,7 @@ from openedx.core.djangoapps.content.course_structures.tests import SignalDiscon ...@@ -25,6 +25,7 @@ from openedx.core.djangoapps.content.course_structures.tests import SignalDiscon
from xblock_django.user_service import DjangoXBlockUserService from xblock_django.user_service import DjangoXBlockUserService
from xmodule.x_module import STUDIO_VIEW from xmodule.x_module import STUDIO_VIEW
from student import auth from student import auth
from student.tests.factories import UserFactory
class LibraryTestCase(ModuleStoreTestCase): class LibraryTestCase(ModuleStoreTestCase):
...@@ -34,6 +35,7 @@ class LibraryTestCase(ModuleStoreTestCase): ...@@ -34,6 +35,7 @@ class LibraryTestCase(ModuleStoreTestCase):
def setUp(self): def setUp(self):
super(LibraryTestCase, self).setUp() super(LibraryTestCase, self).setUp()
self.user = UserFactory(password=self.user_password, is_staff=True)
self.client = AjaxEnabledTestClient() self.client = AjaxEnabledTestClient()
self._login_as_staff_user(logout_first=False) self._login_as_staff_user(logout_first=False)
...@@ -477,7 +479,8 @@ class TestLibraryAccess(SignalDisconnectTestMixin, LibraryTestCase): ...@@ -477,7 +479,8 @@ class TestLibraryAccess(SignalDisconnectTestMixin, LibraryTestCase):
def setUp(self): def setUp(self):
""" Create a library, staff user, and non-staff user """ """ Create a library, staff user, and non-staff user """
super(TestLibraryAccess, self).setUp() super(TestLibraryAccess, self).setUp()
self.non_staff_user, self.non_staff_user_password = self.create_non_staff_user() self.non_staff_user_password = 'foo'
self.non_staff_user = UserFactory(password=self.non_staff_user_password, is_staff=False)
def _login_as_non_staff_user(self, logout_first=True): def _login_as_non_staff_user(self, logout_first=True):
""" Login as a user that starts out with no roles/permissions granted. """ """ Login as a user that starts out with no roles/permissions granted. """
......
...@@ -1217,6 +1217,7 @@ class TestEditSplitModule(ItemTest): ...@@ -1217,6 +1217,7 @@ class TestEditSplitModule(ItemTest):
""" """
def setUp(self): def setUp(self):
super(TestEditSplitModule, self).setUp() super(TestEditSplitModule, self).setUp()
self.user = UserFactory()
self.course.user_partitions = [ self.course.user_partitions = [
UserPartition( UserPartition(
0, 'first_partition', 'First Partition', 0, 'first_partition', 'First Partition',
......
...@@ -8,10 +8,13 @@ from xblock_django.user_service import ( ...@@ -8,10 +8,13 @@ from xblock_django.user_service import (
ATTR_KEY_USER_ID, ATTR_KEY_USER_ID,
ATTR_KEY_USERNAME, ATTR_KEY_USERNAME,
ATTR_KEY_USER_IS_STAFF, ATTR_KEY_USER_IS_STAFF,
ATTR_KEY_USER_PREFERENCES,
USER_PREFERENCES_WHITE_LIST,
) )
from student.models import anonymous_id_for_user from student.models import anonymous_id_for_user
from student.tests.factories import UserFactory, AnonymousUserFactory from student.tests.factories import UserFactory, AnonymousUserFactory
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from openedx.core.djangoapps.user_api.preferences.api import set_user_preference
class UserServiceTestCase(TestCase): class UserServiceTestCase(TestCase):
...@@ -22,6 +25,9 @@ class UserServiceTestCase(TestCase): ...@@ -22,6 +25,9 @@ class UserServiceTestCase(TestCase):
super(UserServiceTestCase, self).setUp() super(UserServiceTestCase, self).setUp()
self.user = UserFactory(username="tester", email="test@tester.com") self.user = UserFactory(username="tester", email="test@tester.com")
self.user.profile.name = "Test Tester" self.user.profile.name = "Test Tester"
set_user_preference(self.user, 'pref-lang', 'en')
set_user_preference(self.user, 'time_zone', 'US/Pacific')
set_user_preference(self.user, 'not_white_listed', 'hidden_value')
self.anon_user = AnonymousUserFactory() self.anon_user = AnonymousUserFactory()
def assert_is_anon_xb_user(self, xb_user): def assert_is_anon_xb_user(self, xb_user):
...@@ -42,6 +48,12 @@ class UserServiceTestCase(TestCase): ...@@ -42,6 +48,12 @@ class UserServiceTestCase(TestCase):
self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USERNAME], dj_user.username) self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USERNAME], dj_user.username)
self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USER_ID], dj_user.id) self.assertEqual(xb_user.opt_attrs[ATTR_KEY_USER_ID], dj_user.id)
self.assertFalse(xb_user.opt_attrs[ATTR_KEY_USER_IS_STAFF]) self.assertFalse(xb_user.opt_attrs[ATTR_KEY_USER_IS_STAFF])
self.assertTrue(
all(
pref in USER_PREFERENCES_WHITE_LIST
for pref in xb_user.opt_attrs[ATTR_KEY_USER_PREFERENCES]
)
)
def test_convert_anon_user(self): def test_convert_anon_user(self):
""" """
......
...@@ -3,13 +3,16 @@ Support for converting a django user to an XBlock user ...@@ -3,13 +3,16 @@ Support for converting a django user to an XBlock user
""" """
from django.contrib.auth.models import User from django.contrib.auth.models import User
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from xblock.reference.user_service import XBlockUser, UserService from openedx.core.djangoapps.user_api.preferences.api import get_user_preferences
from student.models import anonymous_id_for_user, get_user_by_username_or_email from student.models import anonymous_id_for_user, get_user_by_username_or_email
from xblock.reference.user_service import XBlockUser, UserService
ATTR_KEY_IS_AUTHENTICATED = 'edx-platform.is_authenticated' ATTR_KEY_IS_AUTHENTICATED = 'edx-platform.is_authenticated'
ATTR_KEY_USER_ID = 'edx-platform.user_id' ATTR_KEY_USER_ID = 'edx-platform.user_id'
ATTR_KEY_USERNAME = 'edx-platform.username' ATTR_KEY_USERNAME = 'edx-platform.username'
ATTR_KEY_USER_IS_STAFF = 'edx-platform.user_is_staff' ATTR_KEY_USER_IS_STAFF = 'edx-platform.user_is_staff'
ATTR_KEY_USER_PREFERENCES = 'edx-platform.user_preferences'
USER_PREFERENCES_WHITE_LIST = ['pref-lang', 'time_zone']
class DjangoXBlockUserService(UserService): class DjangoXBlockUserService(UserService):
...@@ -69,6 +72,12 @@ class DjangoXBlockUserService(UserService): ...@@ -69,6 +72,12 @@ class DjangoXBlockUserService(UserService):
xblock_user.opt_attrs[ATTR_KEY_USER_ID] = django_user.id xblock_user.opt_attrs[ATTR_KEY_USER_ID] = django_user.id
xblock_user.opt_attrs[ATTR_KEY_USERNAME] = django_user.username xblock_user.opt_attrs[ATTR_KEY_USERNAME] = django_user.username
xblock_user.opt_attrs[ATTR_KEY_USER_IS_STAFF] = django_user.user_is_staff xblock_user.opt_attrs[ATTR_KEY_USER_IS_STAFF] = django_user.user_is_staff
user_preferences = get_user_preferences(django_user)
xblock_user.opt_attrs[ATTR_KEY_USER_PREFERENCES] = {
pref: user_preferences.get(pref)
for pref in USER_PREFERENCES_WHITE_LIST
if pref in user_preferences
}
else: else:
xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False
......
...@@ -31,7 +31,7 @@ class UserPreference(models.Model): ...@@ -31,7 +31,7 @@ class UserPreference(models.Model):
unique_together = ("user", "key") unique_together = ("user", "key")
@classmethod @classmethod
def get_value(cls, user, preference_key): 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.
Note: Note:
...@@ -42,15 +42,16 @@ class UserPreference(models.Model): ...@@ -42,15 +42,16 @@ class UserPreference(models.Model):
Arguments: Arguments:
user (User): The user whose preference should be set. user (User): The user whose preference should be set.
preference_key (str): The key for the user preference. preference_key (str): The key for the user preference.
default: The object to return if user does not have preference key set
Returns: Returns:
The user preference value, or None if one is not set. The user preference value, or default if one is not set.
""" """
try: try:
user_preference = cls.objects.get(user=user, key=preference_key) user_preference = cls.objects.get(user=user, key=preference_key)
return user_preference.value return user_preference.value
except cls.DoesNotExist: except cls.DoesNotExist:
return None return default
@receiver(pre_save, sender=UserPreference) @receiver(pre_save, sender=UserPreference)
......
...@@ -93,6 +93,10 @@ class UserPreferenceModelTest(ModuleStoreTestCase): ...@@ -93,6 +93,10 @@ class UserPreferenceModelTest(ModuleStoreTestCase):
pref = UserPreference.get_value(user, 'testkey_none') pref = UserPreference.get_value(user, 'testkey_none')
self.assertIsNone(pref) self.assertIsNone(pref)
# get default value for key that doesn't exist for user
pref = UserPreference.get_value(user, 'testkey_none', 'default_value')
self.assertEqual('default_value', pref)
class TestUserPreferenceEvents(UserSettingsEventTestMixin, TestCase): class TestUserPreferenceEvents(UserSettingsEventTestMixin, TestCase):
""" """
......
...@@ -11,10 +11,8 @@ def get_user_time_zone(user): ...@@ -11,10 +11,8 @@ def get_user_time_zone(user):
Returns pytz time zone object of the user's time zone if available or UTC time zone if unavailable Returns pytz time zone object of the user's time zone if available or UTC time zone if unavailable
""" """
#TODO: exception for unknown timezones? #TODO: exception for unknown timezones?
time_zone = user.preferences.model.get_value(user, "time_zone") time_zone = user.preferences.model.get_value(user, "time_zone", 'utc')
if time_zone is not None: return timezone(time_zone)
return timezone(time_zone)
return utc
def _format_time_zone_string(time_zone, date_time, format_string): def _format_time_zone_string(time_zone, date_time, format_string):
......
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