Merge pull request #811 from cpennington/initialize-mitxmako-at-startup
Move mitxmako initialization to a startup module
This commit is contained in:
@@ -12,36 +12,11 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from mako.lookup import TemplateLookup
|
||||
import tempdir
|
||||
from django.template import RequestContext
|
||||
from django.conf import settings
|
||||
|
||||
requestcontext = None
|
||||
lookup = {}
|
||||
|
||||
|
||||
class MakoMiddleware(object):
|
||||
def __init__(self):
|
||||
"""Setup mako variables and lookup object"""
|
||||
# Set all mako variables based on django settings
|
||||
template_locations = settings.MAKO_TEMPLATES
|
||||
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
|
||||
|
||||
if module_directory is None:
|
||||
module_directory = tempdir.mkdtemp_clean()
|
||||
|
||||
for location in template_locations:
|
||||
lookup[location] = TemplateLookup(directories=template_locations[location],
|
||||
module_directory=module_directory,
|
||||
output_encoding='utf-8',
|
||||
input_encoding='utf-8',
|
||||
default_filters=['decode.utf8'],
|
||||
encoding_errors='replace',
|
||||
)
|
||||
|
||||
import mitxmako
|
||||
mitxmako.lookup = lookup
|
||||
|
||||
def process_request(self, request):
|
||||
global requestcontext
|
||||
|
||||
@@ -16,7 +16,7 @@ from django.template import Context
|
||||
from django.http import HttpResponse
|
||||
import logging
|
||||
|
||||
from . import middleware
|
||||
import mitxmako
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
log = logging.getLogger(__name__)
|
||||
@@ -80,15 +80,15 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'):
|
||||
context_instance['marketing_link'] = marketing_link
|
||||
|
||||
# In various testing contexts, there might not be a current request context.
|
||||
if middleware.requestcontext is not None:
|
||||
for d in middleware.requestcontext:
|
||||
if mitxmako.middleware.requestcontext is not None:
|
||||
for d in mitxmako.middleware.requestcontext:
|
||||
context_dictionary.update(d)
|
||||
for d in context_instance:
|
||||
context_dictionary.update(d)
|
||||
if context:
|
||||
context_dictionary.update(context)
|
||||
# fetch and render template
|
||||
template = middleware.lookup[namespace].get_template(template_name)
|
||||
template = mitxmako.lookup[namespace].get_template(template_name)
|
||||
return template.render_unicode(**context_dictionary)
|
||||
|
||||
|
||||
|
||||
33
common/djangoapps/mitxmako/startup.py
Normal file
33
common/djangoapps/mitxmako/startup.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""
|
||||
Initialize the mako template lookup
|
||||
"""
|
||||
|
||||
import tempdir
|
||||
from django.conf import settings
|
||||
from mako.lookup import TemplateLookup
|
||||
|
||||
import mitxmako
|
||||
|
||||
|
||||
def run():
|
||||
"""Setup mako variables and lookup object"""
|
||||
# Set all mako variables based on django settings
|
||||
template_locations = settings.MAKO_TEMPLATES
|
||||
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
|
||||
|
||||
if module_directory is None:
|
||||
module_directory = tempdir.mkdtemp_clean()
|
||||
|
||||
lookup = {}
|
||||
|
||||
for location in template_locations:
|
||||
lookup[location] = TemplateLookup(
|
||||
directories=template_locations[location],
|
||||
module_directory=module_directory,
|
||||
output_encoding='utf-8',
|
||||
input_encoding='utf-8',
|
||||
default_filters=['decode.utf8'],
|
||||
encoding_errors='replace',
|
||||
)
|
||||
|
||||
mitxmako.lookup = lookup
|
||||
@@ -16,7 +16,8 @@ from django.conf import settings
|
||||
from mako.template import Template as MakoTemplate
|
||||
from mitxmako.shortcuts import marketing_link
|
||||
|
||||
from mitxmako import middleware
|
||||
import mitxmako
|
||||
import mitxmako.middleware
|
||||
|
||||
django_variables = ['lookup', 'output_encoding', 'encoding_errors']
|
||||
|
||||
@@ -33,7 +34,7 @@ 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])
|
||||
overrides = dict([(k, getattr(mitxmako, k, None),) for k in django_variables])
|
||||
overrides['lookup'] = overrides['lookup']['main']
|
||||
kwargs.update(overrides)
|
||||
super(Template, self).__init__(*args, **kwargs)
|
||||
@@ -47,8 +48,8 @@ class Template(MakoTemplate):
|
||||
context_dictionary = {}
|
||||
|
||||
# In various testing contexts, there might not be a current request context.
|
||||
if middleware.requestcontext is not None:
|
||||
for d in middleware.requestcontext:
|
||||
if mitxmako.middleware.requestcontext is not None:
|
||||
for d in mitxmako.middleware.requestcontext:
|
||||
context_dictionary.update(d)
|
||||
for d in context_instance:
|
||||
context_dictionary.update(d)
|
||||
|
||||
@@ -16,10 +16,6 @@ from django.contrib.auth.models import User
|
||||
|
||||
from student.models import UserProfile
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = \
|
||||
|
||||
@@ -12,10 +12,6 @@ from django.contrib.auth.models import User
|
||||
|
||||
from student.models import UserProfile
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
def import_user(u):
|
||||
user_info = u['u']
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
from student.models import UserTestGroup
|
||||
|
||||
import random
|
||||
@@ -11,8 +10,6 @@ import datetime
|
||||
import json
|
||||
from pytz import UTC
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
def group_from_value(groups, v):
|
||||
''' Given group: (('a',0.3),('b',0.4),('c',0.3)) And random value
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = \
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
import mitxmako
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
@@ -17,8 +15,8 @@ body, and an _subject.txt for the subject. '''
|
||||
#text = open(args[0]).read()
|
||||
#subject = open(args[1]).read()
|
||||
users = User.objects.all()
|
||||
text = middleware.lookup['main'].get_template('email/' + args[0] + ".txt").render()
|
||||
subject = middleware.lookup['main'].get_template('email/' + args[0] + "_subject.txt").render().strip()
|
||||
text = mitxmako.lookup['main'].get_template('email/' + args[0] + ".txt").render()
|
||||
subject = mitxmako.lookup['main'].get_template('email/' + args[0] + "_subject.txt").render().strip()
|
||||
for user in users:
|
||||
if user.is_active:
|
||||
user.email_user(subject, text)
|
||||
|
||||
@@ -4,15 +4,13 @@ import time
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.conf import settings
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
import mitxmako
|
||||
|
||||
from django.core.mail import send_mass_mail
|
||||
import sys
|
||||
|
||||
import datetime
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
def chunks(l, n):
|
||||
""" Yield successive n-sized chunks from l.
|
||||
@@ -41,8 +39,8 @@ rate -- messages per second
|
||||
|
||||
users = [u.strip() for u in open(user_file).readlines()]
|
||||
|
||||
message = middleware.lookup['main'].get_template('emails/' + message_base + "_body.txt").render()
|
||||
subject = middleware.lookup['main'].get_template('emails/' + message_base + "_subject.txt").render().strip()
|
||||
message = mitxmako.lookup['main'].get_template('emails/' + message_base + "_body.txt").render()
|
||||
subject = mitxmako.lookup['main'].get_template('emails/' + message_base + "_subject.txt").render().strip()
|
||||
rate = int(ratestr)
|
||||
|
||||
self.log_file = open(logfilename, "a+", buffering=0)
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import mitxmako.middleware as middleware
|
||||
import json
|
||||
|
||||
from student.models import UserProfile
|
||||
|
||||
middleware.MakoMiddleware()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = \
|
||||
|
||||
@@ -7,7 +7,7 @@ def autostartup():
|
||||
"""
|
||||
for app in settings.INSTALLED_APPS:
|
||||
try:
|
||||
mod = import_module('{}.startup')
|
||||
mod = import_module(app + '.startup')
|
||||
if hasattr(mod, 'run'):
|
||||
mod.run()
|
||||
except ImportError:
|
||||
|
||||
Reference in New Issue
Block a user