Commits

tehfink committed 953b1ae

added two cmsplugin-photologue templatetags

Comments (0)

Files changed (2)

cmsplugin_photologue/templatetags/__init__.py

Empty file added.

cmsplugin_photologue/templatetags/cmsplugin_photologue_tags.py

+from django import template
+from django.db.models import get_model
+
+register = template.Library()
+
+
+class LatestPhotologueModelNode(template.Node):
+    """
+    parses & checks tagnames and arguments passed to ``get_latest_photologue_model``; returns applicable Queryset of recently added Photologue models
+    """
+
+    def __init__(self, model, num, varname):
+        model, num, varname
+
+        #depending on the name of the templatetag, choose the appropriate photologue model
+        if model == 'galleries':
+            self.model = get_model('photologue', 'Gallery')
+
+        if model == 'photos':
+            self.model = get_model('photologue', 'Photo')
+
+        #'All' means slicing with [:None] , which returns everything
+        if num == 'All' or num =='all':
+            num = None
+        self.num = num
+
+        #ensure the slice is not negative, which might return a DataError
+        try:
+            if int(num) < 0:
+                num = 0
+        except TypeError:
+            num = 0
+
+        self.varname = varname
+
+    def render(self, context):
+        context[self.varname] = self.model.objects.filter(is_public=True).order_by('-date_added')[:self.num]
+        return ''
+
+
+@register.tag('get_latest_galleries')
+@register.tag('get_latest_photos')
+def get_latest_photologue_model(parser, token):
+    """
+    A django templatetag for returning recently added Photologue Photo or Gallery models.
+
+    Some common case examples::
+
+        {% get_latest_galleries All as latest_galleries %}
+        {% for latest in latest_galleries %}
+            ...
+        {% endfor %}
+
+        {% get_latest_photos 5 as latest_photos %}
+        {% for latest in latest_photos %}
+            ...
+        {% endfor %}
+
+    Supported tag names are: ``get_latest_galleries`` or ``get_latest_photos``
+    Supported arguments are: ``All``, ``all``, positive integers, and zero
+    """
+
+    #split up arguments
+    bits = token.split_contents()
+
+    #knock off the first argument
+    model = bits.pop(0).split('_')[-1:][0]
+    num = bits[0]
+    varname = bits[2]
+
+    if len(bits) != 3:
+        raise TemplateSyntaxError, "get_latest_galleries tag takes exactly three arguments"
+    if bits[1] != 'as':
+        raise TemplateSyntaxError, "second argument to get_latest_galleries tag must be 'as'"
+
+    return LatestPhotologueModelNode(model, num, varname)