diff --git a/src/open_inwoner/openklant/admin.py b/src/open_inwoner/openklant/admin.py index 62574c9434..aab7d7a46b 100644 --- a/src/open_inwoner/openklant/admin.py +++ b/src/open_inwoner/openklant/admin.py @@ -2,16 +2,20 @@ from django.contrib import admin from django.utils.translation import gettext_lazy as _ +from ordered_model.admin import OrderedTabularInline from solo.admin import SingletonModelAdmin from .models import ContactFormSubject, OpenKlantConfig -class ContactFormSubjectInlineAdmin(admin.TabularInline): +class ContactFormSubjectInlineAdmin(OrderedTabularInline): model = ContactFormSubject fields = [ + "move_up_down_links", "subject", ] + readonly_fields = ("move_up_down_links",) + ordering = ("order",) extra = 0 diff --git a/src/open_inwoner/openklant/migrations/0004_auto_20230620_1419.py b/src/open_inwoner/openklant/migrations/0004_auto_20230620_1419.py new file mode 100644 index 0000000000..dda203b545 --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0004_auto_20230620_1419.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.15 on 2023-06-20 12:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("openklant", "0003_auto_20230526_1013"), + ] + + operations = [ + migrations.AlterModelOptions( + name="contactformsubject", + options={ + "verbose_name": "Contact formulier onderwerp", + "verbose_name_plural": "Contact formulier onderwerpen", + }, + ), + migrations.AddField( + model_name="contactformsubject", + name="order", + field=models.PositiveIntegerField( + db_index=True, default=0, editable=False, verbose_name="order" + ), + ), + ] diff --git a/src/open_inwoner/openklant/migrations/0005_auto_20230620_1419.py b/src/open_inwoner/openklant/migrations/0005_auto_20230620_1419.py new file mode 100644 index 0000000000..a3154b0b31 --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0005_auto_20230620_1419.py @@ -0,0 +1,22 @@ +# Generated by Django 3.2.15 on 2023-06-20 12:19 + +from django.db import migrations + + +def populate_order_of_subjects(apps, schema_editor): + Subject = apps.get_model("openklant", "ContactFormSubject") + for subject in Subject.objects.all(): + subject.order = subject.pk + subject.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("openklant", "0004_auto_20230620_1419"), + ] + + operations = [ + migrations.RunPython( + populate_order_of_subjects, reverse_code=migrations.RunPython.noop + ), + ] diff --git a/src/open_inwoner/openklant/migrations/0006_alter_contactformsubject_order.py b/src/open_inwoner/openklant/migrations/0006_alter_contactformsubject_order.py new file mode 100644 index 0000000000..9924e578a9 --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0006_alter_contactformsubject_order.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.15 on 2023-06-20 14:34 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("openklant", "0005_auto_20230620_1419"), + ] + + operations = [ + migrations.AlterField( + model_name="contactformsubject", + name="order", + field=models.PositiveIntegerField( + db_index=True, editable=False, verbose_name="order" + ), + ), + ] diff --git a/src/open_inwoner/openklant/models.py b/src/open_inwoner/openklant/models.py index 391c539b5d..42cb70edd1 100644 --- a/src/open_inwoner/openklant/models.py +++ b/src/open_inwoner/openklant/models.py @@ -1,6 +1,7 @@ from django.db import models from django.utils.translation import gettext_lazy as _ +from ordered_model.models import OrderedModel, OrderedModelManager from solo.models import SingletonModel from zgw_consumers.constants import APITypes @@ -86,7 +87,7 @@ def has_api_configuration(self): return all(getattr(self, f, "") for f in self.register_api_required_fields) -class ContactFormSubject(models.Model): +class ContactFormSubject(OrderedModel): subject = models.CharField( verbose_name=_("Onderwerp"), max_length=255, @@ -97,10 +98,12 @@ class ContactFormSubject(models.Model): on_delete=models.CASCADE, ) + order_with_respect_to = "subject" + objects = OrderedModelManager() + class Meta: verbose_name = _("Contact formulier onderwerp") verbose_name_plural = _("Contact formulier onderwerpen") - ordering = ("subject",) def __str__(self): return self.subject