GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 194382...d1a5fb )
by Lambda
01:38
created

TemplatePermissionIfParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create_var() 0 2 1
A __init__() 0 3 1
1
# vim: set fileencoding=utf-8 :
2
"""
3
permissionif templatetag
4
"""
5
import django
6
from django import template
7
from django.template import TemplateSyntaxError
8
from django.template import VariableDoesNotExist
9
from django.template.smartif import infix
10
from django.template.smartif import IfParser
11
from django.template.smartif import OPERATORS
12
from django.template.defaulttags import Node
13
from django.template.defaulttags import NodeList
14
from django.template.defaulttags import TemplateLiteral
15
from permission.conf import settings
16
from permission.templatetags.patch import IfNode
17
from permission.templatetags.patch import parser_patch
18
19
20
register = template.Library()
21
22
23
def of_operator(context, x, y):
24
    """
25
    'of' operator of permission if
26
27
    This operator is used to specify the target object of permission
28
    """
29
    return x.eval(context), y.eval(context)
30
31
def has_operator(context, x, y):
32
    """
33
    'has' operator of permission if
34
35
    This operator is used to specify the user object of permission
36
    """
37
    user = x.eval(context)
38
    perm = y.eval(context)
39
    if isinstance(perm, (list, tuple)):
40
        perm, obj = perm
41
    else:
42
        obj = None
43
    return user.has_perm(perm, obj)
44
45
# Add 'of' and 'has' operator to existing operators
46
EXTRA_OPERATORS = {
47
    'of': infix(20, of_operator),
48
    'has': infix(10, has_operator),
49
}
50
EXTRA_OPERATORS.update(OPERATORS)
51
for key, op in EXTRA_OPERATORS.items():
52
    op.id = key
53
54
55
class PermissionIfParser(IfParser):
56
    """Permission if parser"""
57
    OPERATORS = EXTRA_OPERATORS
58
    """use extra operator"""
59
60
    def translate_token(self, token):
61
        try:
62
            # use own operators instead of builtin operators
63
            op = self.OPERATORS[token]
64
        except (KeyError, TypeError):
65
            return self.create_var(token)
66
        else:
67
            return op()
68
69
class TemplatePermissionIfParser(PermissionIfParser):
70
    error_class = TemplateSyntaxError
71
72
    def __init__(self, parser, *args, **kwargs):
73
        self.template_parser = parser
74
        super(TemplatePermissionIfParser, self).__init__(*args, **kwargs)
75
76
    def create_var(self, value):
77
        return TemplateLiteral(self.template_parser.compile_filter(value), value)
78
79
80
@register.tag('permission')
81
def do_permissionif(parser, token):
82
    """
83
    Permission if templatetag
84
85
    Examples
86
    --------
87
    ::
88
89
        {% if user has 'blogs.add_article' %}
90
            <p>This user have 'blogs.add_article' permission</p>
91
        {% elif user has 'blog.change_article' of object %}
92
            <p>This user have 'blogs.change_article' permission of {{object}}</p>
93
        {% endif %}
94
95
        {# If you set 'PERMISSION_REPLACE_BUILTIN_IF = False' in settings #}
96
        {% permission user has 'blogs.add_article' %}
97
            <p>This user have 'blogs.add_article' permission</p>
98
        {% elpermission user has 'blog.change_article' of object %}
99
            <p>This user have 'blogs.change_article' permission of {{object}}</p>
100
        {% endpermission %}
101
102
    """
103
    # patch parser for django 1.3.1
104
    parser = parser_patch(parser)
105
106
    bits = token.split_contents()
107
    ELIF = "el%s" % bits[0]
108
    ELSE = "else"
109
    ENDIF = "end%s" % bits[0]
110
111
    # {% if ... %}
112
    bits = bits[1:]
113
    condition = do_permissionif.Parser(parser, bits).parse()
114
    nodelist = parser.parse((ELIF, ELSE, ENDIF))
115
    conditions_nodelists = [(condition, nodelist)]
116
    token = parser.next_token()
117
118
    # {% elif ... %} (repeatable)
119
    while token.contents.startswith(ELIF):
120
        bits = token.split_contents()[1:]
121
        condition = do_permissionif.Parser(parser, bits).parse()
122
        nodelist = parser.parse((ELIF, ELSE, ENDIF))
123
        conditions_nodelists.append((condition, nodelist))
124
        token = parser.next_token()
125
126
    # {% else %} (optional)
127
    if token.contents == ELSE:
128
        nodelist = parser.parse((ENDIF,))
129
        conditions_nodelists.append((None, nodelist))
130
        token = parser.next_token()
131
132
    # {% endif %}
133
    assert token.contents == ENDIF
134
135
    return IfNode(conditions_nodelists)
136
do_permissionif.Parser = TemplatePermissionIfParser
137
138
# To replace builtin if
139
if django.VERSION >= (1, 4):
140
    def replace_builtin_if(replace=False):
141
        if replace:
142
            OPERATORS.update({
143
                'has': EXTRA_OPERATORS['has'],
144
                'of': EXTRA_OPERATORS['of'],
145
            })
146
        else:
147
            # remove of, has from OPERATORS
148
            OPERATORS.pop('of', None)
149
            OPERATORS.pop('has', None)
150
else:
151
    # Django 1.3 and earlier use a different if model
152
    # So re-register if
153
    def replace_builtin_if(replace):
154
        if replace:
155
            register.tag('if', do_permissionif)
156
        else:
157
            from django.template.defaulttags import do_if
158
            register.tag('if', do_if)
159
160
replace_builtin_if(settings.PERMISSION_REPLACE_BUILTIN_IF)
161