Quick start
Requirements
Python 3.12 or above
Django 4.2 or newer
Install
pip install maykin-config-checks
Add maykin_config_checks to the Django INSTALLED_APPS:
INSTALLED_APPS = [
...
"maykin_config_checks",
...
]
Usage
Implementing checks
This library is based on the following components:
The
ConfigCheckwhich implements the logic of a particular configuration check.The
ConfigCheckResult, which is the result of running aConfigCheck. It contains information about whether the check succeeded and additional information in case the check failed.The
run_checks(), which runs all theConfigCheckthat are retrieved by a collector function and returns an iterable ofConfigCheckResult.
You can see dummy implementations of all of these components below:
from collections.abc import Iterable
from dataclasses import asdict, dataclass
from maykin_config_checks import ConfigCheck, ConfigCheckResult, JSONValue
@dataclass
class DummyResult:
success: bool
identifier: str
verbose_name: str
message: str = ""
extra: dict | None = None
def to_builtins(self) -> JSONValue:
return asdict(self)
class DummyCheck:
identifier = "dummy"
verbose_name = "Dummy"
def __call__(self) -> ConfigCheckResult:
return DummyResult(
success=True,
verbose_name=self.verbose_name,
identifier=self.identifier,
message="Everything is great.",
)
class DummyCheckFail:
identifier = "dummy_fail"
verbose_name = "Dummy fail"
def __call__(self) -> ConfigCheckResult:
return DummyResult(
success=False,
verbose_name=self.verbose_name,
identifier=self.identifier,
message="Everything is sad.",
extra={"info": "bla"},
)
class CheckWithException:
identifier = "check_with_exception"
verbose_name = "Check with exception"
def __call__(self) -> ConfigCheckResult:
raise Exception("HELLO EXCEPTION!")
def check_collector() -> Iterable[ConfigCheck]:
return [DummyCheck(), DummyCheckFail()]
View
To have an API view that returns the results of the performed config checks,
add the following to the urlpatterns:
from django.contrib import admin
from django.urls import path
from maykin_config_checks.api.views import ConfigChecksView
from testapp.checks import DummyCheck, DummyCheckFail
urlpatterns = [
path("admin/", admin.site.urls),
path(
"config-checks",
ConfigChecksView.as_view(
checks_collector=lambda: [DummyCheck(), DummyCheckFail()]
),
name="config-checks",
),
]
Where the argument checks_collector is a Callable[[], Iterable[ConfigCheck]]. It is used to retrieve which config checks should
be performed by the view. You can also add the view multiple times to the urlpatters with different checks_collector arguments
if you want to have multiple config check views that run different checks.
Management command
There is also a management command that can be used to run config checks from the CLI.
django-admin config_checks --checks-collector dotted.path.to.checks_collector_fn