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