Commit 510f0286 by Piotr Mitros

Merge pull request #4 from edx/juho/fixes

render bug fix with updated path, updated to edx repo in req.txt
parents d587cd94 8e12be68
...@@ -7,3 +7,4 @@ def dump_to_db(db, events): ...@@ -7,3 +7,4 @@ def dump_to_db(db, events):
## TODO: Error handling ## TODO: Error handling
collection = db['event_log'] collection = db['event_log']
collection.insert([e.event for e in events]) collection.insert([e.event for e in events])
...@@ -15,5 +15,5 @@ simplejson ...@@ -15,5 +15,5 @@ simplejson
South==0.7.6 South==0.7.6
django-celery==3.0.11 django-celery==3.0.11
celery-with-redis==3.0 celery-with-redis==3.0
-e git://github.com/MITx/django-pipeline.git@c5a4848d3d8fa90a7da4a4007f5653be40cccdd9#egg=django_pipeline-dev -e git://github.com/edx/django-pipeline.git@c5a4848d3d8fa90a7da4a4007f5653be40cccdd9#egg=django_pipeline-dev
-e git://github.com/MITx/django-staticfiles.git@6d2504e5c84a3003b4573e0ba0f11adf7583d372#egg=django_staticfiles-dev -e git://github.com/edx/django-staticfiles.git@6d2504e5c84a3003b4573e0ba0f11adf7583d372#egg=django_staticfiles-dev
...@@ -36,6 +36,7 @@ from pkg_resources import resource_filename ...@@ -36,6 +36,7 @@ from pkg_resources import resource_filename
from mako.lookup import TemplateLookup from mako.lookup import TemplateLookup
from django.conf import settings from django.conf import settings
## Code borrowed from mitx/common/lib/tempdir ## Code borrowed from mitx/common/lib/tempdir
def mkdtemp_clean(suffix="", prefix="tmp", dir=None): def mkdtemp_clean(suffix="", prefix="tmp", dir=None):
"""Just like mkdtemp, but the directory will be deleted when the process ends.""" """Just like mkdtemp, but the directory will be deleted when the process ends."""
...@@ -43,6 +44,7 @@ def mkdtemp_clean(suffix="", prefix="tmp", dir=None): ...@@ -43,6 +44,7 @@ def mkdtemp_clean(suffix="", prefix="tmp", dir=None):
atexit.register(cleanup_tempdir, the_dir) atexit.register(cleanup_tempdir, the_dir)
return the_dir return the_dir
def cleanup_tempdir(the_dir): def cleanup_tempdir(the_dir):
"""Called on process exit to remove a temp directory.""" """Called on process exit to remove a temp directory."""
if os.path.exists(the_dir): if os.path.exists(the_dir):
...@@ -53,30 +55,41 @@ if module_directory is None: ...@@ -53,30 +55,41 @@ if module_directory is None:
module_directory = mkdtemp_clean() module_directory = mkdtemp_clean()
lookups = {} lookups = {}
def lookup(directory): def lookup(directory):
if directory in lookups: if directory in lookups:
return lookups[directory] return lookups[directory]
else: else:
l = TemplateLookup(directories = [directory], l = TemplateLookup(directories=[directory],
module_directory = module_directory, module_directory=module_directory,
output_encoding='utf-8', output_encoding='utf-8',
input_encoding='utf-8', input_encoding='utf-8',
encoding_errors='replace') encoding_errors='replace')
lookups[directory] = l lookups[directory] = l
return l return l
def render(templatefile, context, caller = None):
def render(templatefile, context, caller=None):
''' Render a template within a module.
1) Figure out the module this is being called from.
2) Use resource_filename from packagetools to figure out that module's templates directory
3) Use the mako template engine to render it.
Hacks:
1) We are not caching anything. This will cause performance issues for deployment.
2) The way we inspect the stack is quite messy. We should do this better.
'''
stack = traceback.extract_stack() stack = traceback.extract_stack()
if not caller: if not caller:
caller_path = os.path.abspath(stack[-2][0]) caller_path = os.path.abspath(stack[-2][0])
# For testing, use: sys.modules.keys() if sys.modules[module] and '__file__' in sys.modules[module].__dict__]# # For testing, use: sys.modules.keys() if sys.modules[module] and '__file__' in sys.modules[module].__dict__]#
analytics_modules = [sys.modules[module] for module in settings.INSTALLED_ANALYTICS_MODULES] analytics_modules = [sys.modules[module] for module in settings.INSTALLED_ANALYTICS_MODULES]
analytics_modules.sort(key = lambda x : len(os.path.commonprefix([x.__file__, os.path.abspath(caller_path)]))) analytics_modules.sort(key=lambda x: len(os.path.commonprefix([os.path.abspath(x.__file__), os.path.abspath(caller_path)])))
caller_module = analytics_modules[-1] caller_module = analytics_modules[-1]
caller_name = caller_module.__name__ caller_name = caller_module.__name__
template_directory = os.path.abspath(resource_filename(caller_name, "templates")) template_directory = os.path.abspath(resource_filename(caller_name, "templates"))
template = lookup(template_directory).get_template(templatefile) template = lookup(template_directory).get_template(templatefile)
return template.render_unicode(**context) return template.render_unicode(**context)
...@@ -86,6 +99,7 @@ from django.contrib.staticfiles.finders import BaseFinder ...@@ -86,6 +99,7 @@ from django.contrib.staticfiles.finders import BaseFinder
from django.contrib.staticfiles import utils from django.contrib.staticfiles import utils
from django.core.files.storage import FileSystemStorage from django.core.files.storage import FileSystemStorage
class ModuleStorage(FileSystemStorage): class ModuleStorage(FileSystemStorage):
''' Storage which allows static files to live with prefixes in the ''' Storage which allows static files to live with prefixes in the
static directory which do not exist in the filesystem. A file can static directory which do not exist in the filesystem. A file can
...@@ -112,6 +126,7 @@ class ModuleStorage(FileSystemStorage): ...@@ -112,6 +126,7 @@ class ModuleStorage(FileSystemStorage):
else: else:
return FileSystemStorage.listdir(self, path) return FileSystemStorage.listdir(self, path)
class ModuleFileFinder(BaseFinder): class ModuleFileFinder(BaseFinder):
''' Finds the static files for all installed analytics ''' Finds the static files for all installed analytics
modules. Does magic to put them in the right place in the URL modules. Does magic to put them in the right place in the URL
...@@ -142,5 +157,3 @@ class ModuleFileFinder(BaseFinder): ...@@ -142,5 +157,3 @@ class ModuleFileFinder(BaseFinder):
for module, path, storage in self.static_paths: for module, path, storage in self.static_paths:
for path in utils.get_files(storage, ignore_patterns): for path in utils.get_files(storage, ignore_patterns):
yield path, storage yield path, storage
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment