refactor: use UserService to remove deprecation warnings

from code and tests.
This commit is contained in:
Jillian Vogel
2021-12-17 14:04:40 +10:30
committed by Piotr Surowiec
parent e378e42bfa
commit f828d89feb
6 changed files with 30 additions and 23 deletions

View File

@@ -78,7 +78,7 @@ class LTI20BlockMixin:
except LTIError:
return Response(status=401) # Unauthorized in this case. 401 is right
real_user = self.system.get_real_user(anon_id)
real_user = self.system.service(self, 'user').get_user_by_anonymous_id(anon_id)
if not real_user: # that means we can't save to database, as we do not have real user id.
msg = f"[LTI]: Real user not found against anon_id: {anon_id}"
log.info(msg)

View File

@@ -78,7 +78,10 @@ from xmodule.mako_module import MakoTemplateBlockBase
from openedx.core.djangolib.markup import HTML, Text
from xmodule.editing_module import EditingMixin
from common.djangoapps.xblock_django.constants import ATTR_KEY_ANONYMOUS_USER_ID
from common.djangoapps.xblock_django.constants import (
ATTR_KEY_ANONYMOUS_USER_ID,
ATTR_KEY_USER_ROLE,
)
from xmodule.lti_2_util import LTI20BlockMixin, LTIError
from xmodule.raw_module import EmptyDataRawMixin
from xmodule.util.xmodule_django import add_webpack_to_fragment
@@ -626,7 +629,8 @@ class LTIBlock(
'staff': 'Administrator',
'instructor': 'Instructor',
}
return roles.get(self.system.get_user_role(), 'Student')
user_role = self.runtime.service(self, 'user').get_current_user().opt_attrs.get(ATTR_KEY_USER_ROLE)
return roles.get(user_role, 'Student')
def get_icon_class(self):
""" Returns the icon class """
@@ -676,17 +680,15 @@ class LTIBlock(
# Username and email can't be sent in studio mode, because the user object is not defined.
# To test functionality test in LMS
if callable(self.runtime.get_real_user):
user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID)
real_user_object = self.runtime.get_real_user(user_id)
try:
self.user_email = real_user_object.email # lint-amnesty, pylint: disable=attribute-defined-outside-init
except AttributeError:
self.user_email = "" # lint-amnesty, pylint: disable=attribute-defined-outside-init
try:
self.user_username = real_user_object.username # lint-amnesty, pylint: disable=attribute-defined-outside-init
except AttributeError:
self.user_username = "" # lint-amnesty, pylint: disable=attribute-defined-outside-init
real_user_object = self.runtime.service(self, 'user').get_user_by_anonymous_id()
try:
self.user_email = real_user_object.email # lint-amnesty, pylint: disable=attribute-defined-outside-init
except AttributeError:
self.user_email = "" # lint-amnesty, pylint: disable=attribute-defined-outside-init
try:
self.user_username = real_user_object.username # lint-amnesty, pylint: disable=attribute-defined-outside-init
except AttributeError:
self.user_username = "" # lint-amnesty, pylint: disable=attribute-defined-outside-init
if self.ask_to_send_username and self.user_username:
body["lis_person_sourcedid"] = self.user_username
@@ -837,7 +839,7 @@ oauth_consumer_key="", oauth_signature="frVp4JuvT1mVXlxktiAUjQ7%2F1cw%3D"'}
log.debug("[LTI]: " + error_message) # lint-amnesty, pylint: disable=logging-not-lazy
return Response(response_xml_template.format(**failure_values), content_type="application/xml")
real_user = self.system.get_real_user(parse.unquote(sourcedId.split(':')[-1]))
real_user = self.runtime.service(self, 'user').get_user_by_anonymous_id(parse.unquote(sourcedId.split(':')[-1]))
if not real_user: # that means we can't save to database, as we do not have real user id.
failure_values['imsx_messageIdentifier'] = escape(imsx_messageIdentifier)
failure_values['imsx_description'] = "User not found."

View File

