diff --git a/adhocracy4/categories/fields.py b/adhocracy4/categories/fields.py index ead5b081f..16d4de246 100644 --- a/adhocracy4/categories/fields.py +++ b/adhocracy4/categories/fields.py @@ -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 @@ -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) diff --git a/adhocracy4/categories/form_fields.py b/adhocracy4/categories/form_fields.py index 05251c66a..08c53a587 100644 --- a/adhocracy4/categories/form_fields.py +++ b/adhocracy4/categories/form_fields.py @@ -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) @@ -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): diff --git a/adhocracy4/categories/forms.py b/adhocracy4/categories/forms.py index 55ba172db..513e0a042 100644 --- a/adhocracy4/categories/forms.py +++ b/adhocracy4/categories/forms.py @@ -7,6 +7,7 @@ from adhocracy4.modules import models as module_models from . import has_icons +from .form_fields import CategoryChoiceField class CategorizableFieldMixin: @@ -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",) diff --git a/adhocracy4/categories/models.py b/adhocracy4/categories/models.py index 38d0e165e..391971bd9 100644 --- a/adhocracy4/categories/models.py +++ b/adhocracy4/categories/models.py @@ -1,4 +1,3 @@ -from django.conf import settings from django.db import models from django.utils.translation import gettext_lazy as _ @@ -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 diff --git a/changelog/0006.md b/changelog/0006.md new file mode 100644 index 000000000..77414e4ef --- /dev/null +++ b/changelog/0006.md @@ -0,0 +1,4 @@ +### Changed + +- apps/categories: change CategoryChoiceField for icons from moded field to simple field + (was creating migrations in forked projects)