From 43237a42589ffce605b8aa9e1a3a423aa832e744 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Fri, 5 Aug 2022 15:49:31 +0200 Subject: [PATCH] Made daphne installable as a Django app. Added system check to ensure daphne is installed before django.contrib.staticfiles. --- daphne/apps.py | 20 ++++++++++++++++++++ daphne/checks.py | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 daphne/apps.py create mode 100644 daphne/checks.py diff --git a/daphne/apps.py b/daphne/apps.py new file mode 100644 index 0000000..3dd62fa --- /dev/null +++ b/daphne/apps.py @@ -0,0 +1,20 @@ +# Import the server here to ensure the reactor is installed very early on in case other +# packages import twisted.internet.reactor (e.g. raven does this). +import daphne.server # noqa: F401 + + +try: + # Allow that Django is not installed. + from django.apps import AppConfig + from django.core import checks + from .checks import check_daphne_installed +except ImportError: + pass +else: + + class DaphneConfig(AppConfig): + name = "daphne" + verbose_name = "Daphne" + + def ready(self): + checks.register(check_daphne_installed, checks.Tags.staticfiles) diff --git a/daphne/checks.py b/daphne/checks.py new file mode 100644 index 0000000..ba67d12 --- /dev/null +++ b/daphne/checks.py @@ -0,0 +1,20 @@ +# Django system check to ensure daphne app is listed in INSTALLED_APPS before django.contrib.staticfiles. +from django.core.checks import Error, register + + +@register() +def check_daphne_installed(app_configs, **kwargs): + from daphne.apps import DaphneConfig + from django.apps import apps + from django.contrib.staticfiles.apps import StaticFilesConfig + + for app in apps.get_app_configs(): + if isinstance(app, DaphneConfig): + return [] + if isinstance(app, StaticFilesConfig): + return [ + Error( + "Daphne must be listed before django.contrib.staticfiles in INSTALLED_APPS.", + id="daphne.E001", + ) + ]