Skip to content

Commit

Permalink
♻️ [#2193] Implement Category.zaaktypen select widget with django-jso…
Browse files Browse the repository at this point in the history
…nform
  • Loading branch information
stevenbal committed Mar 12, 2024
1 parent 38043d8 commit b90b562
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions src/open_inwoner/pdc/admin/category.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from collections import defaultdict

from django import forms
from django.contrib import admin
from django.forms import BaseModelFormSet
from django.utils.translation import gettext as _

from django_jsonform.widgets import JSONFormWidget
from django_jsonform.forms.fields import JSONFormField
from import_export.admin import ImportExportMixin
from import_export.formats import base_formats
from ordered_model.admin import OrderedInlineModelAdminMixin, OrderedTabularInline
Expand All @@ -21,40 +19,6 @@
from .faq import QuestionInline


class ZaakTypenSelectWidget(forms.Select):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

zaaktypen = ZaakTypeConfig.objects.order_by("catalogus__domein", "omschrijving")

# Create an optgroup for each Catalogus and its ZaakTypen
choices_dict = defaultdict(list)
choices_dict[""] = [
(
"",
"-------",
)
]
for zaaktype in zaaktypen:
choices_dict[
f"{zaaktype.catalogus.domein} - {zaaktype.catalogus.rsin}"
].append(
(
zaaktype.identificatie,
zaaktype.omschrijving,
)
)

self.choices = choices_dict.items()


class DynamicArraySelectWidget(JSONFormWidget):
def __init__(self, *args, **kwargs):
kwargs["subwidget_form"] = ZaakTypenSelectWidget

super().__init__(*args, **kwargs)


class CategoryProductInline(OrderedTabularInline):
model = CategoryProduct
fields = (
Expand All @@ -66,11 +30,39 @@ class CategoryProductInline(OrderedTabularInline):
extra = 1


def zaaktypen_select_schema() -> dict:
zaaktypen = ZaakTypeConfig.objects.order_by("catalogus__domein", "omschrijving")

choices = []
for zaaktype in zaaktypen:
choices.append(
{
"value": zaaktype.identificatie,
"title": f"{zaaktype.catalogus.domein} - {zaaktype.catalogus.rsin}: {zaaktype.omschrijving}",
}
)

choices = sorted(choices, key=lambda item: item["title"])

schema = {
"type": "array",
"items": {"type": "string", "choices": choices, "widget": "multiselect"},
}
return schema


class CategoryAdminForm(movenodeform_factory(Category)):
zaaktypen = JSONFormField(schema=zaaktypen_select_schema)

class Meta:
model = Category
fields = "__all__"
widgets = {"description": CKEditorWidget, "zaaktypen": DynamicArraySelectWidget}
widgets = {"description": CKEditorWidget}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.fields["zaaktypen"].help_text = self.Meta.model.zaaktypen.field.help_text

def clean(self, *args, **kwargs):
cleaned_data = super().clean(*args, **kwargs)
Expand Down Expand Up @@ -122,9 +114,7 @@ def clean(self):


@admin.register(Category)
class CategoryAdmin(
OrderedInlineModelAdminMixin, ImportExportMixin, TreeAdmin, DynamicArrayMixin
):
class CategoryAdmin(OrderedInlineModelAdminMixin, ImportExportMixin, TreeAdmin):
change_list_template = "admin/category_change_list.html"
form = CategoryAdminForm
inlines = (
Expand Down

0 comments on commit b90b562

Please sign in to comment.