Commit e5413899 by Tim Krones

Remove local implementations of "load_resource" and "render_template";

use xblock-utils equivalents instead.

- load_resource ⟶ ResourceLoader.load_unicode
- render_template ⟶ ResourceLoader.render_template
parent 469662c9
...@@ -11,11 +11,18 @@ import urllib ...@@ -11,11 +11,18 @@ import urllib
from xblock.core import XBlock from xblock.core import XBlock
from xblock.fields import Scope, String, Dict, Float, Boolean from xblock.fields import Scope, String, Dict, Float, Boolean
from xblock.fragment import Fragment from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
from xblockutils.settings import XBlockWithSettingsMixin, ThemableXBlockMixin
from .utils import _, render_template, load_resource # pylint: disable=unused-import from .utils import _ # pylint: disable=unused-import
from .default_data import DEFAULT_DATA from .default_data import DEFAULT_DATA
# Globals ###########################################################
loader = ResourceLoader(__name__)
# Classes ########################################################### # Classes ###########################################################
class DragAndDropBlock(XBlock): class DragAndDropBlock(XBlock):
...@@ -108,7 +115,7 @@ class DragAndDropBlock(XBlock): ...@@ -108,7 +115,7 @@ class DragAndDropBlock(XBlock):
""" """
fragment = Fragment() fragment = Fragment()
fragment.add_content(render_template('/templates/html/drag_and_drop.html')) fragment.add_content(loader.render_template('/templates/html/drag_and_drop.html'))
css_urls = ( css_urls = (
'public/css/vendor/jquery-ui-1.10.4.custom.min.css', 'public/css/vendor/jquery-ui-1.10.4.custom.min.css',
'public/css/drag_and_drop.css' 'public/css/drag_and_drop.css'
...@@ -166,7 +173,7 @@ class DragAndDropBlock(XBlock): ...@@ -166,7 +173,7 @@ class DragAndDropBlock(XBlock):
Editing view in Studio Editing view in Studio
""" """
js_templates = load_resource('/templates/html/js_templates.html') js_templates = loader.load_unicode('/templates/html/js_templates.html')
help_texts = { help_texts = {
field_name: self._(field.help) field_name: self._(field.help)
for field_name, field in self.fields.viewitems() if hasattr(field, "help") for field_name, field in self.fields.viewitems() if hasattr(field, "help")
...@@ -179,7 +186,7 @@ class DragAndDropBlock(XBlock): ...@@ -179,7 +186,7 @@ class DragAndDropBlock(XBlock):
} }
fragment = Fragment() fragment = Fragment()
fragment.add_content(render_template('/templates/html/drag_and_drop_edit.html', context)) fragment.add_content(loader.render_template('/templates/html/drag_and_drop_edit.html', context))
css_urls = ( css_urls = (
'public/css/vendor/jquery-ui-1.10.4.custom.min.css', 'public/css/vendor/jquery-ui-1.10.4.custom.min.css',
......
...@@ -123,7 +123,7 @@ ...@@ -123,7 +123,7 @@
</div> </div>
<div class="row"> <div class="row">
<label for="item-{{id}}-numerical-margin"> <label for="item-{{id}}-numerical-margin">
{{i18n "Margin ± (when a numerical value is required, values entered by students must not differ from the expected value by more than this margin; default is zero)"}} {{i18n "Margin +/- (when a numerical value is required, values entered by students must not differ from the expected value by more than this margin; default is zero)"}}
</label> </label>
<input type="number" <input type="number"
step="0.1" step="0.1"
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Imports ###########################################################
import pkg_resources
from django.template import Context, Template
# Functions #########################################################
# Make '_' a no-op so we can scrape strings # Make '_' a no-op so we can scrape strings
def _(text): def _(text):
return text return text
def load_resource(resource_path):
"""
Gets the content of a resource
"""
resource_content = pkg_resources.resource_string(__name__, resource_path)
return resource_content
def render_template(template_path, context=None):
"""
Evaluate a template by resource path, applying the provided context
"""
if context is None:
context = {}
template_str = load_resource(template_path)
template = Template(template_str)
return template.render(Context(context))
# Imports ########################################################### # Imports ###########################################################
from xml.sax.saxutils import escape from xml.sax.saxutils import escape
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from ..utils import load_resource
from workbench import scenarios from workbench import scenarios
from xblockutils.resources import ResourceLoader
from xblockutils.base_test import SeleniumBaseTest from xblockutils.base_test import SeleniumBaseTest
# Globals ###########################################################
loader = ResourceLoader(__name__)
# Classes ########################################################### # Classes ###########################################################
...@@ -41,7 +48,7 @@ class BaseIntegrationTest(SeleniumBaseTest): ...@@ -41,7 +48,7 @@ class BaseIntegrationTest(SeleniumBaseTest):
) )
def _get_custom_scenario_xml(self, filename): def _get_custom_scenario_xml(self, filename):
data = load_resource(filename) data = loader.load_unicode(filename)
return "<vertical_demo><drag-and-drop-v2 data='{data}'/></vertical_demo>".format( return "<vertical_demo><drag-and-drop-v2 data='{data}'/></vertical_demo>".format(
data=escape(data, self._additional_escapes) data=escape(data, self._additional_escapes)
) )
......
...@@ -8,7 +8,7 @@ class TestCustomDataDragAndDropRendering(BaseIntegrationTest): ...@@ -8,7 +8,7 @@ class TestCustomDataDragAndDropRendering(BaseIntegrationTest):
def setUp(self): def setUp(self):
super(TestCustomDataDragAndDropRendering, self).setUp() super(TestCustomDataDragAndDropRendering, self).setUp()
scenario_xml = self._get_custom_scenario_xml("integration/data/test_html_data.json") scenario_xml = self._get_custom_scenario_xml("data/test_html_data.json")
self._add_scenario(self.PAGE_ID, self.PAGE_TITLE, scenario_xml) self._add_scenario(self.PAGE_ID, self.PAGE_TITLE, scenario_xml)
self._page = self.go_to_page(self.PAGE_TITLE) self._page = self.go_to_page(self.PAGE_TITLE)
......
# Imports ###########################################################
from ddt import ddt, data from ddt import ddt, data
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from xblockutils.resources import ResourceLoader
from drag_and_drop_v2.default_data import ( from drag_and_drop_v2.default_data import (
TOP_ZONE_TITLE, MIDDLE_ZONE_TITLE, BOTTOM_ZONE_TITLE, TOP_ZONE_TITLE, MIDDLE_ZONE_TITLE, BOTTOM_ZONE_TITLE,
ITEM_CORRECT_FEEDBACK, ITEM_INCORRECT_FEEDBACK, ITEM_NO_ZONE_FEEDBACK, ITEM_CORRECT_FEEDBACK, ITEM_INCORRECT_FEEDBACK, ITEM_NO_ZONE_FEEDBACK,
START_FEEDBACK, FINISH_FEEDBACK START_FEEDBACK, FINISH_FEEDBACK
) )
from .test_base import BaseIntegrationTest from .test_base import BaseIntegrationTest
from ..utils import load_resource
# Globals ###########################################################
loader = ResourceLoader(__name__)
ZONES_MAP = { ZONES_MAP = {
0: TOP_ZONE_TITLE, 0: TOP_ZONE_TITLE,
1: MIDDLE_ZONE_TITLE, 1: MIDDLE_ZONE_TITLE,
...@@ -20,6 +27,8 @@ ZONES_MAP = { ...@@ -20,6 +27,8 @@ ZONES_MAP = {
} }
# Classes ###########################################################
class ItemDefinition(object): class ItemDefinition(object):
def __init__(self, item_id, zone_id, feedback_positive, feedback_negative, input_value=None): def __init__(self, item_id, zone_id, feedback_positive, feedback_negative, input_value=None):
self.feedback_negative = feedback_negative self.feedback_negative = feedback_negative
...@@ -377,7 +386,7 @@ class CustomDataInteractionTest(BasicInteractionTest, BaseIntegrationTest): ...@@ -377,7 +386,7 @@ class CustomDataInteractionTest(BasicInteractionTest, BaseIntegrationTest):
} }
def _get_scenario_xml(self): def _get_scenario_xml(self):
return self._get_custom_scenario_xml("integration/data/test_data.json") return self._get_custom_scenario_xml("data/test_data.json")
class CustomHtmlDataInteractionTest(BasicInteractionTest, BaseIntegrationTest): class CustomHtmlDataInteractionTest(BasicInteractionTest, BaseIntegrationTest):
...@@ -395,15 +404,15 @@ class CustomHtmlDataInteractionTest(BasicInteractionTest, BaseIntegrationTest): ...@@ -395,15 +404,15 @@ class CustomHtmlDataInteractionTest(BasicInteractionTest, BaseIntegrationTest):
} }
def _get_scenario_xml(self): def _get_scenario_xml(self):
return self._get_custom_scenario_xml("integration/data/test_html_data.json") return self._get_custom_scenario_xml("data/test_html_data.json")
class MultipleBlocksDataInteraction(InteractionTestBase, BaseIntegrationTest): class MultipleBlocksDataInteraction(InteractionTestBase, BaseIntegrationTest):
PAGE_TITLE = 'Drag and Drop v2 Multiple Blocks' PAGE_TITLE = 'Drag and Drop v2 Multiple Blocks'
PAGE_ID = 'drag_and_drop_v2_multi' PAGE_ID = 'drag_and_drop_v2_multi'
BLOCK1_DATA_FILE = "integration/data/test_data.json" BLOCK1_DATA_FILE = "data/test_data.json"
BLOCK2_DATA_FILE = "integration/data/test_data_other.json" BLOCK2_DATA_FILE = "data/test_data_other.json"
item_maps = { item_maps = {
'block1': { 'block1': {
...@@ -430,7 +439,7 @@ class MultipleBlocksDataInteraction(InteractionTestBase, BaseIntegrationTest): ...@@ -430,7 +439,7 @@ class MultipleBlocksDataInteraction(InteractionTestBase, BaseIntegrationTest):
def _get_scenario_xml(self): def _get_scenario_xml(self):
blocks_xml = "\n".join([ blocks_xml = "\n".join([
"<drag-and-drop-v2 data='{data}'/>".format(data=load_resource(filename)) "<drag-and-drop-v2 data='{data}'/>".format(data=loader.load_unicode(filename))
for filename in (self.BLOCK1_DATA_FILE, self.BLOCK2_DATA_FILE) for filename in (self.BLOCK1_DATA_FILE, self.BLOCK2_DATA_FILE)
]) ])
......
# Imports ###########################################################
from ddt import ddt, unpack, data from ddt import ddt, unpack, data
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from xblockutils.resources import ResourceLoader
from drag_and_drop_v2.default_data import START_FEEDBACK from drag_and_drop_v2.default_data import START_FEEDBACK
from ..utils import load_resource
from .test_base import BaseIntegrationTest from .test_base import BaseIntegrationTest
# Globals ###########################################################
loader = ResourceLoader(__name__)
# Classes ###########################################################
class Colors(object): class Colors(object):
WHITE = 'rgb(255, 255, 255)' WHITE = 'rgb(255, 255, 255)'
BLUE = 'rgb(29, 82, 128)' BLUE = 'rgb(29, 82, 128)'
...@@ -36,7 +46,7 @@ class TestDragAndDropRender(BaseIntegrationTest): ...@@ -36,7 +46,7 @@ class TestDragAndDropRender(BaseIntegrationTest):
SIDES = ['Top', 'Bottom', 'Left', 'Right'] SIDES = ['Top', 'Bottom', 'Left', 'Right']
def load_scenario(self, item_background_color="", item_text_color="", zone_labels=False, zone_borders=False): def load_scenario(self, item_background_color="", item_text_color="", zone_labels=False, zone_borders=False):
exercise_data = load_resource("integration/data/test_data_a11y.json") exercise_data = loader.load_unicode("data/test_data_a11y.json")
exercise_data = exercise_data.replace('{display_labels_value}', 'true' if zone_labels else 'false') exercise_data = exercise_data.replace('{display_labels_value}', 'true' if zone_labels else 'false')
exercise_data = exercise_data.replace('{display_borders_value}', 'true' if zone_borders else 'false') exercise_data = exercise_data.replace('{display_borders_value}', 'true' if zone_borders else 'false')
scenario_xml = """ scenario_xml = """
......
# Imports ###########################################################
import json import json
import unittest import unittest
from ..utils import ( from xblockutils.resources import ResourceLoader
make_block,
load_resource, from ..utils import make_block, TestCaseMixin
TestCaseMixin,
)
# Globals ###########################################################
loader = ResourceLoader(__name__)
# Classes ###########################################################
class BaseDragAndDropAjaxFixture(TestCaseMixin): class BaseDragAndDropAjaxFixture(TestCaseMixin):
ZONE_1 = None ZONE_1 = None
...@@ -32,15 +39,15 @@ class BaseDragAndDropAjaxFixture(TestCaseMixin): ...@@ -32,15 +39,15 @@ class BaseDragAndDropAjaxFixture(TestCaseMixin):
@classmethod @classmethod
def initial_data(cls): def initial_data(cls):
return json.loads(load_resource('unit/data/{}/data.json'.format(cls.FOLDER))) return json.loads(loader.load_unicode('data/{}/data.json'.format(cls.FOLDER)))
@classmethod @classmethod
def initial_settings(cls): def initial_settings(cls):
return json.loads(load_resource('unit/data/{}/settings.json'.format(cls.FOLDER))) return json.loads(loader.load_unicode('data/{}/settings.json'.format(cls.FOLDER)))
@classmethod @classmethod
def expected_configuration(cls): def expected_configuration(cls):
return json.loads(load_resource('unit/data/{}/config_out.json'.format(cls.FOLDER))) return json.loads(loader.load_unicode('data/{}/config_out.json'.format(cls.FOLDER)))
@classmethod @classmethod
def initial_feedback(cls): def initial_feedback(cls):
......
import json import json
import pkg_resources
import re import re
from mock import patch from mock import patch
...@@ -32,14 +31,6 @@ def make_block(): ...@@ -32,14 +31,6 @@ def make_block():
return drag_and_drop_v2.DragAndDropBlock(runtime, field_data, scope_ids=scope_ids) return drag_and_drop_v2.DragAndDropBlock(runtime, field_data, scope_ids=scope_ids)
def load_resource(resource_path):
"""
Gets the content of a resource
"""
resource_content = pkg_resources.resource_string(__name__, resource_path)
return unicode(resource_content)
class TestCaseMixin(object): class TestCaseMixin(object):
""" Helpful mixins for unittest TestCase subclasses """ """ Helpful mixins for unittest TestCase subclasses """
maxDiff = None maxDiff = None
......
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