| Conditions | 13 |
| Total Lines | 75 |
| Code Lines | 55 |
| 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 bot.management.commands._states.VerifiedState.run_actions() 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 | import random |
||
| 128 | def run_actions(self) -> None: |
||
| 129 | """Выполняет характерные для определенного состояния действия""" |
||
| 130 | |||
| 131 | #: bool: Флаг выполнения команды /create |
||
| 132 | is_create_command = self.__chat_state.is_create_command |
||
| 133 | |||
| 134 | if self.__chat_msg == '/create': |
||
| 135 | self.__chat_state.is_create_command = True |
||
| 136 | self.__chat_state.save(update_fields=('is_create_command',)) |
||
| 137 | |||
| 138 | categories: list = Category.objects.all().filter( |
||
| 139 | user_id=self._tg_user.user_id, |
||
| 140 | is_deleted=False |
||
| 141 | ).values_list('title', flat=True) |
||
| 142 | |||
| 143 | self._send_message( |
||
| 144 | text=self._messages['select_category'] + '\n'.join( |
||
| 145 | categories) if categories |
||
| 146 | else '[categories not found]' |
||
| 147 | ) |
||
| 148 | |||
| 149 | if not is_create_command: |
||
| 150 | if self.__chat_msg not in self._allowed_commands: |
||
| 151 | self._send_message( |
||
| 152 | text=self._messages['unknown_command'] + self._messages[ |
||
| 153 | 'allowed_commands']) |
||
| 154 | |||
| 155 | if self.__chat_msg == '/start': |
||
| 156 | self._send_message(text=self._messages['allowed_commands']) |
||
| 157 | |||
| 158 | if self.__chat_msg == '/goals': |
||
| 159 | goals: list = Goal.objects.all().filter( |
||
| 160 | category__board__participants__user_id=self._tg_user.user_id, |
||
| 161 | category__is_deleted=False, |
||
| 162 | status__lt=Goal.Status.archived, |
||
| 163 | ).values_list('title', flat=True) |
||
| 164 | |||
| 165 | self._send_message( |
||
| 166 | text='\n'.join(goals) if goals else '[goals not found]') |
||
| 167 | |||
| 168 | elif self.__chat_msg == '/cancel': |
||
| 169 | self.__chat_state.set_default() |
||
| 170 | self._send_message(text=self._messages['successful']) |
||
| 171 | |||
| 172 | else: |
||
| 173 | if not self.__chat_state.category_id: |
||
| 174 | categories: list = Category.objects.all().filter( |
||
| 175 | user_id=self._tg_user.user_id, |
||
| 176 | is_deleted=False |
||
| 177 | ).values_list('title', flat=True) |
||
| 178 | |||
| 179 | if self.__chat_msg not in categories: |
||
| 180 | self._send_message( |
||
| 181 | text=self._messages['select_category'] + '\n'.join( |
||
| 182 | categories) if categories |
||
| 183 | else '[categories not found]' |
||
| 184 | ) |
||
| 185 | else: |
||
| 186 | category = Category.objects.all().filter( |
||
| 187 | user_id=self._tg_user.user_id).get( |
||
| 188 | title=self.__chat_msg) |
||
| 189 | self.__chat_state.category_id = category.id |
||
| 190 | self.__chat_state.save(update_fields=('category_id',)) |
||
| 191 | self._send_message(text=self._messages['goal_title']) |
||
| 192 | else: |
||
| 193 | goal = Goal.objects.create( |
||
| 194 | user_id=self._tg_user.user_id, |
||
| 195 | category_id=self.__chat_state.category_id, |
||
| 196 | title=self.__chat_msg |
||
| 197 | ) |
||
| 198 | if goal.id: |
||
| 199 | self.__chat_state.set_default() |
||
| 200 | self._send_message(text=self._messages['successful']) |
||
| 201 | else: |
||
| 202 | self._send_message(text=self._messages['failure']) |
||
| 203 |