Commits

frol committed a70944f

add optional jinja2 support (with coffin)

Comments (0)

Files changed (3)

   with multiple recipient management
 - New message notification (default is 'django-notification')
 - Asynchronous mailer (default is 'django-mailer')
+- Jinja2 (with Coffin) (experimental templates are available in branch frol/experimental_jinja2_templates)
 
 See the docs/ directory for Sphinx documentation.
 For example, build the HTML version with >make html

postman/templatetags/postman_tags_jinja2.py

+from django.conf import settings
+
+if 'coffin' in settings.INSTALLED_APPS:
+    from coffin.template import Library
+    from jinja2 import TemplateSyntaxError
+
+    from postman.models import ORDER_BY_MAPPER, Message
+    from postman.templatetags.postman_tags import OrderByNode
+
+    register = Library()
+
+    @register.object
+    def postman_order_by(request, field_name):
+        try:
+            field_code = ORDER_BY_MAPPER[field_name.lower()]
+        except KeyError:
+            raise TemplateSyntaxError(
+                "'{0}' is not a valid argument to 'postman_order_by' tag."
+                " Must be one of: {1}".format(field_name, ORDER_BY_MAPPER.keys()))
+        order_by = OrderByNode(field_code)
+        return order_by.render({'gets': request.GET})
+
+    @register.object
+    def postman_unread(user):
+        return Message.objects.inbox_unread_count(user)
 from django.core.urlresolvers import reverse
 from django.db.models import Q
 from django.http import Http404
-from django.shortcuts import render_to_response, get_object_or_404, redirect
+from django.shortcuts import get_object_or_404, redirect
 from django.template import RequestContext
 from django.utils.translation import ugettext as _
+
+if 'coffin' in settings.INSTALLED_APPS:
+    from coffin.template.response import TemplateResponse
+else:
+    from django.template.response import TemplateResponse
+
 try:
     from django.utils.timezone import now  # Django 1.4 aware datetimes
 except ImportError:
     if order_by:
         kwargs.update(order_by=order_by)
     msgs = getattr(Message.objects, folder_name)(request.user, **kwargs)
-    return render_to_response(template_name, {
+    return TemplateResponse(request, template_name, {
         'pm_messages': msgs,  # avoid 'messages', already used by contrib.messages
         'by_conversation': option is None,
         'by_message': option == OPTION_MESSAGES,
         'by_message_url': reverse(view_name, args=[OPTION_MESSAGES]),
         'current_url': request.get_full_path(),
         'gets': request.GET,  # useful to postman_order_by template tag
-        }, context_instance=RequestContext(request))
+        })
 
 
 @login_required
             if usernames:
                 initial.update(recipients=', '.join(usernames))
         form = form_class(initial=initial, channel=channel)
-    return render_to_response(template_name, {
+    return TemplateResponse(request, template_name, {
         'form': form,
         'autocompleter_app': autocompleter_app,
         'next_url': request.GET.get('next', next_url),
-        }, context_instance=RequestContext(request))
+        })
 if getattr(settings, 'POSTMAN_DISALLOW_ANONYMOUS', False):
     write = login_required(write)
 
     else:
         initial.update(request.GET.items())  # allow overwriting of the defaults by query string
         form = form_class(initial=initial, channel=autocomplete_channel)
-    return render_to_response(template_name, {
+    return TemplateResponse(request, template_name, {
         'form': form,
         'recipient': parent.obfuscated_sender,
         'autocompleter_app': autocompleter_app,
         'next_url': request.GET.get('next', next_url),
-        }, context_instance=RequestContext(request))
+        })
 
 
 def _view(request, filter, form_class=QuickReplyForm, formatters=(format_subject,format_body),
                 break
         else:
             received = None
-        return render_to_response(template_name, {
+        return TemplateResponse(request, template_name, {
             'pm_messages': msgs,
             'archived': archived,
             'reply_to_pk': received.pk if received else None,
             'form': form_class(initial=received.quote(*formatters)) if received else None,
             'next_url': request.GET.get('next', reverse('postman_inbox')),
-            }, context_instance=RequestContext(request))
+            })
     raise Http404