Beispiel #1
0
    def _convert_django_user_to_xblock_user(self, django_user):
        """
        A function that returns an XBlockUser from the current Django request.user
        """
        xblock_user = XBlockUser(is_current_user=True)

        if django_user is not None and django_user.is_authenticated:
            # This full_name is dependent on edx-platform's profile implementation
            if hasattr(django_user, 'profile'):
                full_name = django_user.profile.name
            else:
                full_name = None
            xblock_user.full_name = full_name
            xblock_user.emails = [django_user.email]
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = True
            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_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:
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False

        return xblock_user
    def _convert_django_user_to_xblock_user(self, django_user):
        """
        A function that returns an XBlockUser from the current Django request.user
        """
        xblock_user = XBlockUser(is_current_user=True)

        if django_user is not None and django_user.is_authenticated:
            # This full_name is dependent on edx-platform's profile implementation
            if hasattr(django_user, 'profile'):
                full_name = django_user.profile.name
            else:
                full_name = None
            xblock_user.full_name = full_name
            xblock_user.emails = [django_user.email]
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = True
            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_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:
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False

        return xblock_user
 def get_current_user(self):
     """
     Implements abstract method for getting the current user.
     """
     user = XBlockUser()
     user.opt_attrs['edx-platform.username'] = '******'
     return user
Beispiel #4
0
 def __init__(self, uid):
     """
     Initialize user
     """
     self._user = XBlockUser(
         is_current_user=True,
         emails=["*****@*****.**"],
         full_name="XBlock User ({})".format(uid),
     )
     self._user.opt_attrs['xblock-workbench.user_id'] = uid
 def __init__(self, uid):
     """
     Initialize user
     """
     user = XBlockUser(
         is_current_user=True,
         emails=["*****@*****.**"],
         full_name=u"XBlock User ({})".format(uid),
     )
     user.opt_attrs['xblock-workbench.user_id'] = uid
     super(WorkBenchUserService, self).__init__(user=user)
Beispiel #6
0
 def get_current_user(self):
     """
     Implements abstract method for getting the current user.
     """
     user = XBlockUser()
     if self.is_anonymous:
         user.opt_attrs['edx-platform.username'] = '******'
         user.opt_attrs['edx-platform.is_authenticated'] = False
     else:
         user.opt_attrs['edx-platform.username'] = '******'
     return user
Beispiel #7
0
 def __init__(self, uid):
     """
     Initialize user
     """
     user = XBlockUser(
         is_current_user=True,
         emails=["*****@*****.**"],
         full_name=f"XBlock User ({uid})",
     )
     user.opt_attrs['xblock-workbench.user_id'] = uid
     super().__init__(user=user)
Beispiel #8
0
    def _convert_django_user_to_xblock_user(self, django_user):
        """
        A function that returns an XBlockUser from the current Django request.user
        """
        xblock_user = XBlockUser(is_current_user=True)

        if django_user is not None and django_user.is_authenticated():
            # This full_name is dependent on edx-platform's profile implementation
            full_name = getattr(django_user.profile, 'name') if hasattr(django_user, 'profile') else None
            xblock_user.full_name = full_name
            xblock_user.emails = [django_user.email]
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = True
            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_USER_IS_STAFF] = django_user.user_is_staff
        else:
            xblock_user.opt_attrs[ATTR_KEY_IS_AUTHENTICATED] = False

        return xblock_user
Beispiel #9
0
def test_dummy_user_service_current_user():
    """
    Tests that get_current_user() works on a dummy user service.
    """
    user = XBlockUser(full_name="tester")
    user_service = SingleUserService(user)
    current_user = user_service.get_current_user()
    assert current_user == user
    assert current_user.full_name == "tester"
    # assert that emails is an Iterable but not a string
    assert isinstance(current_user.emails, collections.Iterable)
    assert not isinstance(current_user.emails, (six.text_type, six.binary_type))
    # assert that opt_attrs is a Mapping
    assert isinstance(current_user.opt_attrs, collections.Mapping)
Beispiel #10
0
def test_dummy_user_service_current_user():
    """
    Tests that get_current_user() works on a dummy user service.
    """
    user = XBlockUser(full_name="tester")
    user_service = SingleUserService(user)
    current_user = user_service.get_current_user()
    assert_equals(current_user, user)
    assert_equals(current_user.full_name, "tester")
    # assert that emails is an Iterable but not a string
    assert_is_instance(current_user.emails, collections.Iterable)
    assert_false(isinstance(current_user.emails, basestring))
    # assert that opt_attrs is a Mapping
    assert_is_instance(current_user.opt_attrs, collections.Mapping)
Beispiel #11
0
 def get_current_user(self):
     """
     Implements abstract method for getting the current user.
     """
     user = XBlockUser()
     if self.user and self.user.is_authenticated:
         user.opt_attrs['edx-platform.anonymous_user_id'] = self.anonymous_user_id
         user.opt_attrs['edx-platform.request_country_code'] = self.request_country_code
         user.opt_attrs['edx-platform.user_is_staff'] = self.user_is_staff
         user.opt_attrs['edx-platform.user_id'] = self.user.id
         user.opt_attrs['edx-platform.user_role'] = self.user_role
         user.opt_attrs['edx-platform.username'] = self.user.username
     else:
         user.opt_attrs['edx-platform.username'] = '******'
         user.opt_attrs['edx-platform.request_country_code'] = self.request_country_code
         user.opt_attrs['edx-platform.is_authenticated'] = False
     return user
Beispiel #12
0
    def _refresh_children(self, lib_content_block, status_code_expected=200):
        """
        Helper method: Uses the REST API to call the 'refresh_children' handler
        of a LibraryContent block
        """
        if 'user' not in lib_content_block.runtime._services:  # pylint: disable=protected-access
            mocked_user_service = Mock(user_id=self.user.id)
            mocked_user_service.get_current_user.return_value = XBlockUser(
                is_current_user=True)
            lib_content_block.runtime._services['user'] = mocked_user_service  # pylint: disable=protected-access

        handler_url = reverse_usage_url('component_handler',
                                        lib_content_block.location,
                                        kwargs={'handler': 'refresh_children'})
        response = self.client.ajax_post(handler_url)
        self.assertEqual(response.status_code, status_code_expected)
        return modulestore().get_item(lib_content_block.location)
Beispiel #13
0
 def mocked_get_current_user(block):
     return XBlockUser(is_current_user=True,
                       emails=["*****@*****.**"],
                       full_name="John Doe")