Commit b98d85fd by Ned Batchelder

Merge pull request #7943 from edx/ned/move-xmodule-modifiers

Move xmodule_modifiers.py to openedx/xblock
parents 25eb6942 05cd3316
...@@ -22,7 +22,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError ...@@ -22,7 +22,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from xmodule.html_module import CourseInfoModule 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 from cms.djangoapps.contentstore.push_notification import enqueue_push_course_update
# # This should be in a class which inherits from XmlDescriptor # # This should be in a class which inherits from XmlDescriptor
......
...@@ -11,7 +11,7 @@ import json ...@@ -11,7 +11,7 @@ import json
from collections import OrderedDict from collections import OrderedDict
from functools import partial from functools import partial
from static_replace import replace_static_urls 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 import dogstats_wrapper as dog_stats_api
from django.conf import settings from django.conf import settings
......
...@@ -9,7 +9,7 @@ from django.http import Http404, HttpResponseBadRequest ...@@ -9,7 +9,7 @@ from django.http import Http404, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from edxmako.shortcuts import render_to_string 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.x_module import PREVIEW_VIEWS, STUDENT_VIEW, AUTHOR_VIEW
from xmodule.contentstore.django import contentstore from xmodule.contentstore.django import contentstore
from xmodule.error_module import ErrorDescriptor from xmodule.error_module import ErrorDescriptor
......
...@@ -57,7 +57,7 @@ from opaque_keys.edx.locations import SlashSeparatedCourseKey ...@@ -57,7 +57,7 @@ from opaque_keys.edx.locations import SlashSeparatedCourseKey
from xmodule.contentstore.django import contentstore from xmodule.contentstore.django import contentstore
from xmodule.modulestore.django import modulestore, ModuleI18nService from xmodule.modulestore.django import modulestore, ModuleI18nService
from xmodule.modulestore.exceptions import ItemNotFoundError from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule_modifiers import ( from openedx.core.lib.xblock_utils import (
replace_course_urls, replace_course_urls,
replace_jump_to_id_urls, replace_jump_to_id_urls,
replace_static_urls, replace_static_urls,
......
...@@ -1000,7 +1000,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase): ...@@ -1000,7 +1000,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase):
self.user, self.user,
self.descriptor 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 = [] mock_grade_histogram.return_value = []
module = render.get_module( module = render.get_module(
self.user, self.user,
...@@ -1022,7 +1022,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase): ...@@ -1022,7 +1022,7 @@ class TestStaffDebugInfo(ModuleStoreTestCase):
max_grade=1, max_grade=1,
state="{}", 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 = [] mock_grade_histogram.return_value = []
module = render.get_module( module = render.get_module(
self.user, self.user,
......
...@@ -23,7 +23,7 @@ from util.json_request import JsonResponse ...@@ -23,7 +23,7 @@ from util.json_request import JsonResponse
from mock import patch from mock import patch
from lms.djangoapps.lms_xblock.runtime import quote_slashes 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.html_module import HtmlDescriptor
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
from xblock.field_data import DictFieldData from xblock.field_data import DictFieldData
......
...@@ -7,7 +7,7 @@ from rest_framework.response import Response ...@@ -7,7 +7,7 @@ from rest_framework.response import Response
from courseware.courses import get_course_about_section, get_course_info_section_module 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 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 from ..utils import mobile_view, mobile_course_access
......
...@@ -45,13 +45,24 @@ def request_token(request): ...@@ -45,13 +45,24 @@ def request_token(request):
This token will be the same for all calls to `request_token` This token will be the same for all calls to `request_token`
made on the same request object. made on the same request object.
""" """
# pylint: disable=protected-access
if not hasattr(request, '_xblock_token'): if not hasattr(request, '_xblock_token'):
request._xblock_token = uuid.uuid1().get_hex() request._xblock_token = uuid.uuid1().get_hex()
return request._xblock_token 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 <section> with identifying Wraps the results of rendering an XBlock view in a standard <section> with identifying
data so that the appropriate javascript module can be loaded onto it. data so that the appropriate javascript module can be loaded onto it.
...@@ -181,13 +192,14 @@ def grade_histogram(module_id): ...@@ -181,13 +192,14 @@ def grade_histogram(module_id):
from django.db import connection from django.db import connection
cursor = connection.cursor() cursor = connection.cursor()
q = """SELECT courseware_studentmodule.grade, query = """\
COUNT(courseware_studentmodule.student_id) SELECT courseware_studentmodule.grade,
FROM courseware_studentmodule COUNT(courseware_studentmodule.student_id)
WHERE courseware_studentmodule.module_id=%s FROM courseware_studentmodule
GROUP BY courseware_studentmodule.grade""" WHERE courseware_studentmodule.module_id=%s
GROUP BY courseware_studentmodule.grade"""
# Passing module_id this way prevents sql-injection. # 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 = list(cursor.fetchall())
grades.sort(key=lambda x: x[0]) # Add ORDER BY to sql query? 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): ...@@ -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) edit_link = "//" + settings.CMS_BASE + '/container/' + unicode(block.location)
# return edit link in rendered HTML for display # 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: else:
return frag return frag
...@@ -251,8 +269,9 @@ def add_staff_markup(user, has_instructor_access, block, view, frag, context): ...@@ -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 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 # 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 # TODO (ichuang): use _has_access_descriptor.can_load in lms.courseware.access,
# instead of now>mstart comparison here.
now = datetime.datetime.now(UTC()) now = datetime.datetime.now(UTC())
is_released = "unknown" is_released = "unknown"
mstart = block.start mstart = block.start
...@@ -268,24 +287,25 @@ def add_staff_markup(user, has_instructor_access, block, view, frag, context): ...@@ -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) log.warning("Unable to read field in Staff Debug information", exc_info=True)
field_contents.append((name, "WARNING: Unable to read field")) field_contents.append((name, "WARNING: Unable to read field"))
staff_context = {'fields': field_contents, staff_context = {
'xml_attributes': getattr(block, 'xml_attributes', {}), 'fields': field_contents,
'location': block.location, 'xml_attributes': getattr(block, 'xml_attributes', {}),
'xqa_key': block.xqa_key, 'location': block.location,
'source_file': source_file, 'xqa_key': block.xqa_key,
'source_url': '%s/%s/tree/master/%s' % (giturl, data_dir, source_file), 'source_file': source_file,
'category': str(block.__class__.__name__), 'source_url': '%s/%s/tree/master/%s' % (giturl, data_dir, source_file),
# Template uses element_id in js function names, so can't allow dashes 'category': str(block.__class__.__name__),
'element_id': block.location.html_id().replace('-', '_'), # Template uses element_id in js function names, so can't allow dashes
'edit_link': edit_link, 'element_id': block.location.html_id().replace('-', '_'),
'user': user, 'edit_link': edit_link,
'xqa_server': settings.FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa'), 'user': user,
'histogram': json.dumps(histogram), 'xqa_server': settings.FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa'),
'render_histogram': render_histogram, 'histogram': json.dumps(histogram),
'block_content': frag.content, 'render_histogram': render_histogram,
'is_released': is_released, 'block_content': frag.content,
'has_instructor_access': has_instructor_access, 'is_released': is_released,
} 'has_instructor_access': has_instructor_access,
}
return wrap_fragment(frag, render_to_string("staff_problem_info.html", staff_context)) 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): ...@@ -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. # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break.
try: try:
course_html_parsed = html.fromstring(course_updates.data) 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) log.error("Cannot parse: " + course_updates.data)
escaped = escape(course_updates.data) escaped = escape(course_updates.data)
course_html_parsed = html.fromstring("<ol><li>" + escaped + "</li></ol>") course_html_parsed = html.fromstring("<ol><li>" + escaped + "</li></ol>")
......
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