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