summaryrefslogtreecommitdiff
path: root/ishtar_common/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-11-28 17:58:55 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2025-02-19 14:45:56 +0100
commitd0760b4da854e1dadb4ff130468a4c6d185b1abc (patch)
tree464aab890c5d8ea8b51f6ad3558446b3c8018036 /ishtar_common/forms.py
parentf10b03c55ece933e4277cdf1e7d4acfba9fdd7ed (diff)
downloadIshtar-d0760b4da854e1dadb4ff130468a4c6d185b1abc.tar.bz2
Ishtar-d0760b4da854e1dadb4ff130468a4c6d185b1abc.zip
✨ exhibition: forms/sheets
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r--ishtar_common/forms.py31
1 files changed, 28 insertions, 3 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py
index ca9f37623..6531ad6bb 100644
--- a/ishtar_common/forms.py
+++ b/ishtar_common/forms.py
@@ -627,15 +627,28 @@ class FormSetWithDeleteSwitches(FormSet):
delete_widget = widgets.DeleteSwitchWidget
-class FieldType(object):
- def __init__(self, key, model, is_multiple=False, extra_args=None):
+class FieldType:
+ """
+ Define field choices, help for SELECT field from a model.
+ :key: fields key for the form
+ :model: associated model
+ :is_multiple: True if multi select
+ :extra_args: extra args for 'get_types' call
+ :empty_first: first entry is empty. True by default. Always False when multiple
+ """
+
+ def __init__(self, key, model, is_multiple=False, extra_args=None,
+ empty_first=True):
self.key = key
self.model = model
self.is_multiple = is_multiple
self.extra_args = extra_args
+ if self.is_multiple:
+ empty_first = False
+ self.empty_first = empty_first
def get_choices(self, initial=None):
- args = {"empty_first": not self.is_multiple, "initial": initial}
+ args = {"empty_first": self.empty_first, "initial": initial}
if self.extra_args:
args.update(self.extra_args)
return self.model.get_types(**args)
@@ -779,6 +792,18 @@ class IshtarForm(BSForm, forms.Form):
self.fields[field.key].choices = field.get_choices()
self.fields[field.key].help_text = field.get_help()
+ def _clean_model_field(self, key, model):
+ """
+ Clean autocomplete field returning integer associated to a model.
+ """
+ value = self.cleaned_data.get(key, None)
+ if not value:
+ return
+ try:
+ return model.objects.get(pk=int(value))
+ except (model.DoesNotExist, ValueError):
+ return
+
def get_headers(self):
if self._headers:
return self._headers