Merge pull request #16710 from edx/jmbowman/PLAT-1419

PLAT-1419 Make edxmako a proper template backend
This commit is contained in:
Jeremy Bowman
2017-12-04 11:07:30 -05:00
committed by GitHub
20 changed files with 308 additions and 239 deletions

View File

@@ -113,7 +113,7 @@ def send_credit_notifications(username, course_key):
else:
email_body_content = ''
email_body = Template(email_body_content).render([context])
email_body = Template(email_body_content).render(context)
msg_alternative.attach(SafeMIMEText(email_body, _subtype='html', _charset='utf-8'))
# attach logo image

View File

@@ -4,9 +4,9 @@ These views will NOT be shown on production: trying to access them will result
in a 404 error.
"""
from django.http import HttpResponseNotFound
from django.template import TemplateDoesNotExist
from django.utils.translation import ugettext as _
from edxmako.shortcuts import render_to_response
from mako.exceptions import TopLevelLookupException
from openedx.core.djangoapps.util.user_messages import PageLevelMessages
@@ -51,5 +51,5 @@ def show_reference_template(request, template):
PageLevelMessages.register_error_message(request, _('This is a test error'))
return render_to_response(template, context)
except TopLevelLookupException:
except TemplateDoesNotExist:
return HttpResponseNotFound('Missing template {template}'.format(template=template))

View File

@@ -17,21 +17,23 @@ def derived(*settings):
Can be called multiple times to add more derived settings.
Args:
settings (list): List of setting names to register.
settings (str): Setting names to register.
"""
__DERIVED.extend(settings)
def derived_dict_entry(setting_dict, key):
def derived_collection_entry(collection_name, *accessors):
"""
Registers a setting which is a dictionary and needs a derived value for a particular key.
Registers a setting which is a dictionary or list and needs a derived value for a particular entry.
Can be called multiple times to add more derived settings.
Args:
setting_dict (str): Name of setting which contains a dictionary.
key (str): Name of key in the setting dictionary which will be derived.
collection_name (str): Name of setting which contains a dictionary or list.
accessors (int|str): Sequence of dictionary keys and list indices in the collection (and
collections within it) leading to the value which will be derived.
For example: 0, 'DIRS'.
"""
__DERIVED.append((setting_dict, key))
__DERIVED.append((collection_name, accessors))
def derive_settings(module_name):
@@ -52,13 +54,16 @@ def derive_settings(module_name):
elif isinstance(derived, tuple):
# If a tuple, two elements are expected - else ignore.
if len(derived) == 2:
# Both elements are expected to be strings.
# The first string is the attribute which is expected to be a dictionary.
# The second string is a key in that dictionary containing a derived setting.
setting = getattr(module, derived[0])[derived[1]]
# The first element is the name of the attribute which is expected to be a dictionary or list.
# The second element is a list of string keys in that dictionary leading to a derived setting.
collection = getattr(module, derived[0])
accessors = derived[1]
for accessor in accessors[:-1]:
collection = collection[accessor]
setting = collection[accessors[-1]]
if callable(setting):
setting_val = setting(module)
getattr(module, derived[0]).update({derived[1]: setting_val})
collection[accessors[-1]] = setting_val
def clear_for_tests():

View File

@@ -4,7 +4,7 @@ Tests for derived.py
import sys
from unittest import TestCase
from openedx.core.lib.derived import derived, derive_settings, clear_for_tests
from openedx.core.lib.derived import derived, derived_collection_entry, derive_settings, clear_for_tests
class TestDerivedSettings(TestCase):
@@ -22,7 +22,9 @@ class TestDerivedSettings(TestCase):
derived('DERIVED_VALUE', 'ANOTHER_DERIVED_VALUE')
self.module.DICT_VALUE = {}
self.module.DICT_VALUE['test_key'] = lambda settings: settings.DERIVED_VALUE * 3
derived(('DICT_VALUE', 'test_key'))
derived_collection_entry('DICT_VALUE', 'test_key')
self.module.DICT_VALUE['list_key'] = ['not derived', lambda settings: settings.DERIVED_VALUE]
derived_collection_entry('DICT_VALUE', 'list_key', 1)
def test_derived_settings_are_derived(self):
derive_settings(__name__)
@@ -42,3 +44,7 @@ class TestDerivedSettings(TestCase):
def test_derived_dict_settings(self):
derive_settings(__name__)
self.assertEqual(self.module.DICT_VALUE['test_key'], 'mutter paneermutter paneermutter paneer')
def test_derived_nested_settings(self):
derive_settings(__name__)
self.assertEqual(self.module.DICT_VALUE['list_key'][1], 'mutter paneer')