summaryrefslogtreecommitdiff
path: root/ishtar_common/forms.py
diff options
context:
space:
mode:
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