Files
edx-platform/common/djangoapps/edxmako/paths.py
Felipe Montoya 4742e661f4 Creating a settings object that is aware of the microsite settings.
Adding the declaration of the settings object to openedx.conf to be able to import it from a nicer location

Resolving quality violations

Merging dicts with the settings definition when they exist in the microsite configuration

Using a cache to improve the perfomance of quering any dictionary in the microsite definition

Ignoring the invalid-name pylint warning since the names must be kept thsi way to stay the same as the ones in django.
Removing the default dict argument as per https://docs.python.org/2/tutorial/controlflow.html#default-argument-values

Extracting the implementation of the microsite to a selectable backend.

Leaving the function startup.enable_microsites for backwards compatibilityy

Adding a database backend

Using a cache to improve the perfomance of quering any dictionary in the microsite definition.
Changed the database backend so that it extends the settings file backend and removed all the unnecessary methods.

Using the backend provider for the get_dict function

some tweeks and some initial unit tests

Using getattr as a function insteal of calling the underlying __getattr__ directly

Adding an ModelAdmin object for the microsite model in the django-admin panel

refactor enable_microsites()

consolidate/refactor some shared code

add config to aws.py and add migration files

fix tests

Changes to get the backends to run after the refactor

add archiving capabilities to microsites. Also make a few notes about performance improvements to make

fix tests

Making the query to find if microsites exist in the database faster

add ORG to microsite mapping tables and some performance improvements

allow for Mako templates to be pulled from the database

fix tests

For the database template backend the uri of the template does not use the filesystem relative path

Fixing pylint violations

Added caching of the templates stored in the database

Fixing pylint errors

fix pylint

Clearing the cache on model save

Fixing pylint errors

rebased and added test coverage

rebased cdodge/microsite-improvements branch with master and added test
coverage

added missing migration

fix quality violations

add more test coverage

mattdrayer: Add microsite_configuration to cms.INSTALLED_APPS

added microsite settings to cms/envs/test.py

run session cookie tests only in LMS

fixed broken tests

putting middleware changes back

Preventing the template_backend to be called on requests which have no microsite

changes to address feedback from mjfrey

changed BaseMicrositeBackend to AbstractBaseMicrositeBackend

changes after feedback from mattdrayer

fixed broken tests and quality violations

Allowing the backend to handle the enable_pre_startup routine

Typos and docstrings

Adressing feedback

Fixing python tests

add comment to explain why we need enable_microsites_pre_startup()
2016-01-15 14:52:36 +05:00

121 lines
3.7 KiB
Python

"""
Set up lookup paths for mako templates.
"""
import hashlib
import contextlib
import os
import pkg_resources
from django.conf import settings
from mako.lookup import TemplateLookup
from microsite_configuration import microsite
from . import LOOKUP
class DynamicTemplateLookup(TemplateLookup):
"""
A specialization of the standard mako `TemplateLookup` class which allows
for adding directories progressively.
"""
def __init__(self, *args, **kwargs):
super(DynamicTemplateLookup, self).__init__(*args, **kwargs)
self.__original_module_directory = self.template_args['module_directory']
def __repr__(self):
return "<{0.__class__.__name__} {0.directories}>".format(self)
def add_directory(self, directory, prepend=False):
"""
Add a new directory to the template lookup path.
"""
if prepend:
self.directories.insert(0, os.path.normpath(directory))
else:
self.directories.append(os.path.normpath(directory))
# Since the lookup path has changed, the compiled modules might be
# wrong because now "foo.html" might be a completely different template,
# and "foo.html.py" in the module directory has no way to know that.
# Update the module_directory argument to point to a directory
# specifically for this lookup path.
unique = hashlib.md5(":".join(str(d) for d in self.directories)).hexdigest()
self.template_args['module_directory'] = os.path.join(self.__original_module_directory, unique)
# Also clear the internal caches. Ick.
self._collection.clear()
self._uri_cache.clear()
def get_template(self, uri):
"""
Overridden method which will hand-off the template lookup to the microsite subsystem
"""
microsite_template = microsite.get_template(uri)
return (
microsite_template
if microsite_template
else super(DynamicTemplateLookup, self).get_template(uri)
)
def clear_lookups(namespace):
"""
Remove mako template lookups for the given namespace.
"""
if namespace in LOOKUP:
del LOOKUP[namespace]
def add_lookup(namespace, directory, package=None, prepend=False):
"""
Adds a new mako template lookup directory to the given namespace.
If `package` is specified, `pkg_resources` is used to look up the directory
inside the given package. Otherwise `directory` is assumed to be a path
in the filesystem.
"""
templates = LOOKUP.get(namespace)
if not templates:
LOOKUP[namespace] = templates = DynamicTemplateLookup(
module_directory=settings.MAKO_MODULE_DIR,
output_encoding='utf-8',
input_encoding='utf-8',
default_filters=['decode.utf8'],
encoding_errors='replace',
)
if package:
directory = pkg_resources.resource_filename(package, directory)
templates.add_directory(directory, prepend=prepend)
def lookup_template(namespace, name):
"""
Look up a Mako template by namespace and name.
"""
return LOOKUP[namespace].get_template(name)
@contextlib.contextmanager
def save_lookups():
"""
A context manager to save and restore the Mako template lookup path.
Useful for testing.
"""
# Make a copy of the list of directories for each namespace.
namespace_dirs = {namespace: list(look.directories) for namespace, look in LOOKUP.items()}
try:
yield
finally:
# Get rid of all the lookups.
LOOKUP.clear()
# Re-create the lookups from our saved list.
for namespace, directories in namespace_dirs.items():
for directory in directories:
add_lookup(namespace, directory)