Beispiel #1
0
def if_has_tag(parser, token):
    """
    The logic for both ``{% if_has_tag %}`` and ``{% if not_has_tag %}``.

    Checks if all the given tags exist (or not exist if ``negate`` is ``True``)
    and then only parses the branch that will not error due to non-existing
    tags.

    This means that the following is essentially the same as a
    ``{% comment %}`` tag::

      {% if_has_tag non_existing_tag %}
          {% non_existing_tag %}
      {% endif_has_tag %}

    Another example is checking a built-in tag. This will alway render the
    current year and never FAIL::

      {% if_has_tag now %}
          {% now \"Y\" %}
      {% else %}
          FAIL
      {% endif_has_tag %}
    """
    bits = list(token.split_contents())
    if len(bits) < 2:
        raise TemplateSyntaxError("%r takes at least one arguments" % bits[0])
    end_tag = 'end%s' % bits[0]
    has_tag = all([tag in parser.tags for tag in bits[1:]])
    nodelist_true = nodelist_false = CommentNode()
    if has_tag:
        nodelist_true = parser.parse(('else', end_tag))
        token = parser.next_token()
        if token.contents == 'else':
            parser.skip_past(end_tag)
    else:
        while parser.tokens:
            token = parser.next_token()
            if token.token_type == TOKEN_BLOCK and token.contents == end_tag:
                try:
                    return IfNode([(Literal(has_tag), nodelist_true),
                                   (None, nodelist_false)])
                except TypeError:  # < 1.4
                    return IfNode(Literal(has_tag), nodelist_true,
                                  nodelist_false)
            elif token.token_type == TOKEN_BLOCK and token.contents == 'else':
                break
        nodelist_false = parser.parse((end_tag, ))
        token = parser.next_token()
    try:
        return IfNode([(Literal(has_tag), nodelist_true),
                       (None, nodelist_false)])
    except TypeError:  # < 1.4
        return IfNode(Literal(has_tag), nodelist_true, nodelist_false)
def do_permissionif(parser, token):
    """
    Permission if templatetag

    Examples
    --------
    ::

        {% if user has 'blogs.add_article' %}
            <p>This user have 'blogs.add_article' permission</p>
        {% elif user has 'blog.change_article' of object %}
            <p>This user have 'blogs.change_article' permission of {{object}}</p>
        {% endif %}

        {# If you set 'PERMISSION_REPLACE_BUILTIN_IF = False' in settings #}
        {% permission user has 'blogs.add_article' %}
            <p>This user have 'blogs.add_article' permission</p>
        {% elpermission user has 'blog.change_article' of object %}
            <p>This user have 'blogs.change_article' permission of {{object}}</p>
        {% endpermission %}

    """
    bits = token.split_contents()
    ELIF = "el%s" % bits[0]
    ELSE = "else"
    ENDIF = "end%s" % bits[0]

    # {% if ... %}
    bits = bits[1:]
    condition = do_permissionif.Parser(parser, bits).parse()
    nodelist = parser.parse((ELIF, ELSE, ENDIF))
    conditions_nodelists = [(condition, nodelist)]
    token = parser.next_token()

    # {% elif ... %} (repeatable)
    while token.contents.startswith(ELIF):
        bits = token.split_contents()[1:]
        condition = do_permissionif.Parser(parser, bits).parse()
        nodelist = parser.parse((ELIF, ELSE, ENDIF))
        conditions_nodelists.append((condition, nodelist))
        token = parser.next_token()

    # {% else %} (optional)
    if token.contents == ELSE:
        nodelist = parser.parse((ENDIF, ))
        conditions_nodelists.append((None, nodelist))
        token = parser.next_token()

    # {% endif %}
    assert token.contents == ENDIF

    return IfNode(conditions_nodelists)
Beispiel #3
0
def do_has_perm(parser, token):

    # {% check_perm ... %}
    bits = token.split_contents()[1:]
    condition = TemplateCheckPermParser(parser, bits).parse()
    nodelist = parser.parse(('else', 'endcheck_perm'))
    conditions_nodelists = [(condition, nodelist)]
    token = parser.next_token()

    # {% else %} (optional)
    if token.contents == 'else':
        nodelist = parser.parse(('endcheck_perm',))
        conditions_nodelists.append((None, nodelist))
        token = parser.next_token()

    # this tag should end in {% endcheck_perm %}
    assert token.contents == 'endcheck_perm'

    return IfNode(conditions_nodelists)
Beispiel #4
0
 def test_repr(self):
     node = IfNode(conditions_nodelists=[])
     self.assertEqual(repr(node), '<IfNode>')