Skip to content

Commit

Permalink
fix category form
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk authored and m4ra committed Nov 29, 2023
1 parent 15f5ce3 commit ff78da3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 52 deletions.
6 changes: 0 additions & 6 deletions adhocracy4/categories/fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.db import models
from django.utils.translation import gettext_lazy as _

from adhocracy4.categories.form_fields import CategoryChoiceField
from adhocracy4.categories.models import Category


Expand All @@ -17,8 +16,3 @@ def __init__(self, *args, **kwargs):
}
defaults.update(kwargs)
super().__init__(**defaults)

def formfield(self, **kwargs):
form_class = kwargs.get("form_class", CategoryChoiceField)
kwargs["form_class"] = form_class
return super().formfield(**kwargs)
42 changes: 6 additions & 36 deletions adhocracy4/categories/form_fields.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
from collections import abc

from django import forms
from django.conf import settings
from django.forms import widgets
from django.utils.functional import cached_property

from adhocracy4.categories import get_category_icon_url


class CategoryIconDict(abc.Mapping):
def __init__(self, field):
self.field = field
self.queryset = field.queryset

@cached_property
def _icons(self):
return {
self.field.prepare_value(obj): getattr(obj, "icon", None)
for obj in self.queryset.all()
}

def __getitem__(self, key):
return self._icons.__getitem__(key)

def __iter__(self):
return self._icons.__iter__()

def __len__(self):
return self._icons.__len__()


class CategorySelectWidget(widgets.Select):
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
Expand All @@ -40,30 +16,24 @@ def get_context(self, name, value, attrs):

def create_option(self, name, value, label, selected, index, **kwargs):
option = super().create_option(name, value, label, selected, index, **kwargs)
if hasattr(value, "value") and value.value in self.icons:
icon_url = get_category_icon_url(self.icons[value.value])
if value in self.icons:
icon_url = get_category_icon_url(value)
option["attrs"]["data-icon-src"] = icon_url

return option


class CategoryChoiceField(forms.ModelChoiceField):
class CategoryChoiceField(forms.ChoiceField):
widget = CategorySelectWidget

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.widget.icons = self.icons
self.choices = getattr(settings, "A4_CATEGORY_ICONS", [])

@property
def icons(self):
return CategoryIconDict(self)

# Update the icons if the queryset is updated
def _set_queryset(self, queryset):
super()._set_queryset(queryset)
self.widget.icons = self.icons

queryset = property(forms.ModelChoiceField._get_queryset, _set_queryset)
return dict(self.choices)


class IconSelectWidget(widgets.Select):
Expand Down
2 changes: 2 additions & 0 deletions adhocracy4/categories/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from adhocracy4.modules import models as module_models

from . import has_icons
from .form_fields import CategoryChoiceField


class CategorizableFieldMixin:
Expand Down Expand Up @@ -50,6 +51,7 @@ def __init__(self, module, *args, **kwargs):
del self.fields["icon"]

name = forms.CharField(widget=forms.TextInput(attrs={"placeholder": _("Category")}))
icon = CategoryChoiceField()

class Media:
js = ("category_formset.js",)
Expand Down
10 changes: 0 additions & 10 deletions adhocracy4/categories/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _

Expand All @@ -13,15 +12,6 @@ def __init__(self, *args, **kwargs):
kwargs["blank"] = True
super().__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, **kwargs):
"""Initialize icon choices from the settings if they exist."""

if hasattr(settings, "A4_CATEGORY_ICONS"):
self.choices = settings.A4_CATEGORY_ICONS

# Call the super method at last so that choices are already initialized
super().contribute_to_class(cls, name, **kwargs)

def formfield(self, **kwargs):
form_class = kwargs.get("choices_form_class", IconChoiceField)
kwargs["choices_form_class"] = form_class
Expand Down

0 comments on commit ff78da3

Please sign in to comment.