From abef62a23a112b4afdb02cfeaacb67e6e09a8c21 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sat, 28 Jan 2012 21:27:54 -0500 Subject: [PATCH 1/3] Syncing between machines --- settings_new_askbot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/settings_new_askbot.py b/settings_new_askbot.py index aa79e08ea8..b7d918bcfc 100644 --- a/settings_new_askbot.py +++ b/settings_new_askbot.py @@ -107,6 +107,7 @@ INSTALLED_APPS = ( 'track', 'circuit', 'perfstats', + 'util', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: From b86ba908da84367a6a599827bd5f522b88f440e6 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sun, 29 Jan 2012 07:46:41 -0500 Subject: [PATCH 2/3] Forked django middleware and moved it into the project. We'd like better django integration --- auth/views.py | 2 +- circuit/views.py | 2 +- courseware/capa/inputtypes.py | 2 +- courseware/module_render.py | 2 +- courseware/modules/capa_module.py | 2 +- courseware/modules/html_module.py | 2 +- courseware/modules/schematic_module.py | 2 +- courseware/modules/seq_module.py | 2 +- courseware/modules/vertical_module.py | 2 +- courseware/modules/video_module.py | 2 +- courseware/views.py | 2 +- settings_new_askbot.py | 5 ++++- settings_old_askbot.py | 2 +- simplewiki/mdx_circuit.py | 2 +- simplewiki/views.py | 4 ++-- static_template_view/views.py | 2 +- staticbook/views.py | 2 +- util/views.py | 2 +- 18 files changed, 22 insertions(+), 19 deletions(-) diff --git a/auth/views.py b/auth/views.py index 56c9cd81de..f2f945644f 100644 --- a/auth/views.py +++ b/auth/views.py @@ -12,7 +12,7 @@ from django.core.validators import validate_email, validate_slug from django.db import connection from django.http import HttpResponse from django.shortcuts import redirect -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from models import Registration, UserProfile log = logging.getLogger("mitx.auth") diff --git a/circuit/views.py b/circuit/views.py index c8840484a8..96fcc83115 100644 --- a/circuit/views.py +++ b/circuit/views.py @@ -7,7 +7,7 @@ from django.conf import settings from django.http import Http404 from django.http import HttpResponse from django.shortcuts import redirect -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from models import ServerCircuit diff --git a/courseware/capa/inputtypes.py b/courseware/capa/inputtypes.py index c3b59a4537..c7732014da 100644 --- a/courseware/capa/inputtypes.py +++ b/courseware/capa/inputtypes.py @@ -1,7 +1,7 @@ from lxml.etree import Element from lxml import etree -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string class textline(object): @staticmethod diff --git a/courseware/module_render.py b/courseware/module_render.py index 96833cc32d..6aa407bf80 100644 --- a/courseware/module_render.py +++ b/courseware/module_render.py @@ -17,7 +17,7 @@ from django.http import HttpResponse from django.shortcuts import redirect from django.template import Context from django.template import Context, loader -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from auth.models import UserProfile from models import StudentModule diff --git a/courseware/modules/capa_module.py b/courseware/modules/capa_module.py index 8eeeec1f3c..b6c7f1748c 100644 --- a/courseware/modules/capa_module.py +++ b/courseware/modules/capa_module.py @@ -16,7 +16,7 @@ from lxml import etree ## TODO: Abstract out from Django from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from django.http import Http404 from x_module import XModule diff --git a/courseware/modules/html_module.py b/courseware/modules/html_module.py index 61c903d41c..e72fea9dfd 100644 --- a/courseware/modules/html_module.py +++ b/courseware/modules/html_module.py @@ -2,7 +2,7 @@ import json ## TODO: Abstract out from Django from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from x_module import XModule from lxml import etree diff --git a/courseware/modules/schematic_module.py b/courseware/modules/schematic_module.py index 9c7d291b92..0312321b4d 100644 --- a/courseware/modules/schematic_module.py +++ b/courseware/modules/schematic_module.py @@ -2,7 +2,7 @@ import json ## TODO: Abstract out from Django from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from x_module import XModule diff --git a/courseware/modules/seq_module.py b/courseware/modules/seq_module.py index 2a91b0caa0..02c8320ac8 100644 --- a/courseware/modules/seq_module.py +++ b/courseware/modules/seq_module.py @@ -5,7 +5,7 @@ from lxml import etree ## TODO: Abstract out from Django from django.http import Http404 from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from x_module import XModule diff --git a/courseware/modules/vertical_module.py b/courseware/modules/vertical_module.py index 5530e22415..6939c75cc9 100644 --- a/courseware/modules/vertical_module.py +++ b/courseware/modules/vertical_module.py @@ -2,7 +2,7 @@ import json ## TODO: Abstract out from Django from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from x_module import XModule from lxml import etree diff --git a/courseware/modules/video_module.py b/courseware/modules/video_module.py index 69a0837403..4225f36880 100644 --- a/courseware/modules/video_module.py +++ b/courseware/modules/video_module.py @@ -5,7 +5,7 @@ from lxml import etree ## TODO: Abstract out from Django from django.conf import settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from x_module import XModule diff --git a/courseware/views.py b/courseware/views.py index 9254da1e1e..f7035975df 100644 --- a/courseware/views.py +++ b/courseware/views.py @@ -12,7 +12,7 @@ from django.contrib.auth.models import User from django.http import HttpResponse, Http404 from django.shortcuts import redirect from django.template import Context, loader -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from django.db import connection from lxml import etree diff --git a/settings_new_askbot.py b/settings_new_askbot.py index aa79e08ea8..eb168dabd2 100644 --- a/settings_new_askbot.py +++ b/settings_new_askbot.py @@ -3,6 +3,9 @@ import sys import djcelery +LIB_URL = '/static/lib/' +BOOK_URL = '/static/book/' + # Our parent dir (mitx_all) is the BASE_DIR BASE_DIR = os.path.abspath(os.path.join(__file__, "..", "..")) @@ -85,7 +88,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'track.middleware.TrackMiddleware', - 'djangomako.middleware.MakoMiddleware', + 'mitxmako.middleware.MakoMiddleware', #'debug_toolbar.middleware.DebugToolbarMiddleware', ) diff --git a/settings_old_askbot.py b/settings_old_askbot.py index d7783c05db..b711440d65 100644 --- a/settings_old_askbot.py +++ b/settings_old_askbot.py @@ -81,7 +81,7 @@ MIDDLEWARE_CLASSES = ( 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'track.middleware.TrackMiddleware', - 'djangomako.middleware.MakoMiddleware', + 'mitxmako.middleware.MakoMiddleware', #'debug_toolbar.middleware.DebugToolbarMiddleware', ) diff --git a/simplewiki/mdx_circuit.py b/simplewiki/mdx_circuit.py index efac721b7c..465e7d9a3a 100755 --- a/simplewiki/mdx_circuit.py +++ b/simplewiki/mdx_circuit.py @@ -8,7 +8,7 @@ circuit:name becomes the circuit. import simplewiki.settings as settings -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string import markdown try: diff --git a/simplewiki/views.py b/simplewiki/views.py index 16bfc11c44..856a45d1c0 100644 --- a/simplewiki/views.py +++ b/simplewiki/views.py @@ -15,10 +15,10 @@ from django.template import Context from django.template import RequestContext, Context, loader from django.utils import simplejson from django.utils.translation import ugettext_lazy as _ -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from mako.lookup import TemplateLookup from mako.template import Template -import djangomako.middleware +import mitxmako.middleware from models import * # TODO: Clean up from settings import * diff --git a/static_template_view/views.py b/static_template_view/views.py index 5579e20c86..734434b43a 100644 --- a/static_template_view/views.py +++ b/static_template_view/views.py @@ -3,7 +3,7 @@ # List of valid templates is explicitly managed for (short-term) # security reasons. -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string from django.shortcuts import redirect from django.core.context_processors import csrf diff --git a/staticbook/views.py b/staticbook/views.py index bb6e2ada19..2e5712fc67 100644 --- a/staticbook/views.py +++ b/staticbook/views.py @@ -4,7 +4,7 @@ import os from django.conf import settings from django.http import Http404 from django.shortcuts import redirect -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string def index(request, page=0): if not request.user.is_authenticated(): diff --git a/util/views.py b/util/views.py index 139906e24a..5f0400d143 100644 --- a/util/views.py +++ b/util/views.py @@ -10,7 +10,7 @@ from django.core.mail import send_mail from django.http import Http404 from django.http import HttpResponse from django.shortcuts import redirect -from djangomako.shortcuts import render_to_response, render_to_string +from mitxmako.shortcuts import render_to_response, render_to_string import courseware.capa.calc import track.views From c99bc566ba02bf7e68229046e0d7499f60da4dc2 Mon Sep 17 00:00:00 2001 From: Piotr Mitros Date: Sun, 29 Jan 2012 13:40:52 -0500 Subject: [PATCH 3/3] 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)