@@ -29,6 +29,7 @@ from xblock.core import XBlock
from xblock.fields import ScopeIds
from xblock.runtime import KvsFieldData
from common.djangoapps.xblock_django.constants import ATTR_KEY_REQUEST_COUNTRY_CODE
from openedx.core.djangoapps.video_config.models import HLSPlaybackEnabledFlag, CourseYoutubeBlockedFlag
from openedx.core.djangoapps.video_pipeline.config.waffle import DEPRECATE_YOUTUBE, waffle_flags
from openedx.core.lib.cache_utils import request_cached
@@ -108,7 +109,7 @@ EXPORT_IMPORT_STATIC_DIR = 'static'
@XBlock.wants('settings', 'completion', 'i18n', 'request_cache')
@XBlock.needs('mako')
@XBlock.needs('mako', 'user')
class VideoBlock(
VideoFields, VideoTranscriptsMixin, VideoStudioViewHandlers, VideoStudentViewHandlers,
TabsEditingMixin, EmptyDataRawMixin, XmlMixin, EditingMixin,
@@ -281,7 +282,8 @@ class VideoBlock(
# based on user locale. This exists to support cases where
# we leverage a geography specific CDN, like China.
default_cdn_url = getattr(settings, 'VIDEO_CDN_URL', {}).get('default')
cdn_url = getattr(settings, 'VIDEO_CDN_URL', {}).get(self.system.user_location, default_cdn_url)
user_location = self.runtime.service(self, 'user').get_current_user().opt_attrs[ATTR_KEY_REQUEST_COUNTRY_CODE]
cdn_url = getattr(settings, 'VIDEO_CDN_URL', {}).get(user_location, default_cdn_url)
# If we have an edx_video_id, we prefer its values over what we store
# internally for download links (source, html5_sources) and the youtube
@@ -335,7 +337,7 @@ class VideoBlock(
# Video caching is disabled for Studio. User_location is always None in Studio.
# CountryMiddleware disabled for Studio.
if getattr(self, 'video_speed_optimizations', True) and cdn_url:
branding_info = BrandingInfoConfig.get_config().get(self.system.user_location)
branding_info = BrandingInfoConfig.get_config().get(user_location)
if self.edx_video_id and edxval_api and video_status != 'external':
for index, source_url in enumerate(sources):

View File

@@ -34,6 +34,7 @@ from lxml import etree
from path import Path as path
from waffle.testutils import override_flag
from common.djangoapps.xblock_django.constants import ATTR_KEY_REQUEST_COUNTRY_CODE
from lms.djangoapps.courseware.tests.helpers import get_context_dict_from_string
from openedx.core.djangoapps.video_pipeline.config.waffle import DEPRECATE_YOUTUBE, waffle_flags
from openedx.core.djangoapps.waffle_utils.models import WaffleFlagCourseOverrideModel
@@ -886,7 +887,9 @@ class TestGetHtmlMethod(BaseTestVideoXBlock):
self.initialize_block(data=DATA, runtime_kwargs={
'user_location': 'CN',
})
assert self.item_descriptor.xmodule_runtime.user_location == 'CN'
user_service = self.item_descriptor.xmodule_runtime.service(self.item_descriptor, 'user')
user_location = user_service.get_current_user().opt_attrs[ATTR_KEY_REQUEST_COUNTRY_CODE]
assert user_location == 'CN'
context = self.item_descriptor.render('student_view').content
expected_context = dict(initial_context)
expected_context['metadata'].update({

View File

@@ -9,7 +9,6 @@ from django.conf import settings
from xblock.exceptions import NoSuchServiceError
from common.djangoapps.edxmako.shortcuts import render_to_string
from common.djangoapps.xblock_django.constants import ATTR_KEY_ANONYMOUS_USER_ID
def edxnotes(cls):
@@ -43,8 +42,7 @@ def edxnotes(cls):
# - the feature flag or `edxnotes` setting of the course is set to False
# - the user is not authenticated
try:
user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs.get(ATTR_KEY_ANONYMOUS_USER_ID)
user = self.runtime.get_real_user(user_id)
user = self.runtime.service(self, 'user').get_user_by_anonymous_id()
except NoSuchServiceError:
user = None

View File

@@ -33,6 +33,7 @@ from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disa
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.tabs import CourseTab # lint-amnesty, pylint: disable=wrong-import-order
from xmodule.tests.helpers import StubUserService # lint-amnesty, pylint: disable=wrong-import-order
from . import helpers
from .decorators import edxnotes
@@ -81,7 +82,8 @@ class TestProblem:
self.system = MagicMock(is_author_mode=False)
self.scope_ids = MagicMock(usage_id="test_usage_id")
user = user or UserFactory()
self.runtime = MagicMock(course_id=course.id, get_real_user=lambda __: user)
user_service = StubUserService(user)
self.runtime = MagicMock(course_id=course.id, service=lambda _a, _b: user_service)
self.descriptor = MagicMock()
self.descriptor.runtime.modulestore.get_course.return_value = course