Skip to content

Commit

Permalink
✨ [#4789] Add identifier field to ZGWAPIGroupConfig
Browse files Browse the repository at this point in the history
to make sure there is a unique field that can be used to refer to the groups and identify them when they are loaded using setup-configuration
  • Loading branch information
stevenbal committed Dec 6, 2024
1 parent ae74347 commit b1bbb84
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/openforms/registrations/contrib/zgw_apis/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
),
]
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
7 changes: 7 additions & 0 deletions src/openforms/registrations/contrib/zgw_apis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit b1bbb84

Please sign in to comment.