Change `uploader` to `actor`
[mediagoblin.git] / mediagoblin / tools / template.py
index 0986761b7e4539acd09f72b77e7cc82c47d1a511..f2619808438aa46c29e00fb5bd82b676a51cf0d0 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from math import ceil
+import six
+
 import jinja2
+from jinja2.ext import Extension
+from jinja2.nodes import Include, Const
+
 from babel.localedata import exists
+from werkzeug.urls import url_quote_plus
+
 from mediagoblin import mg_globals
 from mediagoblin import messages
+from mediagoblin import _version
 from mediagoblin.tools import common
-from mediagoblin.tools.translate import setup_gettext
-from mediagoblin.middleware.csrf import render_csrf_form_token
-
+from mediagoblin.tools.translate import is_rtl
+from mediagoblin.tools.translate import set_thread_locale
+from mediagoblin.tools.pluginapi import get_hook_templates, hook_transform
+from mediagoblin.tools.timesince import timesince
+from mediagoblin.meddleware.csrf import render_csrf_form_token
 
 SETUP_JINJA_ENVS = {}
 
 
-def get_jinja_env(template_loader, locale):
+def get_jinja_env(app, template_loader, locale):
     """
     Set up the Jinja environment,
 
     (In the future we may have another system for providing theming;
     for now this is good enough.)
     """
-    setup_gettext(locale)
+    set_thread_locale(locale)
 
     # If we have a jinja environment set up with this locale, just
     # return that one.
-    if SETUP_JINJA_ENVS.has_key(locale):
+    if locale in SETUP_JINJA_ENVS:
         return SETUP_JINJA_ENVS[locale]
 
+    # The default config does not require a [jinja2] block.
+    # You may create one if you wish to enable additional jinja2 extensions,
+    # see example in config_spec.ini 
+    jinja2_config = app.global_config.get('jinja2', {})
+    local_exts = jinja2_config.get('extensions', [])
+
+    # jinja2.StrictUndefined will give exceptions on references
+    # to undefined/unknown variables in templates.
     template_env = jinja2.Environment(
         loader=template_loader, autoescape=True,
-        extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
-
-    template_env.install_gettext_callables(
-        mg_globals.translations.ugettext,
-        mg_globals.translations.ungettext)
+        undefined=jinja2.StrictUndefined,
+        extensions=[
+            'jinja2.ext.i18n', 'jinja2.ext.autoescape',
+            TemplateHookExtension] + local_exts)
+
+    if six.PY2:
+        template_env.install_gettext_callables(mg_globals.thread_scope.translations.ugettext,
+                                           mg_globals.thread_scope.translations.ungettext)
+    else:
+        template_env.install_gettext_callables(mg_globals.thread_scope.translations.gettext,
+                                           mg_globals.thread_scope.translations.ngettext)
 
     # All templates will know how to ...
     # ... fetch all waiting messages and remove them from the queue
     # ... construct a grid of thumbnails or other media
     # ... have access to the global and app config
+    # ... determine if the language is rtl or ltr
     template_env.globals['fetch_messages'] = messages.fetch_messages
-    template_env.globals['gridify_list'] = gridify_list
-    template_env.globals['gridify_cursor'] = gridify_cursor
-    template_env.globals['app_config'] = mg_globals.app_config
-    template_env.globals['global_config'] = mg_globals.global_config
+    template_env.globals['app_config'] = app.app_config
+    template_env.globals['global_config'] = app.global_config
+    template_env.globals['version'] = _version.__version__
+    template_env.globals['auth'] = app.auth
+    template_env.globals['is_rtl'] = is_rtl(locale)
+    template_env.filters['urlencode'] = url_quote_plus
+
+    # add human readable fuzzy date time
+    template_env.globals['timesince'] = timesince
+
+    # allow for hooking up plugin templates
+    template_env.globals['get_hook_templates'] = get_hook_templates
+
+    template_env.globals = hook_transform(
+        'template_global_context', template_env.globals)
+
+    #### THIS IS TEMPORARY, PLEASE FIX IT
+    ## Notifications stuff is not yet a plugin (and we're not sure it will be),
+    ## but it needs to add stuff to the context.  This is THE WRONG WAY TO DO IT
+    from mediagoblin import notifications
+    template_env.globals['get_notifications'] = notifications.get_notifications
+    template_env.globals[
+        'get_notification_count'] = notifications.get_notification_count
+    template_env.globals[
+        'get_comment_subscription'] = notifications.get_comment_subscription
 
     if exists(locale):
         SETUP_JINJA_ENVS[locale] = template_env
@@ -79,7 +124,23 @@ def render_template(request, template_path, context):
     template = request.template_env.get_template(
         template_path)
     context['request'] = request
-    context['csrf_token'] = render_csrf_form_token(request)
+    rendered_csrf_token = render_csrf_form_token(request)
+    if rendered_csrf_token is not None:
+        context['csrf_token'] = render_csrf_form_token(request)
+
+    # allow plugins to do things to the context
+    if request.controller_name:
+        context = hook_transform(
+            (request.controller_name, template_path),
+            context)
+
+    # More evil: allow plugins to possibly do something to the context
+    # in every request ever with access to the request and other
+    # variables.  Note: this is slower than using
+    # template_global_context
+    context = hook_transform(
+        'template_context_prerender', context)
+
     rendered = template.render(context)
 
     if common.TESTS_ENABLED:
@@ -93,30 +154,28 @@ def clear_test_template_context():
     TEMPLATE_TEST_CONTEXT = {}
 
 
-def gridify_list(this_list, num_cols=5):
-    """
-    Generates a list of lists where each sub-list's length depends on
-    the number of columns in the list
+class TemplateHookExtension(Extension):
     """
-    grid = []
-
-    # Figure out how many rows we should have
-    num_rows = int(ceil(float(len(this_list)) / num_cols))
+    Easily loop through a bunch of templates from a template hook.
 
-    for row_num in range(num_rows):
-        slice_min = row_num * num_cols
-        slice_max = (row_num + 1) * num_cols
+    Use:
+      {% template_hook("comment_extras") %}
 
-        row = this_list[slice_min:slice_max]
+    ... will include all templates hooked into the comment_extras section.
+    """
 
-        grid.append(row)
+    tags = set(["template_hook"])
 
-    return grid
+    def parse(self, parser):
+        includes = []
+        expr = parser.parse_expression()
+        lineno = expr.lineno
+        hook_name = expr.args[0].value
 
+        for template_name in get_hook_templates(hook_name):
+            includes.append(
+                parser.parse_import_context(
+                    Include(Const(template_name), True, False, lineno=lineno),
+                    True))
 
-def gridify_cursor(this_cursor, num_cols=5):
-    """
-    Generates a list of lists where each sub-list's length depends on
-    the number of columns in the list
-    """
-    return gridify_list(list(this_cursor), num_cols)
+        return includes