| Conditions | 12 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 GroupMemberViewSet.give_rights() 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 | from django.http import Http404 |
||
| 133 | @decorators.detail_route(methods=['put']) |
||
| 134 | def give_rights(self, request, pk=None): |
||
| 135 | """ |
||
| 136 | Change the rights of a member of the group pk. |
||
| 137 | --- |
||
| 138 | request_serializer: null |
||
| 139 | response_serializer: GroupMemberSerializer |
||
| 140 | parameters_strategy: |
||
| 141 | form: replace |
||
| 142 | parameters: |
||
| 143 | - name: right_to_change |
||
| 144 | type: string |
||
| 145 | possible values : is_administrator, is_super_administrator, |
||
| 146 | can_invite, can_be_contacted, can_kick, can_modify_group_infos, |
||
| 147 | can_publish |
||
| 148 | required: true |
||
| 149 | """ |
||
| 150 | from sigma_core.models.group import Group |
||
| 151 | try: |
||
| 152 | modified_mship = GroupMember.objects.all().select_related('group').get(pk=pk) |
||
| 153 | my_mship = GroupMember.objects.all().get(group=modified_mship.group, user=request.user) |
||
| 154 | except GroupMember.DoesNotExist: |
||
| 155 | raise Http404() |
||
| 156 | |||
| 157 | right_to_change = request.data.get('right_to_change', None) |
||
| 158 | |||
| 159 | if my_mship.can_modify_basic_rights and right_to_change in basic_rights_string: |
||
| 160 | if right_to_change=="can_invite": |
||
| 161 | modified_mship.can_invite= not modified_mship.can_invite |
||
| 162 | elif right_to_change=="can_kick": |
||
| 163 | modified_mship.can_kick= not modified_mship.can_kick |
||
| 164 | elif right_to_change=="can_publish": |
||
| 165 | modified_mship.can_publish= not modified_mship.can_publish |
||
| 166 | elif right_to_change=="can_modify_group_infos": |
||
| 167 | modified_mship.can_modify_group_infos= not modified_mship.can_modify_group_infos |
||
| 168 | elif right_to_change=="can_be_contacted": |
||
| 169 | modified_mship.can_be_contacted= not modified_mship.can_be_contacted |
||
| 170 | |||
| 171 | elif my_mship.can_promote_admin_or_superadmin: |
||
| 172 | if right_to_change=="is_administrator": |
||
| 173 | modified_mship.is_administrator= not modified_mship.is_administrator |
||
| 174 | else: |
||
| 175 | #there can only be one super admin |
||
| 176 | modified_mship.is_super_administrator=True |
||
| 177 | my_mship.is_super_administrator=False |
||
| 178 | |||
| 179 | if modified_mship.is_administrator: |
||
| 180 | #we give him all the rights (except can_be_contacted, he gets to choose) |
||
| 181 | modified_mship.can_invite=True |
||
| 182 | modified_mship.can_kick=True |
||
| 183 | modified_mship.can_publish=True |
||
| 184 | |||
| 185 | else: |
||
| 186 | return Response(status=status.HTTP_403_FORBIDDEN) |
||
| 187 | |||
| 188 | my_mship.save() |
||
| 189 | modified_mship.save() |
||
| 190 | |||
| 191 | return Response(GroupMemberSerializer(modified_mship).data, status=status.HTTP_200_OK) |
||
| 192 | |||
| 216 |