| Conditions | 19 |
| Total Lines | 63 |
| Lines | 63 |
| 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 StaffPermissionLogic.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 |
||
| 69 | View Code Duplication | def has_perm(self, user_obj, perm, obj=None): |
|
|
|
|||
| 70 | """ |
||
| 71 | Check if user have permission (of object) |
||
| 72 | |||
| 73 | If the user_obj is not authenticated, it return ``False``. |
||
| 74 | |||
| 75 | If no object is specified, it return ``True`` when the corresponding |
||
| 76 | permission was specified to ``True`` (changed from v0.7.0). |
||
| 77 | This behavior is based on the django system. |
||
| 78 | https://code.djangoproject.com/wiki/RowLevelPermissions |
||
| 79 | |||
| 80 | If an object is specified, it will return ``True`` if the user is |
||
| 81 | staff. The staff can add, change or delete the object (you can change |
||
| 82 | this behavior to set ``any_permission``, ``add_permission``, |
||
| 83 | ``change_permission``, or ``delete_permission`` attributes of this |
||
| 84 | instance). |
||
| 85 | |||
| 86 | Parameters |
||
| 87 | ---------- |
||
| 88 | user_obj : django user model instance |
||
| 89 | A django user model instance which be checked |
||
| 90 | perm : string |
||
| 91 | `app_label.codename` formatted permission string |
||
| 92 | obj : None or django model instance |
||
| 93 | None or django model instance for object permission |
||
| 94 | |||
| 95 | Returns |
||
| 96 | ------- |
||
| 97 | boolean |
||
| 98 | Weather the specified user have specified permission (of specified |
||
| 99 | object). |
||
| 100 | """ |
||
| 101 | if not is_authenticated(user_obj): |
||
| 102 | return False |
||
| 103 | # construct the permission full name |
||
| 104 | add_permission = self.get_full_permission_string('add') |
||
| 105 | change_permission = self.get_full_permission_string('change') |
||
| 106 | delete_permission = self.get_full_permission_string('delete') |
||
| 107 | if obj is None: |
||
| 108 | if user_obj.is_staff: |
||
| 109 | if self.add_permission and perm == add_permission: |
||
| 110 | return True |
||
| 111 | if self.change_permission and perm == change_permission: |
||
| 112 | return True |
||
| 113 | if self.delete_permission and perm == delete_permission: |
||
| 114 | return True |
||
| 115 | return self.any_permission |
||
| 116 | return False |
||
| 117 | elif user_obj.is_active: |
||
| 118 | if user_obj.is_staff: |
||
| 119 | if self.any_permission: |
||
| 120 | # have any kind of permissions to the obj |
||
| 121 | return True |
||
| 122 | if (self.add_permission and |
||
| 123 | perm == add_permission): |
||
| 124 | return True |
||
| 125 | if (self.change_permission and |
||
| 126 | perm == change_permission): |
||
| 127 | return True |
||
| 128 | if (self.delete_permission and |
||
| 129 | perm == delete_permission): |
||
| 130 | return True |
||
| 131 | return False |
||
| 132 |