diff --git a/src/open_inwoner/accounts/apps.py b/src/open_inwoner/accounts/apps.py index 40a4b49d16..8def5d5dce 100644 --- a/src/open_inwoner/accounts/apps.py +++ b/src/open_inwoner/accounts/apps.py @@ -1,3 +1,4 @@ +import logging from io import StringIO from django.apps import AppConfig, apps @@ -6,10 +7,18 @@ from django.core.management import call_command from django.db.models.signals import post_migrate +logger = logging.getLogger(__name__) + def update_admin_index(sender=None, **kwargs) -> bool: if "django_admin_index" not in settings.INSTALLED_APPS: - print("django_admin_index not installed, skipping update_admin_index()") + logger.info("django_admin_index not installed, skipping update_admin_index()") + return False + + if not settings.LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP: + logger.info( + "LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP is set to False, skipping update_admin_index()" + ) return False from django_admin_index.models import AppGroup @@ -32,18 +41,21 @@ def update_admin_index(sender=None, **kwargs) -> bool: try: call_command("loaddata", "django-admin-index", verbosity=0, stdout=StringIO()) except Exception as exc: - print(f"Error: Unable to load django-admin-index fixture ({exc})") + error_message = f"Error: Unable to load django-admin-index fixture ({exc})" + if ct_create_exceptions: ct_exc = ExceptionGroup( "Unable to create contenttypes", *ct_create_exceptions ) - print( + error_message += ( "NOTE: this may be a consequence of being unable to create the following " f"contenttypes: {ct_exc}" ) + + logger.error(error_message) return False else: - print("Successfully loaded django-admin-index fixture") + logger.info("Successfully loaded django-admin-index fixture") return True diff --git a/src/open_inwoner/accounts/tests/test_admin_index_fixture.py b/src/open_inwoner/accounts/tests/test_admin_index_fixture.py deleted file mode 100644 index 557d1270c0..0000000000 --- a/src/open_inwoner/accounts/tests/test_admin_index_fixture.py +++ /dev/null @@ -1,28 +0,0 @@ -from django.core.management import call_command -from django.db.models.signals import post_migrate -from django.test import TestCase, TransactionTestCase - -from open_inwoner.accounts.apps import update_admin_index - - -class ManualLoadDjangoAdminIndexFixtureTestCase(TransactionTestCase): - """This test case serves as a trigger to warn us when we forget to - update the django-admin-index fixture.""" - - def test_django_admin_fixture_can_be_successfully_loaded(self): - try: - call_command("loaddata", "django-admin-index.json", verbosity=0) - except Exception as e: - self.fail( - "Failed to load the django-admin-index fixture: perhaps you forgot to update them? " - f"Got error: {str(e)}" - ) - - -class AutoLoadDjangoAdminIndexFixtureTestCase(TestCase): - def test_update_admin_index_hook_is_registered(self): - connected_functions = [receiver[1]() for receiver in post_migrate.receivers] - self.assertIn(update_admin_index, connected_functions) - - def test_update_admin_index_hook_returns_true(self): - self.assertTrue(update_admin_index()) diff --git a/src/open_inwoner/conf/ci.py b/src/open_inwoner/conf/ci.py index 67a085208a..cc3433b3d2 100644 --- a/src/open_inwoner/conf/ci.py +++ b/src/open_inwoner/conf/ci.py @@ -73,3 +73,6 @@ PASSWORD_HASHERS = [ "django.contrib.auth.hashers.MD5PasswordHasher", ] + +# No need for this in CI, it just slows things down +LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP = False diff --git a/src/open_inwoner/conf/tests/test_fixtures.py b/src/open_inwoner/conf/tests/test_fixtures.py index a3d649cdc5..dc947df6f3 100644 --- a/src/open_inwoner/conf/tests/test_fixtures.py +++ b/src/open_inwoner/conf/tests/test_fixtures.py @@ -1,12 +1,13 @@ from django.core.management import call_command -from django.test import TestCase +from django.db.models.signals import post_migrate +from django.test import TestCase, override_settings from open_inwoner.accounts.apps import update_admin_index class FixtureTests(TestCase): def test_admin_index(self): - self.assertTrue(update_admin_index()) + call_command("loaddata", "django-admin-index") def test_cms_pages(self): # pass if this doesn't raise anything @@ -23,3 +24,17 @@ def test_mail_editor(self): def test_profile_apphook_config(self): # pass if this doesn't raise anything call_command("loaddata", "profile_apphook_config") + + +class AutoLoadDjangoAdminIndexFixtureTests(TestCase): + @override_settings(LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP=False) + def test_update_admin_index_hook_is_not_registered_if_flag_is_unset(self): + self.assertFalse(update_admin_index()) + + @override_settings(LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP=True) + def test_update_admin_index_hook_is_registered_if_flag_is_set(self): + self.assertTrue(update_admin_index()) + + def test_update_admin_index_hook_is_registered(self): + connected_functions = [receiver[1]() for receiver in post_migrate.receivers] + self.assertIn(update_admin_index, connected_functions)