Skip to content

Commit

Permalink
Merge pull request #1266 from maykinmedia/tasks/2569-persist-service-…
Browse files Browse the repository at this point in the history
…on-zgw-configs

Persist the source service when syncing CatalogusConfig
  • Loading branch information
alextreme authored Jun 21, 2024
2 parents 626dce9 + cad0e96 commit 2149012
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/open_inwoner/openzaak/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ class CatalogusConfigAdmin(admin.ModelAdmin):
"domein",
"rsin",
"url",
"service",
]
fields = [
"url",
"domein",
"rsin",
"service",
]
readonly_fields = fields
search_fields = [
Expand All @@ -85,6 +87,7 @@ class CatalogusConfigAdmin(admin.ModelAdmin):
"url",
]
ordering = ("domein", "rsin")
list_filter = ("service",)


class HasDocNotifyListFilter(admin.SimpleListFilter):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Generated by Django 4.2.11 on 2024-06-19 12:35

import django.db.models.deletion
from django.core.exceptions import MultipleObjectsReturned
from django.db import DataError, migrations, models


def migrate_catalogus_config_service_field_from_default(apps, schema_editor):
CatalogusConfig = apps.get_model("openzaak", "CatalogusConfig")
ZGWApiGroupConfig = apps.get_model("openzaak", "ZGWApiGroupConfig")

if not CatalogusConfig.objects.all().exists():
return

try:
config = ZGWApiGroupConfig.objects.all().get()
except MultipleObjectsReturned:
raise DataError(
"Attempted to set CatalogusConfig.service using ZGWApiGroupConfig, but there"
" are multiple instances configured. Please (temporarily) ensure you have only"
" a single ZGWApiGroupConfig configured, then run this migration again."
)

for catalogus_config in CatalogusConfig.objects.all():
catalogus_config.service = config.ztc_service
catalogus_config.save()


def reverse_migrate_catalogus_config_service_field_from_default(apps, schema_editor):
CatalogusConfig = apps.get_model("openzaak", "CatalogusConfig")
CatalogusConfig.objects.all().update(service=None)


class Migration(migrations.Migration):

dependencies = [
("zgw_consumers", "0019_alter_service_uuid"),
("openzaak", "0051_drop_root_zgw_fields"),
]

operations = [
migrations.AddField(
model_name="catalogusconfig",
name="service",
field=models.ForeignKey(
limit_choices_to={"api_type": "ztc"},
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="catalogus_configs",
to="zgw_consumers.service",
verbose_name="Form API",
),
),
migrations.RunPython(
migrate_catalogus_config_service_field_from_default,
reverse_code=reverse_migrate_catalogus_config_service_field_from_default,
),
migrations.AlterField(
model_name="catalogusconfig",
name="service",
field=models.ForeignKey(
limit_choices_to={"api_type": "ztc"},
on_delete=django.db.models.deletion.PROTECT,
related_name="catalogus_configs",
to="zgw_consumers.service",
verbose_name="Catalogus API",
),
),
]
8 changes: 8 additions & 0 deletions src/open_inwoner/openzaak/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ class CatalogusConfig(models.Model):
verbose_name=_("RSIN"),
max_length=9,
)
service = models.ForeignKey(
"zgw_consumers.Service",
verbose_name=_("Catalogus API"),
on_delete=models.PROTECT,
limit_choices_to={"api_type": APITypes.ztc},
related_name="catalogus_configs",
null=False,
)

class Meta:
ordering = ("domein", "rsin")
Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/openzaak/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CatalogusConfigFactory(factory.django.DjangoModelFactory):
url = factory.Faker("url")
domein = factory.Faker("pystr", max_chars=5)
rsin = factory.Faker("pystr", max_chars=9)
service = factory.SubFactory(ServiceFactory, api_type=APITypes.ztc)

class Meta:
model = CatalogusConfig
Expand Down
89 changes: 87 additions & 2 deletions src/open_inwoner/openzaak/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from django.db import DataError

from zgw_consumers.constants import APITypes

