Ticket #8087: django-ifin-templatetag.patch

File django-ifin-templatetag.patch, 5.4 KB (added by Michał Sałaban, 16 years ago)
  • django/template/defaulttags.py

     
    1515from django.utils.encoding import smart_str, smart_unicode
    1616from django.utils.itercompat import groupby
    1717from django.utils.safestring import mark_safe
     18from django.db.models.query import QuerySet
    1819
    1920register = Library()
    2021
     
    258259        and_ = 0,
    259260        or_ = 1
    260261
     262
     263class IfInNode(Node):
     264    def __init__(self, nodelist_in, nodelist_not_in, element_var, sequence_var):
     265        self.nodelist_in = nodelist_in
     266        self.nodelist_not_in = nodelist_not_in
     267        self.element_var = Variable(element_var)
     268        self.sequence_var = Variable(sequence_var)
     269
     270    def render(self, context):
     271        try:
     272            element = self.element_var.resolve(context)
     273            sequence = self.sequence_var.resolve(context)
     274        except VariableDoesNotExist:
     275            return self.nodelist_not_in.render(context)
     276        if isinstance(sequence, QuerySet):
     277            # QuerySets are checked at the database level,
     278            # by comparing primary keys
     279            if not hasattr(element, 'pk'):
     280                return self.nodelist_not_in.render(context)
     281            if sequence.filter(pk=element.pk).count() > 0:
     282                return self.nodelist_in.render(context)
     283            else:
     284                return self.nodelist_not_in.render(context)
     285        if (element in sequence):
     286            return self.nodelist_in.render(context)
     287        else:
     288            return self.nodelist_not_in.render(context)
     289
     290
    261291class RegroupNode(Node):
    262292    def __init__(self, target, expression, var_name):
    263293        self.target, self.expression = target, expression
     
    815845ifchanged = register.tag(ifchanged)
    816846
    817847#@register.tag
     848def ifin(parser, token):
     849    """
     850    Checks if element is present in the sequence.
     851
     852    {% ifin element sequence %}
     853        Element is in sequence
     854    {% else %}
     855        Element is in sequence... not!
     856    {% endifin %}
     857    """
     858    try:
     859        tag_name, element_var, sequence_var = token.split_contents()
     860    except ValueError:
     861        raise TemplateSyntaxError, \
     862                "%r tag requires two arguments: element and sequence" % token.contents.split()[0]
     863    nodelist_in = parser.parse(('endifin', 'else'))
     864    token = parser.next_token()
     865    if token.contents == 'else':
     866        nodelist_not_in = parser.parse(('endifin',))
     867        parser.delete_first_token()
     868    else:
     869        nodelist_not_in = NodeList()
     870    return IfInNode(nodelist_in, nodelist_not_in, element_var, sequence_var)
     871ifin = register.tag(ifin)
     872
     873#@register.tag
     874def ifnotin(parser, token):
     875    """
     876    Checks if element is not present in the sequence.
     877
     878    {% ifnotin element sequence %}
     879        Element is in sequence... not!
     880    {% else %}
     881        Element is in sequence
     882    {% endifnotin %}
     883    """
     884    try:
     885        tag_name, element_var, sequence_var = token.split_contents()
     886    except ValueError:
     887        raise TemplateSyntaxError, \
     888                "%r tag requires two arguments: element and sequence" % token.contents.split()[0]
     889    nodelist_not_in = parser.parse(('endifnotin','else'))
     890    token = parser.next_token()
     891    if token.contents == 'else':
     892        nodelist_in = parser.parse(('endifnotin',))
     893        parser.delete_first_token()
     894    else:
     895        nodelist_in = NodeList()
     896    return IfInNode(nodelist_in, nodelist_not_in, element_var, sequence_var)
     897ifnotin = register.tag(ifnotin)
     898
     899#@register.tag
    818900def ssi(parser, token):
    819901    """
    820902    Outputs the contents of a given file into the page.
  • tests/regressiontests/templates/tests.py

     
    617617            'ifnotequal03': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
    618618            'ifnotequal04': ("{% ifnotequal a b %}yes{% else %}no{% endifnotequal %}", {"a": 1, "b": 1}, "no"),
    619619
     620            ### IFIN TAG ########################################################
     621            'ifin01': ("{% ifin a b %}yes{% endifin %}", {"a": 1, "b": [1, 'b', 3]}, "yes"),
     622            'ifin02': ("{% ifin a b %}yes{% endifin %}", {"a": 1, "b": ['a', 'b', 3]}, ""),
     623            'ifin03': ("{% ifin a b %}yes{% else %}no{% endifin %}", {"a": 1, "b": [1, 'b', 3]}, "yes"),
     624            'ifin04': ("{% ifin a b %}yes{% else %}no{% endifin %}", {"a": 1, "b": ['a', 'b', 3]}, "no"),
     625
     626            ### IFNOTIN TAG ########################################################
     627            'ifnotin01': ("{% ifnotin a b %}yes{% endifnotin %}", {"a": 1, "b": [1, 'b', 3]}, ""),
     628            'ifnotin02': ("{% ifnotin a b %}yes{% endifnotin %}", {"a": 1, "b": ['a', 'b', 3]}, "yes"),
     629            'ifnotin03': ("{% ifnotin a b %}yes{% else %}no{% endifnotin %}", {"a": 1, "b": [1, 'b', 3]}, "no"),
     630            'ifnotin04': ("{% ifnotin a b %}yes{% else %}no{% endifnotin %}", {"a": 1, "b": ['a', 'b', 3]}, "yes"),
     631
    620632            ### INCLUDE TAG ###########################################################
    621633            'include01': ('{% include "basic-syntax01" %}', {}, "something cool"),
    622634            'include02': ('{% include "basic-syntax02" %}', {'headline': 'Included'}, "Included"),
Back to Top