Skip to content

Commit

Permalink
add auto setup for Django app
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas committed Feb 16, 2024
1 parent 4b6ffaa commit b0605e0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/email_relay/apps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
from __future__ import annotations

from django.apps import AppConfig
from django.conf import global_settings
from django.conf import settings

try:
from environs import Env
except ImportError:
Env = None


class EmailRelayConfig(AppConfig):
name = "email_relay"
default_auto_field = "django.db.models.BigAutoField"

def ready(self) -> None:
from email_relay.conf import app_settings

if not app_settings.ENABLE_AUTO_SETUP:
return

if (
settings.EMAIL_BACKEND is None
or settings.EMAIL_BACKEND == global_settings.EMAIL_BACKEND
):
settings.EMAIL_BACKEND = "email_relay.backend.EmailRelayBackend"

if app_settings.DATABASE_ALIAS not in settings.DATABASES and Env is not None:
env = Env()

settings.DATABASES[app_settings.DATABASE_ALIAS] = env.dj_db_url(
"EMAIL_RELAY_DATABASE_URL", default="sqlite://:memory:"
)

if "email_relay.db.EmailDatabaseRouter" not in settings.DATABASE_ROUTERS:
settings.DATABASE_ROUTERS.append("email_relay.db.EmailDatabaseRouter")
1 change: 1 addition & 0 deletions src/email_relay/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppSettings:
EMAIL_MAX_RETRIES: int | None = None
EMPTY_QUEUE_SLEEP: int = 30
EMAIL_THROTTLE: int = 0
ENABLE_AUTO_SETUP: bool = False
MESSAGES_BATCH_SIZE: int | None = None
MESSAGES_RETENTION_SECONDS: int | None = None
RELAY_HEALTHCHECK_METHOD: str = "GET"
Expand Down
50 changes: 50 additions & 0 deletions tests/test_auto_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import annotations

import pytest
from django.apps import apps
from django.conf import settings
from django.test.utils import override_settings

from email_relay.conf import EMAIL_RELAY_DATABASE_ALIAS


@pytest.fixture(autouse=True, scope="module")
def auto_setup_setting():
with override_settings(
DJANGO_EMAIL_RELAY={
"ENABLE_AUTO_SETUP": True,
},
):
yield


@pytest.fixture
def app_config():
return apps.get_app_config("email_relay")


@override_settings(EMAIL_BACKEND=None)
def test_auto_setup_email_backend(app_config):
app_config.ready()

assert settings.EMAIL_BACKEND == "email_relay.backend.EmailRelayBackend"


@override_settings(
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
},
},
)
def test_auto_setup_database(app_config):
app_config.ready()

assert EMAIL_RELAY_DATABASE_ALIAS in settings.DATABASES


def test_auto_setup_database_router(app_config):
app_config.ready()

assert "email_relay.db.EmailDatabaseRouter" in settings.DATABASE_ROUTERS

0 comments on commit b0605e0

Please sign in to comment.