Source

django-photos / src / photos / templatetags / photos_extras.py

Full commit
from django import template

from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional, Model

register = template.Library()

@tag(register, [Variable(), Variable(), Constant('as'), Name()])
def get_previous(context, album, photo, asvar):
    prev = None
    for tmp in album.photos.all():
        if tmp == photo:
            context[asvar] = prev
            return ''
        else:
            prev = tmp
    
    return ''

@tag(register, [Variable(), Variable(), Constant('as'), Name()])
def get_next(context, album, photo, asvar):
    flag = False
    for tmp in album.photos.all():
        if flag == True:
            context[asvar] = tmp
            return ''
        if tmp == photo:
            flag = True
                
    return ''