from open_inwoner.openzaak.tests.factories import ServiceFactory
from open_inwoner.utils.tests.test_migrations import TestSuccessfulMigrations
from open_inwoner.openzaak.tests.factories import (
ServiceFactory,
ZGWApiGroupConfigFactory,
)
from open_inwoner.utils.tests.test_migrations import (
TestFailingMigrations,
TestSuccessfulMigrations,
)


class TestMultiZGWBackendMigrations(TestSuccessfulMigrations):
Expand Down Expand Up @@ -66,3 +74,80 @@ def test_migration_0048_to_0051_multi_zgw_backend(self):
expected,
msg="Service config should have been moved to a new ZGWApiGroupConfig",
)


class RequiredServiceToCatalogusConfigMigrationsTestCase:
migrate_from = "0051_drop_root_zgw_fields"
migrate_to = "0052_add_catalogusconfig_service"
app = "openzaak"

def setUp(self):
self.api_group_config = ZGWApiGroupConfigFactory() # Not affected by migrations
super().setUp()


class TestRequiredCatalogusConfigServiceHappyPath(
RequiredServiceToCatalogusConfigMigrationsTestCase, TestSuccessfulMigrations
):
def setUpBeforeMigration(self, apps):
CatalogusConfig = apps.get_model("openzaak", "CatalogusConfig")
CatalogusConfig.objects.create(
url="https://foobar.com", domein="foo", rsin="foo"
)

def test_migration_0051_to_0052_sets_service_from_only_api_group_config(self):
CatalogusConfig = self.apps.get_model("openzaak", "CatalogusConfig")
catalogus_config = CatalogusConfig.objects.all().get()

self.assertEqual(
catalogus_config.service.pk,
self.api_group_config.ztc_service.pk,
)


class TestRequiredCatalogusConfigServiceUnhappyPath(
RequiredServiceToCatalogusConfigMigrationsTestCase, TestFailingMigrations
):
def setUpBeforeMigration(self, apps):
super().setUpBeforeMigration(apps)

# Create another API Group Config to simulate ambiguous service resolution
ZGWApiGroupConfig = apps.get_model("openzaak", "ZGWApiGroupConfig")
Service = apps.get_model("zgw_consumers", "Service")
OpenZaakConfig = apps.get_model("openzaak", "OpenZaakConfig")
CatalogusConfig = apps.get_model("openzaak", "CatalogusConfig")

CatalogusConfig = apps.get_model("openzaak", "CatalogusConfig")
CatalogusConfig.objects.create(
url="https://foobar.com", domein="foo", rsin="foo"
)

catalogi_service = ServiceFactory(api_type=APITypes.ztc)
zaken_service = ServiceFactory(api_type=APITypes.zrc)
documenten_service = ServiceFactory(api_type=APITypes.drc)
forms_service = ServiceFactory(api_type=APITypes.orc)

# Note we have to refetch the service instances here: the factories
# create models that differ from the between-migration models
# expected by this OpenZaakConfig
ZGWApiGroupConfig.objects.create(
open_zaak_config=OpenZaakConfig.objects.get(
id=self.api_group_config.open_zaak_config.id
),
zrc_service=Service.objects.get(id=zaken_service.id),
ztc_service=Service.objects.get(id=catalogi_service.id),
drc_service=Service.objects.get(id=documenten_service.id),
form_service=Service.objects.get(id=forms_service.id),
)

def test_migration_0051_to_0052_raises_for_multiple_api_groups(self):

with self.assertRaises(DataError) as cm:
self.attempt_migration()

self.assertEqual(
str(cm.exception),
"Attempted to set CatalogusConfig.service using ZGWApiGroupConfig, but there"
" are multiple instances configured. Please (temporarily) ensure you have only a single"
" ZGWApiGroupConfig configured, then run this migration again.",
)
1 change: 1 addition & 0 deletions src/open_inwoner/openzaak/zgw_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def import_catalog_configs() -> list[CatalogusConfig]:
url=catalog.url,
rsin=catalog.rsin or "",
domein=catalog.domein,
service=client.configured_from,
)
)

Expand Down

0 comments on commit 2149012

Please sign in to comment.