fx-private-relay/emails/apps.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 строка
2.9 KiB
Python
Исходник Постоянная ссылка Обычный вид История

import logging
import os
from typing import NamedTuple
from django.apps import AppConfig, apps
from django.conf import settings
from django.utils.functional import cached_property
import boto3
from botocore.config import Config
from mypy_boto3_ses.client import SESClient
logger = logging.getLogger("events")
# Bad words are split into short and long words
class BadWords(NamedTuple):
# Short words are 4 or less characters. A hit is an exact match to a short word
short: set[str]
# Long words are 5 or more characters. A hit contains a long word.
long: list[str]
2019-06-05 17:38:33 +03:00
class EmailsConfig(AppConfig):
name = "emails"
@cached_property
def ses_client(self) -> SESClient | None:
try:
return boto3.client("ses", region_name=settings.AWS_REGION)
except Exception:
logger.exception("exception during SES connect")
return None
@cached_property
def s3_client(self):
try:
s3_config = Config(
region_name=settings.AWS_REGION,
retries={
2023-05-31 01:20:51 +03:00
# max_attempts includes the initial attempt to get the email
# so this does not retry with backoff, to avoid timeouts
"max_attempts": 1,
"mode": "standard",
},
)
return boto3.client("s3", config=s3_config)
except Exception:
logger.exception("exception during S3 connect")
def __init__(self, app_name, app_module):
2024-03-28 00:16:46 +03:00
super().__init__(app_name, app_module)
# badwords file from:
# https://www.cs.cmu.edu/~biglou/resources/bad-words.txt
2023-05-31 01:20:51 +03:00
# Using `.text` extension because of
# https://github.com/dependabot/dependabot-core/issues/1657
_badwords = self._load_terms("badwords.text")
self.badwords = BadWords(
short=set(word for word in _badwords if len(word) <= 4),
long=sorted(set(word for word in _badwords if len(word) > 4)),
)
self.blocklist = set(self._load_terms("blocklist.text"))
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)
2024-03-28 00:20:11 +03:00
with open(terms_file_path) as terms_file:
for raw_word in terms_file:
word = raw_word.strip()
if not word or (len(word) > 0 and word[0] == "#"):
continue
terms.append(word)
return terms
2024-01-26 19:58:25 +03:00
def emails_config() -> EmailsConfig:
emails_config = apps.get_app_config("emails")
if not isinstance(emails_config, EmailsConfig):
raise TypeError("emails_config must be type EmailsConfig")
2024-01-26 19:58:25 +03:00
return emails_config
def ses_client() -> SESClient | None:
return emails_config().ses_client
def s3_client():
return emails_config().s3_client