Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPP-3827: Move non-model code out of emails/models.py #4799

Merged
merged 7 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion emails/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def __init__(self, app_name, app_module):
self.badwords = self._load_terms("badwords.text")
self.blocklist = self._load_terms("blocklist.text")

def _load_terms(self, filename):
def _load_terms(self, filename: str) -> list[str]:
"""Load a list of terms from a file."""
terms = []
terms_file_path = os.path.join(settings.BASE_DIR, "emails", filename)
with open(terms_file_path) as terms_file:
Expand Down
105 changes: 105 additions & 0 deletions emails/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""Exceptions raised by emails app"""

from django.conf import settings
from django.core.exceptions import BadRequest

from api.exceptions import ErrorContextType, RelayAPIException


class CannotMakeSubdomainException(BadRequest):
"""Exception raised by Profile due to error on subdomain creation.

Attributes:
message -- optional explanation of the error
"""

def __init__(self, message: str | None = None) -> None:
self.message = message


class CannotMakeAddressException(RelayAPIException):
"""Base exception for RelayAddress or DomainAddress creation failure."""


class AccountIsPausedException(CannotMakeAddressException):
default_code = "account_is_paused"
default_detail = "Your account is on pause."
status_code = 403


class AccountIsInactiveException(CannotMakeAddressException):
default_code = "account_is_inactive"
default_detail = "Your account is not active."
status_code = 403


class RelayAddrFreeTierLimitException(CannotMakeAddressException):
default_code = "free_tier_limit"
default_detail_template = (
"You’ve used all {free_tier_limit} email masks included with your free account."
" You can reuse an existing mask, but using a unique mask for each account is"
" the most secure option."
)
status_code = 403

def __init__(self, free_tier_limit: int | None = None):
self.free_tier_limit = free_tier_limit or settings.MAX_NUM_FREE_ALIASES
super().__init__()

def error_context(self) -> ErrorContextType:
return {"free_tier_limit": self.free_tier_limit}


class DomainAddrFreeTierException(CannotMakeAddressException):
default_code = "free_tier_no_subdomain_masks"
default_detail = (
"Your free account does not include custom subdomains for masks."
" To create custom masks, upgrade to Relay Premium."
)
status_code = 403


class DomainAddrNeedSubdomainException(CannotMakeAddressException):
default_code = "need_subdomain"
default_detail = "Please select a subdomain before creating a custom email address."
status_code = 400


class DomainAddrUpdateException(CannotMakeAddressException):
"""Exception raised when attempting to edit an existing domain address field."""

default_code = "address_not_editable"
default_detail = "You cannot edit an existing domain address field."
status_code = 400


class DomainAddrUnavailableException(CannotMakeAddressException):
default_code = "address_unavailable"
default_detail_template = (
"“{unavailable_address}” could not be created."
" Please try again with a different mask name."
)
status_code = 400

def __init__(self, unavailable_address: str):
self.unavailable_address = unavailable_address
super().__init__()

def error_context(self) -> ErrorContextType:
return {"unavailable_address": self.unavailable_address}


class DomainAddrDuplicateException(CannotMakeAddressException):
default_code = "duplicate_address"
default_detail_template = (
"“{duplicate_address}” already exists."
" Please try again with a different mask name."
)
status_code = 409

def __init__(self, duplicate_address: str):
self.duplicate_address = duplicate_address
super().__init__()

def error_context(self) -> ErrorContextType:
return {"duplicate_address": self.duplicate_address}
4 changes: 2 additions & 2 deletions emails/migrations/0024_increase_subdomain_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.db import migrations, models

import emails.models
import emails.validators


class Migration(migrations.Migration):
Expand All @@ -20,7 +20,7 @@ class Migration(migrations.Migration):
max_length=63,
null=True,
unique=True,
validators=[emails.models.valid_available_subdomain],
validators=[emails.validators.valid_available_subdomain],
),
),
]
Loading