| Conditions | 4 |
| Total Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | # vim: set fileencoding=utf-8 : |
||
| 76 | @register.tag('permission') |
||
| 77 | def do_permissionif(parser, token): |
||
| 78 | """ |
||
| 79 | Permission if templatetag |
||
| 80 | |||
| 81 | Examples |
||
| 82 | -------- |
||
| 83 | :: |
||
| 84 | |||
| 85 | {% if user has 'blogs.add_article' %} |
||
| 86 | <p>This user have 'blogs.add_article' permission</p> |
||
| 87 | {% elif user has 'blog.change_article' of object %} |
||
| 88 | <p>This user have 'blogs.change_article' permission of {{object}}</p> |
||
| 89 | {% endif %} |
||
| 90 | |||
| 91 | {# If you set 'PERMISSION_REPLACE_BUILTIN_IF = False' in settings #} |
||
| 92 | {% permission user has 'blogs.add_article' %} |
||
| 93 | <p>This user have 'blogs.add_article' permission</p> |
||
| 94 | {% elpermission user has 'blog.change_article' of object %} |
||
| 95 | <p>This user have 'blogs.change_article' permission of {{object}}</p> |
||
| 96 | {% endpermission %} |
||
| 97 | |||
| 98 | """ |
||
| 99 | bits = token.split_contents() |
||
| 100 | ELIF = "el%s" % bits[0] |
||
| 101 | ELSE = "else" |
||
| 102 | ENDIF = "end%s" % bits[0] |
||
| 103 | |||
| 104 | # {% if ... %} |
||
| 105 | bits = bits[1:] |
||
| 106 | condition = do_permissionif.Parser(parser, bits).parse() |
||
| 107 | nodelist = parser.parse((ELIF, ELSE, ENDIF)) |
||
| 108 | conditions_nodelists = [(condition, nodelist)] |
||
| 109 | token = parser.next_token() |
||
| 110 | |||
| 111 | # {% elif ... %} (repeatable) |
||
| 112 | while token.contents.startswith(ELIF): |
||
| 113 | bits = token.split_contents()[1:] |
||
| 114 | condition = do_permissionif.Parser(parser, bits).parse() |
||
| 115 | nodelist = parser.parse((ELIF, ELSE, ENDIF)) |
||
| 116 | conditions_nodelists.append((condition, nodelist)) |
||
| 117 | token = parser.next_token() |
||
| 118 | |||
| 119 | # {% else %} (optional) |
||
| 120 | if token.contents == ELSE: |
||
| 121 | nodelist = parser.parse((ENDIF,)) |
||
| 122 | conditions_nodelists.append((None, nodelist)) |
||
| 123 | token = parser.next_token() |
||
| 124 | |||
| 125 | # {% endif %} |
||
| 126 | assert token.contents == ENDIF |
||
| 127 | |||
| 128 | return IfNode(conditions_nodelists) |
||
| 129 | do_permissionif.Parser = TemplatePermissionIfParser |
||
| 145 |