| Conditions | 16 |
| Total Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| 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 ChatViewSet.change_role() 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 |
||
| 90 | @decorators.detail_route(methods=['put']) |
||
| 91 | def change_role(self, request, pk=None): |
||
| 92 | """ |
||
| 93 | Change the permissions of user in chat pk. |
||
| 94 | --- |
||
| 95 | omit_serializer: true |
||
| 96 | parameters_strategy: |
||
| 97 | form: replace |
||
| 98 | parameters: |
||
| 99 | - name: chatmember_id |
||
| 100 | type: integer |
||
| 101 | required: true |
||
| 102 | - name: role |
||
| 103 | type: string |
||
| 104 | required: true |
||
| 105 | """ |
||
| 106 | try: |
||
| 107 | chat = Chat.objects.get(pk=pk) |
||
| 108 | chatmember = ChatMember.objects.get(pk=request.data.get('chatmember_id', None)) |
||
| 109 | """ |
||
| 110 | - cannot change the role of the creator |
||
| 111 | - if has quit the conversation, can rejoin, but administrator cannot force it |
||
| 112 | - if not admin, only can REjoin conversation if not banned |
||
| 113 | """ |
||
| 114 | role = request.data.get('role', None) |
||
| 115 | is_admin = request.user.is_chat_admin(chat) |
||
| 116 | not_has_left = chatmember.is_member or chatmember.is_banned |
||
| 117 | has_ragequit = request.user == chatmember.user and not not_has_left |
||
| 118 | may_change = is_admin and not_has_left |
||
| 119 | |||
| 120 | if chatmember.is_creator: |
||
| 121 | return Response(status=status.HTTP_403_FORBIDDEN) |
||
| 122 | |||
| 123 | chatmember.is_admin = False |
||
| 124 | chatmember.is_member = False |
||
| 125 | chatmember.is_banned = False |
||
| 126 | changed = False |
||
| 127 | if role == "admin": |
||
| 128 | if not may_change: |
||
| 129 | return Response(status=status.HTTP_401_UNAUTHORIZED) |
||
| 130 | if has_ragequit: |
||
| 131 | return Response(status=status.HTTP_400_BAD_REQUEST) |
||
| 132 | chatmember.is_admin = True |
||
| 133 | chatmember.is_member = True |
||
| 134 | changed = True |
||
| 135 | if role == "member": |
||
| 136 | if not may_change: |
||
| 137 | if not has_ragequit: |
||
| 138 | return Response(status=status.HTTP_401_UNAUTHORIZED) |
||
| 139 | chatmember.is_member = True |
||
| 140 | changed = True |
||
| 141 | if role == "banned": |
||
| 142 | if not may_change: |
||
| 143 | return Response(status=status.HTTP_401_UNAUTHORIZED) |
||
| 144 | chatmember.is_banned = True |
||
| 145 | changed = True |
||
| 146 | if(role == "leave" and request.user == chatmember.user and not request.user.is_chat_banned(chat)): |
||
| 147 | changed = True |
||
| 148 | if changed: |
||
| 149 | chatmember.save() |
||
| 150 | s = ChatMemberSerializer(chatmember) |
||
| 151 | return Response(s.data, status=status.HTTP_200_OK) |
||
| 152 | return Response("Incorrect role.", status=status.HTTP_400_BAD_REQUEST) |
||
| 153 | |||
| 154 | except Chat.DoesNotExist: |
||
| 155 | raise Http404("Chat {0} not found".format(pk)) |
||
| 156 | except ChatMember.DoesNotExist: |
||
| 157 | raise Http404("ChatMember %d not found" % request.data.get('chatmember_id', None)) |
||
| 158 |