| 262 | |
| 263 | class 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 | |
| 848 | def 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) |
| 871 | ifin = register.tag(ifin) |
| 872 | |
| 873 | #@register.tag |
| 874 | def 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) |
| 897 | ifnotin = register.tag(ifnotin) |
| 898 | |
| 899 | #@register.tag |