diff --git a/app/settings.py b/app/settings.py index b30dd13ede..2776f89eff 100644 --- a/app/settings.py +++ b/app/settings.py @@ -47,6 +47,8 @@ # Application definition INSTALLED_APPS = [ + 'django.contrib.auth', + 'django.contrib.contenttypes', 'django.contrib.staticfiles', ] @@ -84,7 +86,7 @@ TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': ['resources'], 'OPTIONS': { 'context_processors': [ 'django.contrib.auth.context_processors.auth', @@ -95,9 +97,6 @@ 'django.template.context_processors.tz', 'django.contrib.messages.context_processors.messages', ], - 'loaders': [ - 'template_loader.TemplateLoader', - ] }, }, ] diff --git a/app/template_loader.py b/app/template_loader.py deleted file mode 100644 index f49277fe88..0000000000 --- a/app/template_loader.py +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright 2019 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import django.template -import django.template.loaders.base -import django.utils.translation - - -class TemplateLoader(django.template.loaders.base.Loader): - """Our custom template loader, which loads templates from Resources.""" - - def get_template(self, name, template_dirs=None, skip=None): - import resources - lang = django.utils.translation.get_language() # currently active lang - resource = resources.get_localized(name, lang) - template = resource and resource.get_template() - if template: - return template - else: - raise django.template.TemplateDoesNotExist(name) - - def get_contents(self, origin): - # Defining this method is necessary so that Django recognizes that - # this loader is in the new format (using get_template() instead of - # load_template()). But this method is actually not called when - # get_template() is overridden. - raise Exception('Not expected to be called') diff --git a/app/views/base.py b/app/views/base.py index 1a851bf194..1ce74bd357 100644 --- a/app/views/base.py +++ b/app/views/base.py @@ -17,13 +17,13 @@ import re import django.http +import django.shortcuts import django.utils.decorators import django.views import six.moves.urllib.parse as urlparse import config import const -import resources import site_settings import user_agents import utils @@ -349,23 +349,18 @@ def render(self, template_name, status_code=200, **template_vars): Returns: HttpResponse: An HttpResponse with the rendered template. """ - - def get_vars(): - """A function returning vars, for use by the resources module.""" - template_vars['env'] = self.env + template_name = '%s.template' % template_name + context = { + 'env': self.env, # TODO(nworden): change templates to access config through env, # which already has the config anyway - template_vars['config'] = self.env.config - template_vars['params'] = self.params - template_vars['csp_nonce'] = self.request.csp_nonce - return template_vars - - query_str = self.request.META.get('QUERY_STRING', '') - extra_key = (self.env.repo, self.env.charset, query_str) - return django.http.HttpResponse( - resources.get_rendered(template_name, self.env.lang, extra_key, - get_vars, 0), - status=status_code) + 'config': self.env.config, + 'params': self.params, + 'csp_nonce': self.request.csp_nonce, + } + context.update(template_vars) + return django.shortcuts.render( + self.request, template_name, context, status=status_code) def error(self, status_code, message=''): """Returns an error response. diff --git a/tests/test_resources.py b/tests/test_resources.py index 2d7522b27d..ee1fa4819e 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -66,16 +66,12 @@ def setUp(self): resources.set_active_bundle_name('1') self.temp_entity_keys = [] - self.put_resource('1', 'base.html.template', 50, + self.put_resource('1', 'test-base.html.template', 50, 'hi! {% block foo %}{% endblock foo %}') - self.put_resource('1', 'base.html.template:es', 40, + self.put_resource('1', 'test-base.html.template:es', 40, '\xc2\xa1hola! {% block foo %}{% endblock foo %}') - self.put_resource('1', 'page.html.template', 30, - '{% extends "base.html.template" %} ' - '{% block foo %}default{% endblock foo %}') - self.put_resource('1', 'page.html.template:fr', 20, - '{% extends "base.html.template" %} ' - '{% block foo %}fran\xc3\xa7ais{% endblock foo %}') + self.put_resource('1', 'page.html.template', 30, 'default') + self.put_resource('1', 'page.html.template:fr', 20, 'fran\xc3\xa7ais') self.put_resource('1', 'static.html', 30, 'hello') self.put_resource('1', 'static.html:fr', 20, 'bonjour') self.put_resource('1', 'data', 10, '\xff\xfe\xfd\xfc') @@ -214,38 +210,26 @@ def test_get_rendered(self): get_rendered = resources.get_rendered eq = self.assertEquals - # There's no es-specific page but there is an es-specific base template. - self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'es') == u'\xa1hola! default' - assert self.fetched == ['page.html:es', 'page.html', - 'page.html.template:es', 'page.html.template', - 'base.html.template:es'] - assert self.compiled == ['page.html.template', 'base.html.template:es'] - assert self.rendered == ['page.html.template'] - # There's an fr-specific page but no fr-specific base template. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'fr') == u'hi! fran\xe7ais' - assert self.fetched == ['page.html:fr', 'page.html', - 'page.html.template:fr', - 'base.html.template:fr', 'base.html.template'] - assert self.compiled == ['page.html.template:fr', 'base.html.template'] + assert get_rendered('page.html', 'fr') == u'fran\xe7ais' + assert self.fetched == [ + 'page.html:fr', 'page.html', 'page.html.template:fr'] + assert self.compiled == ['page.html.template:fr'] assert self.rendered == ['page.html.template:fr'] # There's no en-specific page and no en-specific base template. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'en') == u'hi! default' + assert get_rendered('page.html', 'en') == u'default' assert self.fetched == ['page.html:en', 'page.html', - 'page.html.template:en', 'page.html.template', - 'base.html.template:en', 'base.html.template'] - assert self.compiled == ['page.html.template', 'base.html.template'] + 'page.html.template:en', 'page.html.template'] + assert self.compiled == ['page.html.template'] assert self.rendered == ['page.html.template'] # These should be cache hits, and shouldn't fetch, compile, or render. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'es') == u'\xa1hola! default' - assert get_rendered('page.html', 'fr') == u'hi! fran\xe7ais' - assert get_rendered('page.html', 'en') == u'hi! default' + assert get_rendered('page.html', 'fr') == u'fran\xe7ais' + assert get_rendered('page.html', 'en') == u'default' assert self.fetched == [] assert self.compiled == [] assert self.rendered == [] @@ -255,7 +239,7 @@ def test_get_rendered(self): # Should fetch and recompile the pages but not the base templates. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'es') == u'\xa1hola! default' + assert get_rendered('page.html', 'es') == u'default' assert self.fetched == ['page.html:es', 'page.html', 'page.html.template:es', 'page.html.template'] assert self.compiled == ['page.html.template'] @@ -263,7 +247,7 @@ def test_get_rendered(self): # Should fetch and recompile the pages but not the base templates. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'fr') == u'hi! fran\xe7ais' + assert get_rendered('page.html', 'fr') == u'fran\xe7ais' assert self.fetched == ['page.html:fr', 'page.html', 'page.html.template:fr'] assert self.compiled == ['page.html.template:fr'] @@ -271,7 +255,7 @@ def test_get_rendered(self): # Should fetch and recompile the pages but not the base templates. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'en') == u'hi! default' + assert get_rendered('page.html', 'en') == u'default' assert self.fetched == ['page.html:en', 'page.html', 'page.html.template:en', 'page.html.template'] assert self.compiled == ['page.html.template'] @@ -281,29 +265,26 @@ def test_get_rendered(self): # (page.html.template:en and page.html.template:es remain cached). utils.set_utcnow_for_test(52) - # Should fetch and recompile the base template but not the page. + # Should not recompile the page. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'es') == u'\xa1hola! default' - assert self.fetched == ['page.html:es', 'page.html', - 'base.html.template:es'] - assert self.compiled == ['base.html.template:es'] + assert get_rendered('page.html', 'es') == u'default' + assert self.fetched == ['page.html:es', 'page.html'] + assert self.compiled == [] assert self.rendered == ['page.html.template'] # Should fetch and recompile both the fr page and the base template. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'fr') == u'hi! fran\xe7ais' + assert get_rendered('page.html', 'fr') == u'fran\xe7ais' assert self.fetched == ['page.html:fr', 'page.html', - 'page.html.template:fr', - 'base.html.template:fr', 'base.html.template'] - assert self.compiled == ['page.html.template:fr', 'base.html.template'] + 'page.html.template:fr'] + assert self.compiled == ['page.html.template:fr'] assert self.rendered == ['page.html.template:fr'] - # Should fetch and recompile the base template but not the page. + # Should not recompile the page. self.fetched, self.compiled, self.rendered = [], [], [] - assert get_rendered('page.html', 'en') == u'hi! default' - assert self.fetched == ['page.html:en', 'page.html', - 'base.html.template:en', 'base.html.template'] - assert self.compiled == ['base.html.template'] + assert get_rendered('page.html', 'en') == u'default' + assert self.fetched == ['page.html:en', 'page.html'] + assert self.compiled == [] assert self.rendered == ['page.html.template'] # Ensure binary data is preserved.