Skip to content

Commit

Permalink
Use dataclass for django settings
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed May 9, 2024
1 parent 7837d02 commit 599de40
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
9 changes: 6 additions & 3 deletions docs/pages/integrations/django.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ Dependency injection for Django is available via the first-party integration wir
To install the integration, add `wireup.integration.django` to `INSTALLED_APPS` and define a new `WIREUP` setting.

```python title="settings.py"
from wireup.integration.django import WireupSettings


INSTALLED_APPS = [
...,
"wireup.integration.django"
]

WIREUP = {
WIREUP = WireupSettings(
# This is a list of top-level modules containing service registrations.
# It can be either a list of strings or module types.
"SERVICE_MODULES": ["mysite.polls.services"]
},
service_modules=["mysite.polls.services"]
)
```


Expand Down
5 changes: 4 additions & 1 deletion test/integration/django/test_django_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import django
from django.test import Client
from django.urls import path
from wireup.integration.django import WireupSettings

INSTALLED_APPS = ["wireup.integration.django"]
DEBUG = True
ROOT_URLCONF = sys.modules[__name__]
WIREUP = {"SERVICE_MODULES": ["test.integration.django.service", "test.integration.django.factory"]}
WIREUP = WireupSettings(
service_modules=["test.integration.django.service", "test.integration.django.factory"]
)
SECRET_KEY = "not_actually_a_secret" # noqa: S105
START_NUM = 4

Expand Down
15 changes: 15 additions & 0 deletions wireup/integration/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from types import ModuleType


@dataclass(frozen=True)
class WireupSettings:
"""Class containing Wireup settings specific to Django."""

service_modules: list[str | ModuleType]
"""List of modules containing wireup service registrations."""
11 changes: 9 additions & 2 deletions wireup/integration/django/apps.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
from __future__ import annotations

import importlib
from typing import TYPE_CHECKING

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

from wireup import container, warmup_container

if TYPE_CHECKING:
from wireup.integration.django import WireupSettings


class WireupConfig(AppConfig):
"""Integrate wireup with Django."""

name = "wireup.integration.django"

def ready(self) -> None: # noqa: D102
service_modules = settings.WIREUP.get("SERVICE_MODULES", [])
integration_settings: WireupSettings = settings.WIREUP

for entry in dir(settings):
if not entry.startswith("__") and hasattr(settings, entry):
container.params.put(entry, getattr(settings, entry))

warmup_container(container, [importlib.import_module(m) if isinstance(m, str) else m for m in service_modules])
warmup_container(
container,
[importlib.import_module(m) if isinstance(m, str) else m for m in integration_settings.service_modules],
)

0 comments on commit 599de40

Please sign in to comment.