diff --git a/src/openforms/registrations/contrib/zgw_apis/admin.py b/src/openforms/registrations/contrib/zgw_apis/admin.py index 5924927471..f824a48341 100644 --- a/src/openforms/registrations/contrib/zgw_apis/admin.py +++ b/src/openforms/registrations/contrib/zgw_apis/admin.py @@ -6,14 +6,26 @@ @admin.register(ZGWApiGroupConfig) class ZGWApiGroupConfigAdmin(admin.ModelAdmin): - list_display = ("name", "zrc_service", "drc_service", "ztc_service") + list_display = ("name", "identifier", "zrc_service", "drc_service", "ztc_service") list_select_related = ("zrc_service", "drc_service", "ztc_service") - search_fields = ("name",) + search_fields = ( + "name", + "identifier", + ) raw_id_fields = ("zrc_service", "drc_service", "ztc_service") + prepopulated_fields = {"identifier": ["name"]} ordering = ("name",) fieldsets = ( - (None, {"fields": ("name",)}), + ( + None, + { + "fields": ( + "name", + "identifier", + ) + }, + ), ( _("Services"), { diff --git a/src/openforms/registrations/contrib/zgw_apis/migrations/0016_zgwapigroupconfig_identifier.py b/src/openforms/registrations/contrib/zgw_apis/migrations/0016_zgwapigroupconfig_identifier.py new file mode 100644 index 0000000000..7bf1d17d4e --- /dev/null +++ b/src/openforms/registrations/contrib/zgw_apis/migrations/0016_zgwapigroupconfig_identifier.py @@ -0,0 +1,43 @@ +# Generated by Django 4.2.16 on 2024-12-02 10:30 + +from django.db import migrations, models +from django.utils.text import slugify + + +def set_zgw_api_group_config_identifier_from_name(apps, schema_editor): + ZGWApiGroupConfig = apps.get_model("zgw_apis", "ZGWApiGroupConfig") + + def generate_unique_identifier(original_identifier, count=0): + identifier = original_identifier + ("-" + str(count) if count else "") + if not ZGWApiGroupConfig.objects.filter(identifier=identifier).exists(): + return identifier + + return generate_unique_identifier(original_identifier, count + 1) + + for row in ZGWApiGroupConfig.objects.all(): + candidate_slug = slugify(row.name) + row.identifier = generate_unique_identifier(candidate_slug) + row.save(update_fields=["identifier"]) + + +class Migration(migrations.Migration): + + dependencies = [ + ("zgw_apis", "0015_explicit_objects_api_groups"), + ] + + operations = [ + migrations.AddField( + model_name="zgwapigroupconfig", + name="identifier", + field=models.SlugField( + blank=True, + help_text="A unique, human-friendly identifier to identify this group.", + verbose_name="identifier", + ), + ), + migrations.RunPython( + set_zgw_api_group_config_identifier_from_name, + reverse_code=migrations.RunPython.noop, + ), + ] diff --git a/src/openforms/registrations/contrib/zgw_apis/migrations/0017_alter_zgwapigroupconfig_identifier.py b/src/openforms/registrations/contrib/zgw_apis/migrations/0017_alter_zgwapigroupconfig_identifier.py new file mode 100644 index 0000000000..e74020acd0 --- /dev/null +++ b/src/openforms/registrations/contrib/zgw_apis/migrations/0017_alter_zgwapigroupconfig_identifier.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.16 on 2024-12-02 10:31 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("zgw_apis", "0016_zgwapigroupconfig_identifier"), + ] + + operations = [ + migrations.AlterField( + model_name="zgwapigroupconfig", + name="identifier", + field=models.SlugField( + help_text="A unique, human-friendly identifier to identify this group.", + unique=True, + verbose_name="identifier", + ), + ), + ] diff --git a/src/openforms/registrations/contrib/zgw_apis/models.py b/src/openforms/registrations/contrib/zgw_apis/models.py index 2d38d1344c..c911a4978e 100644 --- a/src/openforms/registrations/contrib/zgw_apis/models.py +++ b/src/openforms/registrations/contrib/zgw_apis/models.py @@ -54,6 +54,13 @@ class ZGWApiGroupConfig(models.Model): max_length=255, help_text=_("A recognisable name for this set of ZGW APIs."), ) + identifier = models.SlugField( + _("identifier"), + blank=False, + null=False, + unique=True, + help_text=_("A unique, human-friendly identifier to identify this group."), + ) zrc_service = models.ForeignKey( "zgw_consumers.Service", verbose_name=_("Zaken API"), diff --git a/src/openforms/registrations/contrib/zgw_apis/tests/factories.py b/src/openforms/registrations/contrib/zgw_apis/tests/factories.py index 67c1cb943c..719fe3fb75 100644 --- a/src/openforms/registrations/contrib/zgw_apis/tests/factories.py +++ b/src/openforms/registrations/contrib/zgw_apis/tests/factories.py @@ -1,3 +1,5 @@ +from django.utils.text import slugify + import factory from zgw_consumers.constants import APITypes, AuthTypes from zgw_consumers.test.factories import ServiceFactory @@ -7,6 +9,7 @@ class ZGWApiGroupConfigFactory(factory.django.DjangoModelFactory): name = factory.Sequence(lambda n: "ZGW API set %03d" % n) + identifier = factory.LazyAttribute(lambda o: slugify(o.name)) zrc_service = factory.SubFactory( "zgw_consumers.test.factories.ServiceFactory", api_type=APITypes.zrc ) diff --git a/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py b/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py index 51bdd2dd48..f1631ef6e9 100644 --- a/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py +++ b/src/openforms/registrations/contrib/zgw_apis/tests/test_migrations.py @@ -43,3 +43,27 @@ def test_set_explicit_objects_api_groups_on_zgw_api_group_configs(self): backend_with_api_group.options["objects_api_group"], self.objects_api_group.pk, ) + + +class AddZGWApiGroupConfigIdentifierTests(TestMigrations): + app = "zgw_apis" + migrate_from = "0015_explicit_objects_api_groups" + migrate_to = "0016_zgwapigroupconfig_identifier" + + def setUpBeforeMigration(self, apps: StateApps): + ZGWApiGroupConfig = apps.get_model("zgw_apis", "ZGWApiGroupConfig") + ZGWApiGroupConfig.objects.create(name="Group name") + ZGWApiGroupConfig.objects.create(name="Duplicate name") + ZGWApiGroupConfig.objects.create(name="Duplicate name") + + def test_identifiers_generated(self): + ZGWApiGroupConfig = self.apps.get_model("zgw_apis", "ZGWApiGroupConfig") + groups = ZGWApiGroupConfig.objects.all() + + self.assertEqual(groups.count(), 3) + + group1, group2, group3 = groups + + self.assertEqual(group1.identifier, "group-name") + self.assertEqual(group2.identifier, "duplicate-name") + self.assertEqual(group3.identifier, "duplicate-name-1")