diff --git a/src/objecttypes/conf/base.py b/src/objecttypes/conf/base.py index 92d773e..b540364 100644 --- a/src/objecttypes/conf/base.py +++ b/src/objecttypes/conf/base.py @@ -52,8 +52,7 @@ # Django setup configuration # SETUP_CONFIGURATION_STEPS = [ - "objecttypes.setup_configuration.steps.sites.SitesConfigurationStep", "objecttypes.setup_configuration.steps.token_auth.TokenAuthConfigurationStep", - "objecttypes.setup_configuration.steps.TokenAuthConfigurationStep", + "django_setup_configuration.contrib.sites.steps.SitesConfigurationStep", "mozilla_django_oidc_db.setup_configuration.steps.AdminOIDCConfigurationStep", ] diff --git a/src/objecttypes/setup_configuration/steps/sites.py b/src/objecttypes/setup_configuration/steps/sites.py deleted file mode 100644 index b5993d1..0000000 --- a/src/objecttypes/setup_configuration/steps/sites.py +++ /dev/null @@ -1,58 +0,0 @@ -import logging - -from django.contrib.sites.models import Site -from django.core.exceptions import ValidationError -from django.db import IntegrityError - -from django_setup_configuration.configuration import BaseConfigurationStep -from django_setup_configuration.exceptions import ConfigurationRunFailed - -from objecttypes.setup_configuration.models.sites import SitesConfigurationModel - -logger = logging.getLogger(__name__) - - -class SitesConfigurationStep(BaseConfigurationStep[SitesConfigurationModel]): - """ - Configure the application site/domain. - """ - - namespace = "sites_config" - enable_setting = "sites_config_enable" - - verbose_name = "Configuration to set up Sites for ObjectTypes" - config_model = SitesConfigurationModel - - def execute(self, model: SitesConfigurationModel) -> None: - for item in model.items: - logger.info(f"Configuring {item.domain}") - - model_kwargs = { - "domain": item.domain, - "name": item.name, - } - - instance = Site(**model_kwargs) - - try: - instance.full_clean(exclude=("id",), validate_unique=False) - except ValidationError as exception: - exception_message = f"Validation error(s) occured for {item.domain}." - raise ConfigurationRunFailed(exception_message) from exception - - logger.debug(f"No validation errors found for {item.domain}") - - try: - logger.debug(f"Saving {item.domain}") - Site.objects.update_or_create( - domain=item.domain, - defaults={ - "name": item.name, - }, - ) - - except IntegrityError as exception: - exception_message = f"Failed configuring token {item.domain}." - raise ConfigurationRunFailed(exception_message) from exception - - logger.info(f"Configured {item.domain}") diff --git a/src/objecttypes/setup_configuration/tests/test_sites_config.py b/src/objecttypes/setup_configuration/tests/test_sites_config.py deleted file mode 100644 index a2ee69c..0000000 --- a/src/objecttypes/setup_configuration/tests/test_sites_config.py +++ /dev/null @@ -1,58 +0,0 @@ -from pathlib import Path - -from django.contrib.sites.models import Site -from django.test import TestCase - -from django_setup_configuration.exceptions import PrerequisiteFailed -from django_setup_configuration.test_utils import execute_single_step - -from objecttypes.setup_configuration.steps.sites import SitesConfigurationStep - -DIR_FILES = (Path(__file__).parent / "files/sites").resolve() - - -class SitesConfigurationStepTests(TestCase): - def test_valid_setup_default(self): - self.assertTrue( - Site.objects.filter(domain="example.com", name="example.com").exists() - ) - - execute_single_step( - SitesConfigurationStep, yaml_source=str(DIR_FILES / "valid_setup.yaml") - ) - - sites = Site.objects.all() - self.assertEqual(sites.count(), 3) - self.assertTrue(sites.filter(domain="example-1.com", name="example-1").exists()) - self.assertTrue(sites.filter(domain="example-2.com", name="example-2").exists()) - - def test_valid_update_existing_sites(self): - self.assertTrue( - Site.objects.filter(domain="example.com", name="example.com").exists() - ) - - Site.objects.create(domain="example-2.com", name="example-3") - self.assertEqual(Site.objects.count(), 2) - - execute_single_step( - SitesConfigurationStep, yaml_source=str(DIR_FILES / "valid_setup.yaml") - ) - - sites = Site.objects.all() - self.assertEqual(sites.count(), 3) - self.assertTrue(sites.filter(domain="example-2.com", name="example-2").exists()) - self.assertTrue(sites.filter(domain="example-1.com", name="example-1").exists()) - - def test_invalid_setup(self): - self.assertTrue( - Site.objects.filter(domain="example.com", name="example.com").exists() - ) - - with self.assertRaises(PrerequisiteFailed) as command_error: - execute_single_step( - SitesConfigurationStep, - yaml_source=str(DIR_FILES / "invalid_setup.yaml"), - ) - - self.assertTrue("Input should be a valid list" in str(command_error.exception)) - self.assertEqual(Site.objects.count(), 1)