From 5d39b0ce2952c657f0d32467f89dfe08d2ee5b5e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 15 Jun 2015 13:27:23 -0400 Subject: [PATCH] Make edxmako cache properly with changing lookup paths --- common/djangoapps/edxmako/paths.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/common/djangoapps/edxmako/paths.py b/common/djangoapps/edxmako/paths.py index d14773954d..12cbcdf1d3 100644 --- a/common/djangoapps/edxmako/paths.py +++ b/common/djangoapps/edxmako/paths.py @@ -1,6 +1,8 @@ """ Set up lookup paths for mako templates. """ + +import hashlib import os import pkg_resources @@ -15,6 +17,10 @@ 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 add_directory(self, directory, prepend=False): """ Add a new directory to the template lookup path. @@ -24,6 +30,18 @@ class DynamicTemplateLookup(TemplateLookup): 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 clear_lookups(namespace): """