| Conditions | 15 |
| Total Lines | 64 |
| Lines | 64 |
| Ratio | 100 % |
| Changes | 1 | ||
| 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:
Complex classes like AuthorPermissionLogic.has_perm() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # coding=utf-8 |
||
| 70 | def has_perm(self, user_obj, perm, obj=None): |
||
| 71 | """ |
||
| 72 | Check if user have permission (of object) |
||
| 73 | |||
| 74 | If the user_obj is not authenticated, it return ``False``. |
||
| 75 | |||
| 76 | If no object is specified, it return ``True`` when the corresponding |
||
| 77 | permission was specified to ``True`` (changed from v0.7.0). |
||
| 78 | This behavior is based on the django system. |
||
| 79 | https://code.djangoproject.com/wiki/RowLevelPermissions |
||
| 80 | |||
| 81 | If an object is specified, it will return ``True`` if the user is |
||
| 82 | specified in ``field_name`` of the object (e.g. ``obj.author``). |
||
| 83 | So once user create an object and the object store who is the author in |
||
| 84 | ``field_name`` attribute (default: ``author``), the author can change |
||
| 85 | or delete the object (you can change this behavior to set |
||
| 86 | ``any_permission``, ``change_permissino`` or ``delete_permission`` |
||
| 87 | attributes of this instance). |
||
| 88 | |||
| 89 | Parameters |
||
| 90 | ---------- |
||
| 91 | user_obj : django user model instance |
||
| 92 | A django user model instance which be checked |
||
| 93 | perm : string |
||
| 94 | `app_label.codename` formatted permission string |
||
| 95 | obj : None or django model instance |
||
| 96 | None or django model instance for object permission |
||
| 97 | |||
| 98 | Returns |
||
| 99 | ------- |
||
| 100 | boolean |
||
| 101 | Whether the specified user have specified permission (of specified |
||
| 102 | object). |
||
| 103 | """ |
||
| 104 | if not is_authenticated(user_obj): |
||
| 105 | return False |
||
| 106 | # construct the permission full name |
||
| 107 | change_permission = self.get_full_permission_string('change') |
||
| 108 | delete_permission = self.get_full_permission_string('delete') |
||
| 109 | # check if the user is authenticated |
||
| 110 | if obj is None: |
||
| 111 | # object permission without obj should return True |
||
| 112 | # Ref: https://code.djangoproject.com/wiki/RowLevelPermissions |
||
| 113 | if self.any_permission: |
||
| 114 | return True |
||
| 115 | if self.change_permission and perm == change_permission: |
||
| 116 | return True |
||
| 117 | if self.delete_permission and perm == delete_permission: |
||
| 118 | return True |
||
| 119 | return False |
||
| 120 | elif user_obj.is_active: |
||
| 121 | # get author instance |
||
| 122 | author = field_lookup(obj, self.field_name) |
||
| 123 | if author == user_obj: |
||
| 124 | if self.any_permission: |
||
| 125 | # have any kind of permissions to the obj |
||
| 126 | return True |
||
| 127 | if (self.change_permission and |
||
| 128 | perm == change_permission): |
||
| 129 | return True |
||
| 130 | if (self.delete_permission and |
||
| 131 | perm == delete_permission): |
||
| 132 | return True |
||
| 133 | return False |
||
| 134 |