Source

cmsplugin-photologue / cmsplugin_photologue / templatetags / cmsplugin_photologue_tags.py

The cms-2.X branch has multiple heads

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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.