cmsplugin-photologue / cmsplugin_photologue / templatetags /

The cms-2.X branch has multiple heads

Full commit
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
            num = abs(int(num))
        self.num = num

        self.varname = varname

    def render(self, context):
        context[self.varname] = self.model.objects.filter(is_public=True).order_by('-date_added')[:self.num]
        return ''

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 template.TemplateSyntaxError, "get_latest_galleries tag takes exactly three arguments"
    if bits[1] != 'as':
        raise template.TemplateSyntaxError, "second argument to get_latest_galleries tag must be 'as'"

    return LatestPhotologueModelNode(model, num, varname)