Conditions | 8 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 | # coding=utf-8 |
||
28 | def has_perm(self, user_obj, perm, obj=None): |
||
29 | """ |
||
30 | Check if user have permission (of object) based on registered handlers. |
||
31 | |||
32 | It will raise ``ObjectDoesNotExist`` exception when the specified |
||
33 | string permission does not exist and |
||
34 | ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings`` |
||
35 | module. |
||
36 | |||
37 | Parameters |
||
38 | ---------- |
||
39 | user_obj : django user model instance |
||
40 | A django user model instance which be checked |
||
41 | perm : string |
||
42 | `app_label.codename` formatted permission string |
||
43 | obj : None or django model instance |
||
44 | None or django model instance for object permission |
||
45 | |||
46 | Returns |
||
47 | ------- |
||
48 | boolean |
||
49 | Whether the specified user have specified permission (of specified |
||
50 | object). |
||
51 | |||
52 | Raises |
||
53 | ------ |
||
54 | django.core.exceptions.ObjectDoesNotExist |
||
55 | If the specified string permission does not exist and |
||
56 | ``PERMISSION_CHECK_PERMISSION_PRESENCE`` is ``True`` in ``settings`` |
||
57 | module. |
||
58 | """ |
||
59 | if settings.PERMISSION_CHECK_PERMISSION_PRESENCE: |
||
60 | # get permission instance from string permission (perm) |
||
61 | # it raise ObjectDoesNotExists when the permission is not exists |
||
62 | try: |
||
63 | perm_to_permission(perm) |
||
64 | except AttributeError: |
||
65 | # Django 1.2 internally use wrong permission string thus ignore |
||
66 | pass |
||
67 | |||
68 | # get permission handlers fot this perm |
||
69 | cache_name = '_%s_cache' % perm |
||
70 | if hasattr(self, cache_name): |
||
71 | handlers = getattr(self, cache_name) |
||
72 | else: |
||
73 | handlers = [h for h in registry.get_handlers() |
||
74 | if perm in h.get_supported_permissions()] |
||
75 | setattr(self, cache_name, handlers) |
||
76 | for handler in handlers: |
||
77 | if handler.has_perm(user_obj, perm, obj=obj): |
||
78 | return True |
||
79 | return False |
||
80 | |||
122 |