From f27f02607e77301e21ddda7bed9fa74467b77256 Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Mon, 19 Nov 2018 00:34:46 +0200 Subject: [PATCH] Fix the FTR XBlock for Hawthorn --- freetextresponse/freetextresponse.py | 20 +++++++++++--------- freetextresponse/tests.py | 18 ++++++++++++++---- package.json | 2 +- requirements.txt | 2 +- setup.py | 3 ++- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/freetextresponse/freetextresponse.py b/freetextresponse/freetextresponse.py index c1f60fa1..497bebbc 100644 --- a/freetextresponse/freetextresponse.py +++ b/freetextresponse/freetextresponse.py @@ -4,7 +4,6 @@ from enum import Enum from django.db import IntegrityError from django.template.context import Context -from django.template.loader import get_template from django.utils.translation import ungettext from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext @@ -17,6 +16,7 @@ from xblock.fields import String from xblock.fragment import Fragment from xblock.validation import ValidationMessage +from xblockutils.resources import ResourceLoader from xblockutils.studio_editable import StudioEditableXBlockMixin from .mixins import EnforceDueDates, MissingDataFetcherMixin @@ -35,6 +35,9 @@ class FreeTextResponse( """ Enables instructors to create questions with free-text responses. """ + + loader = ResourceLoader(__name__) + @staticmethod def workbench_scenarios(): """ @@ -240,8 +243,7 @@ def workbench_scenarios(): def build_fragment( self, - template, - context_dict, + rendered_template, initialize_js_func, additional_css=[], additional_js=[], @@ -250,8 +252,7 @@ def build_fragment( """ Creates a fragment for display. """ - context = Context(context_dict) - fragment = Fragment(template.render(context)) + fragment = Fragment(rendered_template) for item in additional_css: url = self.runtime.local_resource_url(self, item) fragment.add_css_url(url) @@ -278,9 +279,7 @@ def student_view(self, context={}): (Fragment): The HTML Fragment for this XBlock, which determines the general frame of the FreeTextResponse Question. """ - display_other_responses = self.display_other_student_responses - self.runtime.service(self, 'i18n') context.update( { @@ -298,10 +297,13 @@ def student_view(self, context={}): 'other_responses': self.get_other_answers(), } ) - template = get_template('freetextresponse_view.html') + template = self.loader.render_django_template( + 'templates/freetextresponse_view.html', + context=Context(context), + i18n_service=self.runtime.service(self, 'i18n'), + ) fragment = self.build_fragment( template, - context, initialize_js_func='FreeTextResponseView', additional_css=[ 'public/view.css', diff --git a/freetextresponse/tests.py b/freetextresponse/tests.py index 31292b7f..647c78e0 100644 --- a/freetextresponse/tests.py +++ b/freetextresponse/tests.py @@ -11,9 +11,10 @@ from xblock.field_data import DictFieldData from xblock.validation import ValidationMessage +from xblockutils.resources import ResourceLoader from django.db import IntegrityError -from django.template.loader import get_template +from django.template.context import Context from .freetextresponse import Credit from .freetextresponse import FreeTextResponse @@ -56,7 +57,13 @@ def make_an_xblock(cls, **kw): Helper method that creates a Free-text Response XBlock """ course_id = SlashSeparatedCourseKey('foo', 'bar', 'baz') - runtime = Mock(course_id=course_id) + runtime = Mock( + course_id=course_id, + service=Mock( + # Is there a cleaner mock to the `i18n` service? + return_value=Mock(_catalog={}), + ), + ) scope_ids = Mock() field_data = DictFieldData(kw) xblock = FreeTextResponse(runtime, field_data, scope_ids) @@ -182,10 +189,13 @@ def test_build_fragment_prompt_html(self): context = { 'prompt': studio_settings_prompt, } - template = get_template('freetextresponse_view.html') + loader = ResourceLoader('freetextresponse') + template = loader.render_django_template( + 'templates/freetextresponse_view.html', + context=Context(context), + ) fragment = self.xblock.build_fragment( template, - context, initialize_js_func='FreeTextResponseView', additional_css=[], additional_js=[], diff --git a/package.json b/package.json index 65d045c7..34336be7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "xblock-free-text-response", "title": "FreeTextResponse XBlock", "description": "Enables instructors to create questions with free-text responses.", - "version": "0.3.1", + "version": "0.4.0", "homepage": "https://github.com/Stanford-Online/xblock-free-text-response", "author": { "name": "Azim Pradhan", diff --git a/requirements.txt b/requirements.txt index d1531567..7526e392 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ ddt django<1.9,>=1.8 git+https://github.com/edx/XBlock.git#egg=XBlock -git+https://github.com/edx/xblock-utils.git@v1.0.0#egg=xblock-utils==v1.0.0 +git+https://github.com/edx/xblock-utils.git@v1.1.1#egg=xblock-utils==v1.1.1 git+https://github.com/edx/opaque-keys.git@27dc382ea587483b1e3889a3d19cbd90b9023a06#egg=opaque-keys -e . diff --git a/setup.py b/setup.py index 671a1a1d..0691c90d 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ def run_tests(self): setup( name="xblock-free-text-response", - version="0.3.1", + version="0.4.0", description="Enables instructors to create questions with free-text responses.", license='AGPL-3.0', packages=[ @@ -57,6 +57,7 @@ def run_tests(self): package_data={ "freetextresponse": [ 'public/*', + 'templates/*', ], }, classifiers=[