@@ -1,12 +1,6 @@ -from authentication.exceptions.business_host_exceptions.base_already import ( - BaseAlready, -) +from django.utils.translation import gettext as _ -class AlreadyAccount(BaseAlready): - def msg(self) -> dict: - return dict( - message='user is already a business account for other company', - user_id=self.user.uid, - parent_company=self.user.business_account.parent_company.uid, - ) +class AlreadyAccount(Exception): + def __str__(self): + return _('User is already a business account for another company') @@ -1,12 +1,6 @@ -from authentication.exceptions.business_host_exceptions.base_already import ( - BaseAlready, -) +from django.utils.translation import gettext as _ -class AlreadyHost(BaseAlready): - def msg(self) -> dict: - return dict( - message='user already has a host account', - user_id=self.user.uid, - company_id=self.user.host_account.uid, - ) +class AlreadyHost(Exception): + def __str__(self): + return _('User already has a host account') @@ -1,12 +0,0 @@ -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - # TODO: убрать кринж на рефакторинге - from authentication.models import CustomUserModel - - -class BaseAlready(Exception): - def __init__(self, user: 'CustomUserModel'): - self.user = user - - def msg(self): ... @@ -1,8 +1,4 @@ -from authentication.exceptions.business_host_exceptions.base_already import ( - BaseAlready, -) - -__all__ = ('BaseAlready',) +__all__ = () class InvalidPassword(Exception): ... @@ -8,7 +8,6 @@ from rest_framework.request import Request from authentication.exceptions import business_host_exceptions from authentication.exceptions.business_account import BusinessAccountNotFound, AdminReinviteForbidden from authentication.exceptions.business_host_exceptions import AlreadyAccount, InviteeHasPlan -from authentication.exceptions.business_host_exceptions.access_denied import AccessDenied from authentication.exceptions.business_host_exceptions.already_host import ( AlreadyHost, ) @@ -87,12 +86,12 @@ class BusinessHostService: ) -> BusinessAccountService: try: user = UserSelector.get_by_email(email.lower()) - if AccountStatusSelector(user).is_business_host() and not user.is_deleted: - raise AlreadyHost(user) - if AccountStatusSelector(user).is_business_account() and not user.is_deleted: - raise AlreadyAccount(user) + if user.host: + raise AlreadyHost + if user.employee and user.employee.parent_company != self.user.host: + raise AlreadyAccount if PaymentPlanSelector(user).is_plan_paid(): - raise InviteeHasPlan(user) + raise InviteeHasPlan except CustomUserModel.DoesNotExist: return self.create_account( email=email, @@ -213,13 +212,13 @@ class BusinessHostService: serializer.is_valid(raise_exception=True) if PaymentPlanSelector(self.user).is_plan_paid(): - raise business_host_exceptions.AlreadyHasPlan(self.user) + raise business_host_exceptions.AlreadyHasPlan() if AccountStatusSelector(self.user).is_business_host(): - raise business_host_exceptions.AlreadyHost(self.user) + raise business_host_exceptions.AlreadyHost() if AccountStatusSelector(self.user).is_business_account(): - raise business_host_exceptions.AlreadyAccount(self.user) + raise business_host_exceptions.AlreadyAccount() host = BusinessUserHost.objects.create(user=self.user, **serializer.validated_data) host.save() @@ -22,7 +22,12 @@ from rest_framework.request import Request from rest_framework.response import Response from rest_framework.views import APIView -from authentication.exceptions import BaseAlready +from authentication.exceptions.business_host_exceptions import ( + AlreadyAccount, + AlreadyHasPlan, + AlreadyHost, + InviteeHasPlan, +) from authentication.exceptions.business_account import AdminReinviteForbidden from authentication.exceptions.business_host_exceptions.not_allowed_ip import ( NotAllowedIP, @@ -297,8 +302,8 @@ class BusinessHostAPIView(APIView): try: result = BusinessHostService(self.request.user).create(request) return Response(result.data, status=status.HTTP_201_CREATED) - except BaseAlready as err: - return Response(err.msg(), status=status.HTTP_400_BAD_REQUEST) + except (AlreadyAccount, AlreadyHost, AlreadyHasPlan, InviteeHasPlan) as err: + return Response({'detail': str(err)}, status=status.HTTP_400_BAD_REQUEST) except Exception as err: return Response({'detail': f'{err}'}, status=status.HTTP_400_BAD_REQUEST) @@ -569,10 +574,10 @@ class StartHostRegistrationAPIView(APIView): try: response = BusinessHostService(self.request.user).create_host(request) return Response(response.data, status=status.HTTP_201_CREATED) - except BaseAlready as err: - return Response(err.msg(), status=status.HTTP_400_BAD_REQUEST) - except Exception as err: - return Response({'detail': str(err)}, status=status.HTTP_400_BAD_REQUEST) + except (AlreadyAccount, AlreadyHost, AlreadyHasPlan, InviteeHasPlan) as exc: + return Response({'detail': str(exc)}, status=status.HTTP_400_BAD_REQUEST) + except Exception as exc: + return Response({'detail': str(exc)}, status=status.HTTP_400_BAD_REQUEST) class AllowedHostModelsAPIView(APIView): @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2026-01-15 12:49+0300\n" +"POT-Creation-Date: 2026-01-15 16:04+0300\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -29,11 +29,15 @@ msgid "You cannot reinvite other administrators" msgstr "Вы не можете повторно приглашать администраторов" #: authentication/exceptions/business_host_exceptions/access_denied.py:6 -#: authentication/services/business_account_service.py:104 -#: authentication/services/business_account_service.py:107 +#: authentication/services/business_account_service.py:103 +#: authentication/services/business_account_service.py:106 msgid "You do not have sufficient rights to perform this action" msgstr "У вас недостаточно прав для выполнения этого действия" +#: authentication/exceptions/business_host_exceptions/already_account.py:6 +msgid "User is already a business account for another company" +msgstr "Пользователь уже является сотрудником другой компании" + #: authentication/exceptions/business_host_exceptions/already_has_plan.py:7 msgid "" "You already have an active tariff plan. You must request a cancellation of " @@ -54,6 +58,10 @@ msgstr "" "запросить аннулирование текущего тарифного плана (через кнопку \"Сообщить об " "ошибке\"), после чего у вас появится возможность его пригласить" +#: authentication/exceptions/business_host_exceptions/already_host.py:6 +msgid "User already has a host account" +msgstr "Пользователь уже имеет корпоративный аккаунт" + #: authentication/exceptions/business_host_exceptions/not_allowed_ip.py:6 msgid "Current IP not allowed in this context" msgstr "Текущий IP не разрешен в данном контексте" @@ -496,17 +504,17 @@ msgstr "Не найден пользователь с данным ID" msgid "BusinessAccount for this user doesn't exist" msgstr "Бизнес-аккаунт для данного юзера не найден" -#: authentication/services/business_account_service.py:75 +#: authentication/services/business_account_service.py:74 msgid "Invited account can either accept or reject an invitation" msgstr "Приглашенный аккаунт может принять или отклонить приглашение" -#: authentication/services/business_account_service.py:101 +#: authentication/services/business_account_service.py:100 #, fuzzy #| msgid "Passwords do not match" msgid "Passwords don't match" msgstr "Пароли не совпадают" -#: authentication/services/business_account_service.py:110 +#: authentication/services/business_account_service.py:109 msgid "You cannot change the password of an unconfirmed e-mail user." msgstr "Вы не можете изменить пароль неподтвержденного по e-mail пользователя."