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