-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#467] update django-setup-configuration site step
- Loading branch information
Showing
10 changed files
with
208 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.contrib.sites.models import Site | ||
from django_setup_configuration.models import ConfigurationModel | ||
from pydantic import Field | ||
|
||
|
||
class SiteConfigurationModel(ConfigurationModel): | ||
class Meta: | ||
django_model_refs = { | ||
Site: ( | ||
"domain", | ||
"name", | ||
) | ||
} | ||
|
||
|
||
class SitesConfigurationModel(ConfigurationModel): | ||
items: list[SiteConfigurationModel] = Field() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 objects.setup_configuration.models.sites import SitesConfigurationModel | ||
|
||
|
||
class SitesConfigurationStep(BaseConfigurationStep): | ||
config_model = SitesConfigurationModel | ||
verbose_name = "Sites configuration" | ||
|
||
namespace = "sites" | ||
enable_setting = "sites_config_enable" | ||
|
||
def execute(self, model: SitesConfigurationModel) -> None: | ||
for item in model.items: | ||
site_kwargs = dict(domain=item.domain, name=item.name) | ||
site_instance = Site(**site_kwargs) | ||
|
||
try: | ||
site_instance.full_clean(exclude=("id",), validate_unique=False) | ||
except ValidationError as exception: | ||
exception_message = ( | ||
f"Validation error(s) occured for site {item.domain}." | ||
) | ||
raise ConfigurationRunFailed(exception_message) from exception | ||
|
||
try: | ||
Site.objects.update_or_create( | ||
domain=item.domain, | ||
defaults=dict(name=item.name) | ||
) | ||
except IntegrityError as exception: | ||
exception_message = ( | ||
f"Failed configuring site {item.domain}." | ||
) | ||
raise ConfigurationRunFailed(exception_message) from exception |
Empty file.
8 changes: 8 additions & 0 deletions
8
src/objects/setup_configuration/tests/files/sites_empty_database.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sites_config_enable: true | ||
sites: | ||
items: | ||
- domain: example.com | ||
name: Example site | ||
|
||
- domain: alternative.example.com | ||
name: Alternative example site |
8 changes: 8 additions & 0 deletions
8
src/objects/setup_configuration/tests/files/sites_existing_sites.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sites_config_enable: true | ||
sites: | ||
items: | ||
- domain: example.com | ||
name: Example site (revised) | ||
|
||
- domain: test.example.com | ||
name: Test site |
8 changes: 8 additions & 0 deletions
8
src/objects/setup_configuration/tests/files/sites_idempotent_step.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sites_config_enable: true | ||
sites: | ||
items: | ||
- domain: example.com | ||
name: Example site | ||
|
||
- domain: alternative.example.com | ||
name: Alternative example site |
8 changes: 8 additions & 0 deletions
8
src/objects/setup_configuration/tests/files/sites_invalid_domain.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sites_config_enable: true | ||
sites: | ||
items: | ||
- domain: example.com | ||
name: Example site | ||
|
||
- domain: foobar whitespace.com | ||
name: Invalid site |
101 changes: 101 additions & 0 deletions
101
src/objects/setup_configuration/tests/test_site_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from pathlib import Path | ||
|
||
from django.contrib.sites.models import Site | ||
from django.db.models import QuerySet | ||
from django.test import TestCase | ||
|
||
from django_setup_configuration.exceptions import ConfigurationRunFailed | ||
from django_setup_configuration.test_utils import execute_single_step | ||
|
||
from objects.setup_configuration.steps.sites import SitesConfigurationStep | ||
|
||
TEST_FILES = (Path(__file__).parent / "files").resolve() | ||
|
||
|
||
class SitesConfigurationStepTests(TestCase): | ||
def test_empty_database(self): | ||
test_file_path = str(TEST_FILES / "sites_empty_database.yaml") | ||
|
||
execute_single_step(SitesConfigurationStep, yaml_source=test_file_path) | ||
|
||
sites: QuerySet[Site] = Site.objects.all() | ||
|
||
self.assertEqual(sites.count(), 2) | ||
|
||
example_site: Site = sites.get(name="Example site") | ||
self.assertEqual(example_site.domain, "example.com") | ||
|
||
alternative_site: Site = sites.get(name="Alternative example site") | ||
self.assertEqual(alternative_site.domain, "alternative.example.com") | ||
|
||
def test_existing_sites(self): | ||
test_file_path = str(TEST_FILES / "sites_existing_sites.yaml") | ||
|
||
example_site, _ = Site.objects.get_or_create( | ||
domain="example.com", | ||
defaults=dict(name="Example site") | ||
) | ||
|
||
alternative_site = Site.objects.create( | ||
domain="alternative.example.com", | ||
name="Alternative example site", | ||
) | ||
|
||
execute_single_step(SitesConfigurationStep, yaml_source=test_file_path) | ||
|
||
sites: QuerySet[Site] = Site.objects.order_by("name") | ||
|
||
self.assertEqual(sites.count(), 3) | ||
|
||
example_site: Site = sites.get(name="Example site (revised)") | ||
self.assertEqual(example_site.domain, "example.com") | ||
|
||
alternative_site: Site = sites.get(name="Alternative example site") | ||
self.assertEqual(alternative_site.domain, "alternative.example.com") | ||
|
||
test_site: Site = sites.get(name="Test site") | ||
self.assertEqual(test_site.domain, "test.example.com") | ||
|
||
def test_invalid_domain(self): | ||
test_file_path = str(TEST_FILES / "sites_invalid_domain.yaml") | ||
|
||
with self.assertRaises(ConfigurationRunFailed): | ||
execute_single_step(SitesConfigurationStep, yaml_source=test_file_path) | ||
|
||
sites: QuerySet[Site] = Site.objects.all() | ||
|
||
# the default test site created during test runs | ||
self.assertEqual(sites.count(), 1) | ||
|
||
site: Site = sites.get() | ||
|
||
self.assertEqual(site.domain, "example.com") | ||
|
||
def test_idempotent_step(self): | ||
test_file_path = str(TEST_FILES / "sites_idempotent_step.yaml") | ||
|
||
execute_single_step(SitesConfigurationStep, yaml_source=test_file_path) | ||
|
||
sites: QuerySet[Site] = Site.objects.all() | ||
|
||
self.assertEqual(sites.count(), 2) | ||
|
||
example_site: Site = sites.get(name="Example site") | ||
self.assertEqual(example_site.domain, "example.com") | ||
|
||
alternative_site: Site = sites.get(name="Alternative example site") | ||
self.assertEqual(alternative_site.domain, "alternative.example.com") | ||
|
||
execute_single_step(SitesConfigurationStep, yaml_source=test_file_path) | ||
|
||
self.assertEqual(Site.objects.count(), 2) | ||
|
||
example_site.refresh_from_db() | ||
|
||
self.assertEqual(example_site.name, "Example site") | ||
self.assertEqual(example_site.domain, "example.com") | ||
|
||
alternative_site.refresh_from_db() | ||
|
||
self.assertEqual(alternative_site.name, "Alternative example site") | ||
self.assertEqual(alternative_site.domain, "alternative.example.com") |