From c99bc566ba02bf7e68229046e0d7499f60da4dc2 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sun, 29 Jan 2012 13:40:52 -0500 Subject: [PATCH] Forked django-mako into our project. --- mitxmako/README | 15 +++++++++++++++ mitxmako/__init__.py | 16 ++++++++++++++++ mitxmako/middleware.py | 41 +++++++++++++++++++++++++++++++++++++++++ mitxmako/shortcuts.py | 37 +++++++++++++++++++++++++++++++++++++ mitxmako/template.py | 28 ++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 mitxmako/README create mode 100644 mitxmako/__init__.py create mode 100644 mitxmako/middleware.py create mode 100644 mitxmako/shortcuts.py create mode 100644 mitxmako/template.py diff --git a/mitxmako/README b/mitxmako/README new file mode 100644 index 0000000000..ab04df2cf7 --- /dev/null +++ b/mitxmako/README @@ -0,0 +1,15 @@ +================================================================================ +django-mako +================================================================================ +This module provides a drop in replacement of Django templates for Mako +Templates. + +Django: http://www.djangoproject.com/ +Mako: http://www.makotemplates.org/ + +================================================================================ +How to install? +================================================================================ + + $ sudo python setup.py install + diff --git a/mitxmako/__init__.py b/mitxmako/__init__.py new file mode 100644 index 0000000000..aa8a48bfe7 --- /dev/null +++ b/mitxmako/__init__.py @@ -0,0 +1,16 @@ +# Copyright (c) 2008 Mikeal Rogers +# +# 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. + +lookup = None + diff --git a/mitxmako/middleware.py b/mitxmako/middleware.py new file mode 100644 index 0000000000..01990fa93a --- /dev/null +++ b/mitxmako/middleware.py @@ -0,0 +1,41 @@ +# Copyright (c) 2008 Mikeal Rogers +# +# 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. + +from mako.lookup import TemplateLookup +import tempfile + +class MakoMiddleware(object): + def __init__(self): + """Setup mako variables and lookup object""" + from django.conf import settings + # Set all mako variables based on django settings + global template_dirs, output_encoding, module_directory, encoding_errors + directories = getattr(settings, 'MAKO_TEMPLATE_DIRS', settings.TEMPLATE_DIRS) + + module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) + if module_directory is None: + module_directory = tempfile.mkdtemp() + + output_encoding = getattr(settings, 'MAKO_OUTPUT_ENCODING', 'utf-8') + encoding_errors = getattr(settings, 'MAKO_ENCODING_ERRORS', 'replace') + + global lookup + lookup = TemplateLookup(directories=directories, + module_directory=module_directory, + output_encoding=output_encoding, + encoding_errors=encoding_errors, + ) + import mitxmako + mitxmako.lookup = lookup + diff --git a/mitxmako/shortcuts.py b/mitxmako/shortcuts.py new file mode 100644 index 0000000000..946832cfaf --- /dev/null +++ b/mitxmako/shortcuts.py @@ -0,0 +1,37 @@ +# Copyright (c) 2008 Mikeal Rogers +# +# 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. + +from django.template import Context +from django.http import HttpResponse + +import middleware + +def render_to_string(template_name, dictionary, context_instance=None): + context_instance = context_instance or Context(dictionary) + # add dictionary to context_instance + context_instance.update(dictionary or {}) + # collapse context_instance to a single dictionary for mako + context_dictionary = {} + for d in context_instance: + context_dictionary.update(d) + # fetch and render template + template = middleware.lookup.get_template(template_name) + return template.render(**context_dictionary) + +def render_to_response(template_name, dictionary, context_instance=None, **kwargs): + """ + Returns a HttpResponse whose content is filled with the result of calling + lookup.get_template(args[0]).render with the passed arguments. + """ + return HttpResponse(render_to_string(template_name, dictionary, context_instance), **kwargs) diff --git a/mitxmako/template.py b/mitxmako/template.py new file mode 100644 index 0000000000..9e5897ef25 --- /dev/null +++ b/mitxmako/template.py @@ -0,0 +1,28 @@ +# Copyright (c) 2008 Mikeal Rogers +# +# 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. + +from mako.template import Template as MakoTemplate + +import middleware + +django_variables = ['lookup', 'template_dirs', 'output_encoding', + 'module_directory', 'encoding_errors',] + +class Template(MakoTemplate): + def __init__(self, *args, **kwargs): + """Overrides base __init__ to provide django variable overrides""" + if not kwargs.get('no_django', False): + overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables]) + kwargs.update(overrides) + super(Template, self).__init__(*args, **kwargs)