@@ -34,3 +34,9 @@ class UserAlreadyExists(Exception): class DomainNotFound(Exception): def __str__(self): return _('Domain not found') + + +class EmailSendFailed(Exception): + def __str__(self): + return _('Failed to send the email. Verify that the email exists and is available') + @@ -14,7 +14,7 @@ from django.utils.safestring import SafeString from django.utils.translation import gettext_lazy as _ from authentication.exceptions.email_exceptions import LetterNotFound, LetterUnknownException -from authentication.exceptions.user import DomainNotFound +from authentication.exceptions.user import DomainNotFound, EmailSendFailed from authentication.models import BusinessAccount, BusinessUserHost from authentication.models.user import CustomUserModel from authentication.services.email_token_service import EmailTokenService @@ -50,6 +50,8 @@ class EmailService: raise DomainNotFound except smtplib.SMTPRecipientsRefused: raise DomainNotFound + except smtplib.SMTPDataError: + raise EmailSendFailed except Exception as exc: logger.exception(exc) raise Exception(_('Error occured when proceed email sending')) @@ -46,6 +46,7 @@ from authentication.exceptions.email_token import EmailTokenNotFound from authentication.exceptions.user import ( DomainNotFound, EmailNotConfirmed, + EmailSendFailed, PasswordsDoNotMatch, UserAlreadyExists, WrongEmail, @@ -236,6 +237,8 @@ class UserAPIView(APIView): return Response({'detail': f'{exc}'}, status=status.HTTP_400_BAD_REQUEST) except DomainNotFound: return Response({'detail': _('Email not found')}, status=status.HTTP_400_BAD_REQUEST) + except EmailSendFailed as exc: + return Response({'detail': f'{exc}'}, status=status.HTTP_400_BAD_REQUEST) except Exception as exc: logger.exception(exc) return Response( @@ -315,7 +318,9 @@ class BusinessHostAPIView(APIView): except AdminCreateForbidden as err: return Response({'detail': str(err)}, status=status.HTTP_403_FORBIDDEN) except DomainNotFound: - return Response({'detail': _('Email sending error: email not found')}, status=status.HTTP_400_BAD_REQUEST) + return Response( + {'detail': _('Email sending error: email not found')}, status=status.HTTP_400_BAD_REQUEST + ) except Exception as err: return Response({'detail': f'{err}'}, status=status.HTTP_400_BAD_REQUEST) @@ -361,13 +366,14 @@ class ReinviteBusinessAccountAPIView(APIView): """Reinvite business account including generation of a new password""" try: business_account = BusinessAccountSelector.filter_by_email( - email, - BusinessAccountService.get_company_name(request.user) + email, BusinessAccountService.get_company_name(request.user) ) BusinessHostService(request.user).reinvite_business_account(business_account=business_account) return Response({'detail': _('Business account has been reinvited')}, status=status.HTTP_200_OK) except DomainNotFound: - return Response({'detail': _('Email sending error: email not found')}, status=status.HTTP_400_BAD_REQUEST) + return Response( + {'detail': _('Email sending error: email not found')}, status=status.HTTP_400_BAD_REQUEST + ) except LetterNotFound as exc: return Response({'detail': f'{exc}'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) except LetterUnknownException as exc: @@ -433,11 +439,15 @@ class ChangeBusinessAccountPassAPIView(APIView): BusinessAccountService.get_company_name(self.request.user), ) if not business_account: - return Response({'detail': _('Business account not found')}, status=status.HTTP_400_BAD_REQUEST) + return Response( + {'detail': _('Business account not found')}, status=status.HTTP_400_BAD_REQUEST + ) BusinessAccountService(business_account).update_user_password( request.user, data['password_1'], data['password_2'] ) - return Response({'detail': _('Business account password has been updated')}, status=status.HTTP_200_OK) + return Response( + {'detail': _('Business account password has been updated')}, status=status.HTTP_200_OK + ) except (UnconfirmedUserChangePass, PasswordsDoNotMatch, AdminPasswordChangeForbidden) as exc: return Response({'detail': str(exc)}, status=status.HTTP_403_FORBIDDEN) except Exception as exc: @@ -647,7 +657,7 @@ class HostInvitationAPIView(APIView): request=AccountDataUpdateSerializer, responses={200: BusinessAccountDataSerializer}, ) - def put(self, request, user_email : str, *args, **kwargs): + def put(self, request, user_email: str, *args, **kwargs): """Update company sub-user.""" try: serializer = AccountDataUpdateSerializer(data=request.data) @@ -140,6 +140,10 @@ msgstr "Пользователь уже существует" msgid "Domain not found" msgstr "Домен не найден" +#: authentication/exceptions/user.py:41 +msgid "Failed to send the email. Verify that the email exists and is available" +msgstr "Не удалось отправить письмо. Убедитесь, что email существует и доступен" + #: authentication/models/business_account.py:18 payments/models/promocode.py:72 #: tools/public_api/models.py:34 tools/public_api/services/api_key.py:24 msgid "Owner"