diff --git a/cms/djangoapps/contentstore/course_info_model.py b/cms/djangoapps/contentstore/course_info_model.py index 7ba75ff215..570763bd28 100644 --- a/cms/djangoapps/contentstore/course_info_model.py +++ b/cms/djangoapps/contentstore/course_info_model.py @@ -22,7 +22,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.django import modulestore from xmodule.html_module import CourseInfoModule -from xmodule_modifiers import get_course_update_items +from openedx.core.lib.xblock_utils import get_course_update_items from cms.djangoapps.contentstore.push_notification import enqueue_push_course_update # # This should be in a class which inherits from XmlDescriptor diff --git a/cms/djangoapps/contentstore/views/item.py b/cms/djangoapps/contentstore/views/item.py index a50cbc2e16..ae197ee0f2 100644 --- a/cms/djangoapps/contentstore/views/item.py +++ b/cms/djangoapps/contentstore/views/item.py @@ -11,7 +11,7 @@ import json from collections import OrderedDict from functools import partial from static_replace import replace_static_urls -from xmodule_modifiers import wrap_xblock, request_token +from openedx.core.lib.xblock_utils import wrap_xblock, request_token import dogstats_wrapper as dog_stats_api from django.conf import settings diff --git a/cms/djangoapps/contentstore/views/preview.py b/cms/djangoapps/contentstore/views/preview.py index 83ad1f6d65..7305eb248a 100644 --- a/cms/djangoapps/contentstore/views/preview.py +++ b/cms/djangoapps/contentstore/views/preview.py @@ -9,7 +9,7 @@ from django.http import Http404, HttpResponseBadRequest from django.contrib.auth.decorators import login_required from edxmako.shortcuts import render_to_string -from xmodule_modifiers import replace_static_urls, wrap_xblock, wrap_fragment, request_token +from openedx.core.lib.xblock_utils import replace_static_urls, wrap_xblock, wrap_fragment, request_token from xmodule.x_module import PREVIEW_VIEWS, STUDENT_VIEW, AUTHOR_VIEW from xmodule.contentstore.django import contentstore from xmodule.error_module import ErrorDescriptor diff --git a/lms/djangoapps/courseware/module_render.py b/lms/djangoapps/courseware/module_render.py index f381edd3fc..720304ac51 100644 --- a/lms/djangoapps/courseware/module_render.py +++ b/lms/djangoapps/courseware/module_render.py @@ -57,7 +57,7 @@ from opaque_keys.edx.locations import SlashSeparatedCourseKey from xmodule.contentstore.django import contentstore from xmodule.modulestore.django import modulestore, ModuleI18nService from xmodule.modulestore.exceptions import ItemNotFoundError -from xmodule_modifiers import ( +from openedx.core.lib.xblock_utils import ( replace_course_urls, replace_jump_to_id_urls, replace_static_urls, diff --git a/lms/djangoapps/courseware/tests/test_module_render.py b/lms/djangoapps/courseware/tests/test_module_render.py index b8f4bebc93..68117b06f4 100644 --- a/lms/djangoapps/courseware/tests/test_module_render.py +++ b/lms/djangoapps/courseware/tests/test_module_render.py @@ -1000,7 +1000,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase): self.user, self.descriptor ) - with patch('xmodule_modifiers.grade_histogram') as mock_grade_histogram: + with patch('openedx.core.lib.xblock_utils.grade_histogram') as mock_grade_histogram: mock_grade_histogram.return_value = [] module = render.get_module( self.user, @@ -1022,7 +1022,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase): max_grade=1, state="{}", ) - with patch('xmodule_modifiers.grade_histogram') as mock_grade_histogram: + with patch('openedx.core.lib.xblock_utils.grade_histogram') as mock_grade_histogram: mock_grade_histogram.return_value = [] module = render.get_module( self.user, diff --git a/lms/djangoapps/instructor/views/instructor_dashboard.py b/lms/djangoapps/instructor/views/instructor_dashboard.py index c91a8d3855..4399cc6dff 100644 --- a/lms/djangoapps/instructor/views/instructor_dashboard.py +++ b/lms/djangoapps/instructor/views/instructor_dashboard.py @@ -23,7 +23,7 @@ from util.json_request import JsonResponse from mock import patch from lms.djangoapps.lms_xblock.runtime import quote_slashes -from xmodule_modifiers import wrap_xblock +from openedx.core.lib.xblock_utils import wrap_xblock from xmodule.html_module import HtmlDescriptor from xmodule.modulestore.django import modulestore from xblock.field_data import DictFieldData diff --git a/lms/djangoapps/mobile_api/course_info/views.py b/lms/djangoapps/mobile_api/course_info/views.py index 87bd97ecc0..764a580fc6 100644 --- a/lms/djangoapps/mobile_api/course_info/views.py +++ b/lms/djangoapps/mobile_api/course_info/views.py @@ -7,7 +7,7 @@ from rest_framework.response import Response from courseware.courses import get_course_about_section, get_course_info_section_module from static_replace import make_static_urls_absolute, replace_static_urls -from xmodule_modifiers import get_course_update_items +from openedx.core.lib.xblock_utils import get_course_update_items from ..utils import mobile_view, mobile_course_access diff --git a/common/djangoapps/xmodule_modifiers.py b/openedx/core/lib/xblock_utils.py similarity index 86% rename from common/djangoapps/xmodule_modifiers.py rename to openedx/core/lib/xblock_utils.py index 1b08ce1960..98cddb8650 100644 --- a/common/djangoapps/xmodule_modifiers.py +++ b/openedx/core/lib/xblock_utils.py @@ -45,13 +45,24 @@ def request_token(request): This token will be the same for all calls to `request_token` made on the same request object. """ + # pylint: disable=protected-access if not hasattr(request, '_xblock_token'): request._xblock_token = uuid.uuid1().get_hex() return request._xblock_token -def wrap_xblock(runtime_class, block, view, frag, context, usage_id_serializer, request_token, display_name_only=False, extra_data=None): # pylint: disable=unused-argument +def wrap_xblock( + runtime_class, + block, + view, + frag, + context, # pylint: disable=unused-argument + usage_id_serializer, + request_token, # pylint: disable=redefined-outer-name + display_name_only=False, + extra_data=None +): """ Wraps the results of rendering an XBlock view in a standard
with identifying data so that the appropriate javascript module can be loaded onto it. @@ -181,13 +192,14 @@ def grade_histogram(module_id): from django.db import connection cursor = connection.cursor() - q = """SELECT courseware_studentmodule.grade, - COUNT(courseware_studentmodule.student_id) - FROM courseware_studentmodule - WHERE courseware_studentmodule.module_id=%s - GROUP BY courseware_studentmodule.grade""" + query = """\ + SELECT courseware_studentmodule.grade, + COUNT(courseware_studentmodule.student_id) + FROM courseware_studentmodule + WHERE courseware_studentmodule.module_id=%s + GROUP BY courseware_studentmodule.grade""" # Passing module_id this way prevents sql-injection. - cursor.execute(q, [module_id.to_deprecated_string()]) + cursor.execute(query, [module_id.to_deprecated_string()]) grades = list(cursor.fetchall()) grades.sort(key=lambda x: x[0]) # Add ORDER BY to sql query? @@ -218,7 +230,13 @@ def add_staff_markup(user, has_instructor_access, block, view, frag, context): edit_link = "//" + settings.CMS_BASE + '/container/' + unicode(block.location) # return edit link in rendered HTML for display - return wrap_fragment(frag, render_to_string("edit_unit_link.html", {'frag_content': frag.content, 'edit_link': edit_link})) + return wrap_fragment( + frag, + render_to_string( + "edit_unit_link.html", + {'frag_content': frag.content, 'edit_link': edit_link} + ) + ) else: return frag @@ -251,8 +269,9 @@ def add_staff_markup(user, has_instructor_access, block, view, frag, context): source_file = block.source_file # source used to generate the problem XML, eg latex or word - # useful to indicate to staff if problem has been released or not - # TODO (ichuang): use _has_access_descriptor.can_load in lms.courseware.access, instead of now>mstart comparison here + # Useful to indicate to staff if problem has been released or not. + # TODO (ichuang): use _has_access_descriptor.can_load in lms.courseware.access, + # instead of now>mstart comparison here. now = datetime.datetime.now(UTC()) is_released = "unknown" mstart = block.start @@ -268,24 +287,25 @@ def add_staff_markup(user, has_instructor_access, block, view, frag, context): log.warning("Unable to read field in Staff Debug information", exc_info=True) field_contents.append((name, "WARNING: Unable to read field")) - staff_context = {'fields': field_contents, - 'xml_attributes': getattr(block, 'xml_attributes', {}), - 'location': block.location, - 'xqa_key': block.xqa_key, - 'source_file': source_file, - 'source_url': '%s/%s/tree/master/%s' % (giturl, data_dir, source_file), - 'category': str(block.__class__.__name__), - # Template uses element_id in js function names, so can't allow dashes - 'element_id': block.location.html_id().replace('-', '_'), - 'edit_link': edit_link, - 'user': user, - 'xqa_server': settings.FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa'), - 'histogram': json.dumps(histogram), - 'render_histogram': render_histogram, - 'block_content': frag.content, - 'is_released': is_released, - 'has_instructor_access': has_instructor_access, - } + staff_context = { + 'fields': field_contents, + 'xml_attributes': getattr(block, 'xml_attributes', {}), + 'location': block.location, + 'xqa_key': block.xqa_key, + 'source_file': source_file, + 'source_url': '%s/%s/tree/master/%s' % (giturl, data_dir, source_file), + 'category': str(block.__class__.__name__), + # Template uses element_id in js function names, so can't allow dashes + 'element_id': block.location.html_id().replace('-', '_'), + 'edit_link': edit_link, + 'user': user, + 'xqa_server': settings.FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa'), + 'histogram': json.dumps(histogram), + 'render_histogram': render_histogram, + 'block_content': frag.content, + 'is_released': is_released, + 'has_instructor_access': has_instructor_access, + } return wrap_fragment(frag, render_to_string("staff_problem_info.html", staff_context)) @@ -323,7 +343,7 @@ def get_course_update_items(course_updates, provided_index=0): # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: course_html_parsed = html.fromstring(course_updates.data) - except (etree.XMLSyntaxError, etree.ParserError): + except (etree.XMLSyntaxError, etree.ParserError): # pylint: disable=no-member log.error("Cannot parse: " + course_updates.data) escaped = escape(course_updates.data) course_html_parsed = html.fromstring("
  1. " + escaped + "
")