-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[BD-04] Convert PollModule to PollBlock and CustomTagModule to CustomTagBlock #26873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,32 @@ | |
| from string import Template | ||
|
|
||
| from lxml import etree | ||
| from xmodule.raw_module import RawDescriptor | ||
| from xmodule.x_module import XModule # lint-amnesty, pylint: disable=unused-import | ||
|
|
||
|
|
||
| class CustomTagModule(XModule): | ||
| from pkg_resources import resource_string | ||
| from web_fragments.fragment import Fragment | ||
| from xmodule.editing_module import EditingMixin | ||
| from xmodule.raw_module import RawMixin | ||
| from xmodule.util.xmodule_django import add_webpack_to_fragment | ||
| from xmodule.x_module import ( | ||
| HTMLSnippet, | ||
| ResourceTemplates, | ||
| shim_xmodule_js, | ||
| XModuleMixin, | ||
| XModuleDescriptorToXBlockMixin, | ||
| XModuleToXBlockMixin, | ||
| ) | ||
| from xmodule.xml_module import XmlMixin | ||
|
|
||
|
|
||
| class CustomTagBlock( | ||
| RawMixin, | ||
| XmlMixin, | ||
| EditingMixin, | ||
| XModuleDescriptorToXBlockMixin, | ||
| XModuleToXBlockMixin, | ||
| HTMLSnippet, | ||
| ResourceTemplates, | ||
| XModuleMixin, | ||
| ): # pylint: disable=abstract-method | ||
| """ | ||
| This module supports tags of the form | ||
| <customtag option="val" option2="val2" impl="tagname"/> | ||
|
|
@@ -31,17 +52,35 @@ class CustomTagModule(XModule): | |
| Renders to:: | ||
| More information given in <a href="/book/234">the text</a> | ||
| """ | ||
|
|
||
| def get_html(self): | ||
| return self.descriptor.rendered_html | ||
|
|
||
|
|
||
| class CustomTagDescriptor(RawDescriptor): | ||
| """ Descriptor for custom tags. Loads the template when created.""" | ||
| module_class = CustomTagModule | ||
| resources_dir = None | ||
| template_dir_name = 'customtag' | ||
|
|
||
| preview_view_js = { | ||
| 'js': [], | ||
| 'xmodule_js': resource_string(__name__, 'js/src/xmodule.js'), | ||
| } | ||
| preview_view_css = { | ||
| 'scss': [], | ||
| } | ||
| studio_view_js = { | ||
| 'js': [resource_string(__name__, 'js/src/raw/edit/xml.js')], | ||
| 'xmodule_js': resource_string(__name__, 'js/src/xmodule.js'), | ||
| } | ||
| studio_view_css = { | ||
| 'scss': [resource_string(__name__, 'css/codemirror/codemirror.scss')], | ||
| } | ||
|
||
|
|
||
| def studio_view(self, _context): | ||
| """ | ||
| Return the studio view. | ||
| """ | ||
| fragment = Fragment( | ||
| self.system.render_template(self.mako_template, self.get_context()) | ||
| ) | ||
| add_webpack_to_fragment(fragment, 'CustomTagBlockStudio') | ||
| shim_xmodule_js(fragment, 'XMLEditingDescriptor') | ||
| return fragment | ||
|
|
||
| def render_template(self, system, xml_data): | ||
| '''Render the template, given the definition xml_data''' | ||
| xmltree = etree.fromstring(xml_data) | ||
|
|
@@ -71,6 +110,14 @@ def render_template(self, system, xml_data): | |
| def rendered_html(self): | ||
| return self.render_template(self.system, self.data) | ||
|
|
||
| def student_view(self, _context): | ||
| """ | ||
| Renders the student view. | ||
| """ | ||
| fragment = Fragment() | ||
| fragment.add_content(self.rendered_html) | ||
| return fragment | ||
|
|
||
| def export_to_file(self): | ||
| """ | ||
| Custom tags are special: since they're already pointers, we don't want | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,29 +191,6 @@ def test_load_class(self): | |
| assert str(vc) == vc_str | ||
|
|
||
|
|
||
| class LogicTest(unittest.TestCase): | ||
|
||
| """Base class for testing xmodule logic.""" | ||
| descriptor_class = None | ||
| raw_field_data = {} | ||
|
|
||
| def setUp(self): | ||
| super(LogicTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments | ||
| self.system = get_test_system() | ||
| self.descriptor = Mock(name="descriptor", url_name='', category='test') | ||
|
|
||
| self.xmodule_class = self.descriptor_class.module_class | ||
| usage_key = self.system.course_id.make_usage_key(self.descriptor.category, 'test_loc') | ||
| # ScopeIds has 4 fields: user_id, block_type, def_id, usage_id | ||
| scope_ids = ScopeIds(1, self.descriptor.category, usage_key, usage_key) | ||
| self.xmodule = self.xmodule_class( | ||
| self.descriptor, self.system, DictFieldData(self.raw_field_data), scope_ids | ||
| ) | ||
|
|
||
| def ajax_request(self, dispatch, data): | ||
| """Call Xmodule.handle_ajax.""" | ||
| return json.loads(self.xmodule.handle_ajax(dispatch, data)) | ||
|
|
||
|
|
||
| def map_references(value, field, actual_course_key): | ||
| """ | ||
| Map the references in value to actual_course_key and return value | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,42 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """Test for Poll Xmodule functional logic.""" | ||
|
|
||
| import json | ||
| import unittest | ||
|
|
||
| from mock import Mock | ||
|
|
||
| from xmodule.poll_module import PollDescriptor | ||
| from xblock.field_data import DictFieldData | ||
| from xblock.fields import ScopeIds | ||
| from xmodule.poll_module import PollBlock | ||
|
|
||
| from . import LogicTest | ||
| from . import get_test_system | ||
| from .test_import import DummySystem | ||
|
|
||
|
|
||
| class PollModuleTest(LogicTest): | ||
| class PollBlockTest(unittest.TestCase): | ||
| """Logic tests for Poll Xmodule.""" | ||
| descriptor_class = PollDescriptor | ||
|
|
||
| raw_field_data = { | ||
| 'poll_answers': {'Yes': 1, 'Dont_know': 0, 'No': 0}, | ||
| 'voted': False, | ||
| 'poll_answer': '' | ||
| } | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.system = get_test_system() | ||
| usage_key = self.system.course_id.make_usage_key(PollBlock.category, 'test_loc') | ||
| # ScopeIds has 4 fields: user_id, block_type, def_id, usage_id | ||
| scope_ids = ScopeIds(1, PollBlock.category, usage_key, usage_key) | ||
| self.xmodule = PollBlock( | ||
| self.system, DictFieldData(self.raw_field_data), scope_ids | ||
| ) | ||
|
|
||
| def ajax_request(self, dispatch, data): | ||
| """Call Xmodule.handle_ajax.""" | ||
| return json.loads(self.xmodule.handle_ajax(dispatch, data)) | ||
|
|
||
|
||
| def test_bad_ajax_request(self): | ||
| # Make sure that answer for incorrect request is error json. | ||
| response = self.ajax_request('bad_answer', {}) | ||
|
|
@@ -52,7 +70,7 @@ def test_poll_export_with_unescaped_characters_xml(self): | |
| </poll_question> | ||
| ''' | ||
|
|
||
| output = PollDescriptor.from_xml(sample_poll_xml, module_system, id_generator) | ||
| output = PollBlock.from_xml(sample_poll_xml, module_system, id_generator) | ||
| # Update the answer with invalid character. | ||
| invalid_characters_poll_answer = output.answers[0] | ||
| # Invalid less-than character. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be possible to fix this but I am assuming it is better to leave deprecated code broken.