From 5b7e912398ee6767b51a2b0bea1808077f6469b9 Mon Sep 17 00:00:00 2001 From: Andy Chosak Date: Thu, 3 May 2018 21:50:24 -0400 Subject: [PATCH] add django check and dependency on modeladmin The UI for wagtail-sharing depends on wagtail.contrib.modeladmin, but this is an optional package that is not part of the standard Wagtail installation. This commit adds a note to the README about this and also adds a Django check that will fail if wagtail.contrib.modeladmin isn't in INSTALLED_APPS. As a side effect this change also adds a custom AppConfig for this app. --- CHANGELOG.md | 1 + README.rst | 2 ++ wagtailsharing/__init__.py | 1 + wagtailsharing/apps.py | 10 ++++++++++ wagtailsharing/checks.py | 23 +++++++++++++++++++++++ wagtailsharing/tests/test_checks.py | 21 +++++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 wagtailsharing/apps.py create mode 100644 wagtailsharing/checks.py create mode 100644 wagtailsharing/tests/test_checks.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 46dcb6a..c684689 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ We follow the [Semantic Versioning 2.0.0](http://semver.org/) format. - Simplified CHANGELOG format. - Removed superfluous tests of `Page.get_admin_display_title`. - Add additional tests for the `before_serve_shared_page` and `after_serve_shared_page` hooks. +- Added Django check and note in README to clarify dependency on `wagtail.contrib.modeladmin` app. ## 0.6 - 2017-11-27 diff --git a/README.rst b/README.rst index b2c6071..85bcb99 100644 --- a/README.rst +++ b/README.rst @@ -42,6 +42,8 @@ Add ``wagtailsharing`` as an installed app in your Django settings: ... ) +``wagtail.contrib.modeladmin`` is also required and must be included in your list of installed apps. + Run migrations to create required database tables: .. code-block:: bash diff --git a/wagtailsharing/__init__.py b/wagtailsharing/__init__.py index e69de29..e3c27c3 100644 --- a/wagtailsharing/__init__.py +++ b/wagtailsharing/__init__.py @@ -0,0 +1 @@ +default_app_config = 'wagtailsharing.apps.WagtailSharingAppConfig' diff --git a/wagtailsharing/apps.py b/wagtailsharing/apps.py new file mode 100644 index 0000000..ffb24d1 --- /dev/null +++ b/wagtailsharing/apps.py @@ -0,0 +1,10 @@ +from django.apps import AppConfig +from django.utils.translation import ugettext_lazy as _ + +from . import checks # noqa F401 + + +class WagtailSharingAppConfig(AppConfig): + name = 'wagtailsharing' + label = 'wagtailsharing' + verbose_name = _("Wagtail Sharing") diff --git a/wagtailsharing/checks.py b/wagtailsharing/checks.py new file mode 100644 index 0000000..474c0b5 --- /dev/null +++ b/wagtailsharing/checks.py @@ -0,0 +1,23 @@ +from django.apps import apps +from django.core.checks import Error, register + + +@register() +def modeladmin_installed_check(app_configs, **kwargs): + errors = [] + + MODELADMIN_APP = 'wagtail.contrib.modeladmin' + if not apps.is_installed(MODELADMIN_APP): + error_hint = ( + "Is '{}' in settings.INSTALLED_APPS?".format(MODELADMIN_APP) + ) + + errors.append( + Error( + "wagtail-sharing requires the Wagtail ModelAdmin app.", + hint=error_hint, + id='wagtailsharing.E001' + ) + ) + + return errors diff --git a/wagtailsharing/tests/test_checks.py b/wagtailsharing/tests/test_checks.py new file mode 100644 index 0000000..ef17fea --- /dev/null +++ b/wagtailsharing/tests/test_checks.py @@ -0,0 +1,21 @@ +from django.apps import apps +from django.core.checks import Error +from django.test import SimpleTestCase, override_settings + +from wagtailsharing.checks import modeladmin_installed_check + + +class TestModelAdminInstalledCheck(SimpleTestCase): + @override_settings(INSTALLED_APPS=[ + 'wagtail.contrib.modeladmin', + 'wagtailsharing', + ]) + def test_check_passes_if_modeladmin_installed(self): + self.assertFalse(modeladmin_installed_check(apps.get_app_configs())) + + @override_settings(INSTALLED_APPS=['wagtailsharing']) + def test_check_fails_if_modeladmin_not_installed(self): + errors = modeladmin_installed_check(apps.get_app_configs()) + self.assertEqual(len(errors), 1) + self.assertIsInstance(errors[0], Error) + self.assertEqual(errors[0].id, 'wagtailsharing.E001')