Просмотр исходного кода

Added template middle framework and created added hooks to two templates. #138

Ralfp 12 лет назад
Родитель
Сommit
a14bef06a5

+ 9 - 0
misago/apps/index.py

@@ -76,5 +76,14 @@ def index(request):
                                              'ranks_online': ranks_list,
                                              'users_online': users_online,
                                              'popular_threads': popular_threads,
+                                             'hook_above_forum_home': u'',
+                                             'hook_below_forum_home': u'',
+                                             'hook_above_home_forums_list': u'',
+                                             'hook_below_home_forums_list': u'',
+                                             'hook_above_home_sidepanel': u'',
+                                             'hook_after_home_sidepanel_ranks_online': u'',
+                                             'hook_after_home_sidepanel_popular_threads': u'',
+                                             'hook_after_home_sidepanel_forum_stats': u'',
+                                             'hook_below_home_sidepanel': u'',
                                              },
                                             context_instance=RequestContext(request));

+ 12 - 1
misago/context_processors.py

@@ -4,7 +4,18 @@ from misago.admin import site
 from misago.models import Forum
 
 def common(request):
-    context = {}
+    context = {
+        'hook_primary_menu_prepend': u'',
+        'hook_primary_menu_append': u'',
+        'hook_foot_menu_prepend': u'',
+        'hook_foot_menu_append': u'',
+        'hook_guest_menu_prepend': u'',
+        'hook_guest_menu_append': u'',
+        'hook_user_menu_prepend': u'',
+        'hook_user_menu_append': u'',
+        'hook_credits_side': u'',
+    }
+
     try:
         context.update({
             'acl': request.acl,

+ 3 - 0
misago/settings_base.py

@@ -89,6 +89,9 @@ TEMPLATE_CONTEXT_PROCESSORS = (
     'misago.context_processors.admin',
 )
 
+# Template middlewares
+TEMPLATE_MIDDLEWARES = ()
+
 # Jinja2 Template Extensions
 JINJA2_EXTENSIONS = (
     'jinja2.ext.do',

+ 54 - 8
misago/theme.py

@@ -1,4 +1,5 @@
 from django.conf import settings
+from django.utils.importlib import import_module
 from coffin.shortcuts import render_to_response
 from coffin.template import dict_from_django_context
 from coffin.template.loader import get_template, select_template, render_to_string
@@ -10,9 +11,50 @@ if not hasattr(safestring, '__html__'):
     safestring.SafeUnicode.__html__ = lambda self: unicode(self)
 
 class Theme(object):
+    middlewares = ()
+
     def __init__(self, theme):
         self.set_theme(theme);
 
+        if not self.middlewares:
+            self.load_middlewares(settings.TEMPLATE_MIDDLEWARES)
+
+    def load_middlewares(self, middlewares):
+        for extension in middlewares:
+            module = '.'.join(extension.split('.')[:-1])
+            extension = extension.split('.')[-1]
+            module = import_module(module)
+            middleware = getattr(module, extension)
+            self.middlewares += (middleware(), )
+
+    def merge_contexts(self, dictionary=None, context_instance=None):
+        dictionary = dictionary or {}
+        if context_instance:
+            context_instance.update(dictionary)
+        else:
+            context_instance = dictionary
+        return context_instance
+
+    def process_context(self, templates, context):
+        for middleware in self.middlewares:
+            try:
+                new_context = middleware.process_context(self, templates, context)
+                if new_context:
+                    context = new_context
+            except AttributeError:
+                pass
+        return context
+
+    def process_template(self, templates, context):
+        for middleware in self.middlewares:
+            try:
+                new_templates = middleware.process_template(self, templates, context)
+                if new_templates:
+                    return new_templates
+            except AttributeError:
+                pass
+        return templates
+
     def set_theme(self, theme):
         if theme not in settings.INSTALLED_THEMES:
             raise ValueError('"%s" is not correct theme name.' % theme)
@@ -26,7 +68,8 @@ class Theme(object):
     def get_theme(self):
         return self._theme
 
-    def prefix_templates(self, templates):
+    def prefix_templates(self, templates, dictionary=None):
+        templates = self.process_template(templates, dictionary)
         if isinstance(templates, str):
             return ('%s/%s' % (self._theme, templates), templates)
         else:
@@ -36,16 +79,19 @@ class Theme(object):
             prefixed += templates
             return prefixed
 
-    def render_to_string(self, templates, *args, **kwargs):
-        templates = self.prefix_templates(templates)
-        return render_to_string(templates, *args, **kwargs)
+    def render_to_string(self, templates, dictionary=None, context_instance=None):
+        dictionary = self.process_context(templates, self.merge_contexts(dictionary, context_instance))
+        templates = self.prefix_templates(templates, dictionary)
+        return render_to_string(templates, dictionary)
 
-    def render_to_response(self, templates, *args, **kwargs):
-        templates = self.prefix_templates(templates)
-        return render_to_response(templates, *args, **kwargs)
+    def render_to_response(self, templates, dictionary=None, context_instance=None,
+                       mimetype=None):
+        dictionary = self.process_context(templates, self.merge_contexts(dictionary, context_instance))
+        templates = self.prefix_templates(templates, dictionary)
+        return render_to_response(templates, dictionary=dictionary, mimetype=mimetype)
 
     def macro(self, templates, macro, dictionary={}, context_instance=None):
-        templates = self.prefix_templates(templates)
+        templates = self.prefix_templates(templates, dictionary)
         template = select_template(templates)
         if context_instance:
             context_instance.update(dictionary)

+ 14 - 0
templates/cranefly/index.html

@@ -8,9 +8,11 @@
 {%- endif %}{%- endblock %}
       
 {% block content %}
+{{ hook_above_forum_home|safe }}
 <div class="row">
   <div class="span8">
     <div class="index-forums-list">
+      {{ hook_above_home_forums_list|safe }}
 
       {% for category in forums_list %}{% if category.subforums %}
       <div id="{{ category.slug }}" class="index-category{% if category.style %} index-category-{{ category.style }}{% endif %}">
@@ -82,10 +84,13 @@
       </div>
       {% endif %}{% endfor %}
 
+      {{ hook_below_home_forums_list|safe }}
     </div>
   </div>
   <div class="span4 index-sidebar">
 
+    {{ hook_above_home_sidepanel|safe }}
+
     {% if ranks_online %}
     <div class="index-ranks-list">
       {% for rank in ranks_online %}{% if rank.online %}
@@ -111,6 +116,8 @@
     </div>
     {% endif %}
 
+    {{ hook_after_home_sidepanel_ranks_online|safe }}
+
     {% if popular_threads %}
     <div class="index-popular-threads">
       <h4>{% trans %}Popular Threads{% endtrans %}</h4>
@@ -125,6 +132,8 @@
     </div>
     {% endif %}
 
+    {{ hook_after_home_sidepanel_popular_threads|safe }}
+
     <div class="index-stats">
       <ul class="unstyled">
         <li>
@@ -144,6 +153,8 @@
       </ul>
     </div>
 
+    {{ hook_after_home_sidepanel_forum_stats|safe }}
+
     {% if user.is_authenticated() %}
     <form action="{% url 'read_all' %}" method="post" class="index-forums-read-all">
       <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
@@ -151,8 +162,11 @@
     </form>
     {% endif %}
 
+    {{ hook_below_home_sidepanel|safe }}
+
   </div>
 </div>
+{{ hook_below_forum_home|safe }}
 {% endblock %}
 
 {% block javascripts -%}{{ super() }}

+ 10 - 1
templates/cranefly/layout.html

@@ -38,6 +38,7 @@
         {% endif %}
         <ul class="nav navbar-blocks pull-left">
           <li><a href="{% url 'index' %}" title="{% trans %}Forum Home{% endtrans %}" class="tooltip-bottom"><i class="icon-th-list"></i></a></li>
+          {{ hook_primary_menu_prepend|safe }}
           <li><a href="{% url 'popular_threads' %}" title="{% trans %}Popular Threads{% endtrans %}" class="hot tooltip-bottom"><i class="icon-fire"></i></a></li>
           <li><a href="{% url 'new_threads' %}" title="{% trans %}New Threads{% endtrans %}" class="fresh tooltip-bottom"><i class="icon-leaf"></i></a></li>{% if not user.crawler %}
           {% if not user.is_crawler() %}
@@ -45,11 +46,13 @@
           {% endif %}
           <li><a href="{% url 'users' %}" title="{% trans %}Browse Users{% endtrans %}" class="tooltip-bottom"><i class="icon-user"></i></a></li>
           {% if settings.tos_url or settings.tos_content %}<li><a href="{% if settings.tos_url %}{{ settings.tos_url }}{% else %}{% url 'tos' %}{% endif %}" title="{% if settings.tos_title %}{{ settings.tos_title }}{% else %}{% trans %}Forum Terms of Service{% endtrans %}{% endif %}" class="tooltip-bottom"><i class="icon-certificate"></i></a></li>{% endif %}
+          {{ hook_primary_menu_append|safe }}
         </ul>
         {% if not user.is_crawler() %}
         {% if user.is_authenticated() %}
         <ul class="nav navbar-blocks pull-right">
           <li class="user-profile"><a href="{% url 'user' user=user.id, username=user.username_slug %}" title="{% trans %}Go to your profile{% endtrans %}" class="tooltip-bottom"><div><img src="{{ user.get_avatar(28) }}" alt=""> {{ user.username }}</div></a></li>
+          {{ hook_user_menu_prepend|safe }}
           <li><a href="{% url 'alerts' %}" title="{% if user.alerts %}{% trans %}You have new notifications!{% endtrans %}{% else %}{% trans %}Your Notifications{% endtrans %}{% endif %}" class="tooltip-bottom"><i class="icon-asterisk"></i>{% if user.alerts %}<span class="label label-important">{{ user.alerts }}</span>{% endif %}</a></li>
           {% if settings.enable_private_threads and acl.private_threads.can_participate() %}
           <li><a href="{% url 'private_threads' %}" title="{% if user.unread_pds %}{% trans %}There are unread Private Threads!{% endtrans %}{% else %}{% trans %}Your Private Threads{% endtrans %}{% endif %}" class="tooltip-bottom"><i class="icon-inbox"></i>{% if user.unread_pds %}<span class="label label-important">{{ user.unread_pds }}</span>{% endif %}</a></li>
@@ -57,12 +60,15 @@
           <li><a href="{% url 'newsfeed' %}" title="{% trans %}Your News Feed{% endtrans %}" class="tooltip-bottom"><i class="icon-signal"></i></a></li>
           <li><a href="{% url 'watched_threads' %}" title="{% trans %}Threads you are watching{% endtrans %}" class="tooltip-bottom"><i class="icon-bookmark"></i></a></li>
           <li><a href="{% url 'usercp' %}" title="{% trans %}Edit your profile options{% endtrans %}" class="tooltip-bottom"><i class="icon-cog"></i></a></li>
+          {{ hook_user_menu_append|safe }}
           <li><form action="{% url 'sign_out' %}" method="post"><input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}"><button type="submit" title="{% trans %}Sign Out and browse as guest{% endtrans %}" class="btn btn-link danger tooltip-bottom"><i class="icon-off"></i></button></form></li>
         </ul>
         {% else %}
         <ul class="nav navbar-user-nav pull-right">
+          {{ hook_guest_menu_prepend|safe }}
           <li><a href="{% url 'sign_in' %}" title="{% trans %}Sign In to Your Account{% endtrans %}" class="tooltip-bottom btn btn-danger"><i class="icon-check"></i> {% trans %}Sign In{% endtrans %}</a></li>{% if settings.account_activation != 'block' %}
           <li><a href="{% url 'register' %}" title="{% trans %}Register new account{% endtrans %}" class="tooltip-bottom btn btn-inverse"><i class="icon-edit"></i> {% trans %}Register{% endtrans %}</a></li>{% endif %}
+          {{ hook_guest_menu_append|safe }}
         </ul>
         {% endif %}
         {% endif %}
@@ -85,7 +91,9 @@
   <div class="container">
     <ul class="breadcrumb">
       {% block breadcrumb %}<li class="first"><a href="{% url 'index' %}">{{ settings.board_name }}</a>{% endblock %}</li>
+      {{ hook_foot_menu_prepend|safe }}
       <li class="pull-right"><i class="icon-move"></i> <a href="{% url 'forum_map' %}">{% trans %}Forum Map{% endtrans %}</a></li>
+      {{ hook_foot_menu_append|safe }}
     </ul>
     <hr>
     <div class="credits">
@@ -95,6 +103,7 @@
         {% endif %}
         <a href="http://misago-project.org">This community is powered by Misago forum software by Rafał Pitoń</a>
       </p>
+      {{ hook_html_credits_side|safe }}
     </div>
   </div>
-</footer>{% endblock %}
+</footer>{% endblock %}