diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-15 18:26:26 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-02-15 18:26:26 +0100 |
commit | 4912bc6fa310d6a5294d5c46c29e4ed8dbd7c390 (patch) | |
tree | 8945da56ceb068c8739fbb7faca5e490b2c318e0 /ishtar_common | |
parent | c8f5d324cad4de6b51cc8b954fe3f5b4fc874c47 (diff) | |
download | Ishtar-4912bc6fa310d6a5294d5c46c29e4ed8dbd7c390.tar.bz2 Ishtar-4912bc6fa310d6a5294d5c46c29e4ed8dbd7c390.zip |
Forms: move FieldType management to IshtarForm
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/forms.py | 71 |
1 files changed, 41 insertions, 30 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index eb548f70c..0c8427d9f 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -242,9 +242,36 @@ class FormSet(CustomForm, BaseFormSet): form.fields[DELETION_FIELD_NAME].widget = widgets.DeleteWidget() +class FieldType(object): + def __init__(self, key, model, is_multiple=False, extra_args=None): + self.key = key + self.model = model + self.is_multiple = is_multiple + self.extra_args = extra_args + + def get_choices(self, initial=None): + args = { + 'empty_first': not self.is_multiple, + 'initial': initial + } + if self.extra_args: + args.update(self.extra_args) + return self.model.get_types(**args) + + def get_help(self): + args = {} + if self.extra_args: + args.update(self.extra_args) + return self.model.get_help(**args) + + class IshtarForm(forms.Form): + TYPES = [] # FieldType list + def __init__(self, *args, **kwargs): super(IshtarForm, self).__init__(*args, **kwargs) + for field in self.TYPES: + self._init_type(field) for k in self.fields: if not hasattr(self.fields[k].widget, 'NO_FORM_CONTROL'): cls = 'form-control' @@ -262,6 +289,12 @@ class IshtarForm(forms.Form): widget.options['autoclose'] = 'true' widget.options['todayHighlight'] = 'true' + def _init_type(self, field): + if field.key not in self.fields: + return + self.fields[field.key].choices = field.get_choices() + self.fields[field.key].help_text = field.get_help() + class TableSelect(IshtarForm): def __init__(self, *args, **kwargs): @@ -361,32 +394,7 @@ def get_data_from_formset(data): return values -class FieldType(object): - def __init__(self, key, model, is_multiple=False, extra_args=None): - self.key = key - self.model = model - self.is_multiple = is_multiple - self.extra_args = extra_args - - def get_choices(self, initial=None): - args = { - 'empty_first': not self.is_multiple, - 'initial': initial - } - if self.extra_args: - args.update(self.extra_args) - return self.model.get_types(**args) - - def get_help(self): - args = {} - if self.extra_args: - args.update(self.extra_args) - return self.model.get_help(**args) - - class ManageOldType(IshtarForm): - TYPES = [] # FieldType list - def __init__(self, *args, **kwargs): """ init_data is used to manage deactivated items in list when editing @@ -425,11 +433,14 @@ class ManageOldType(IshtarForm): self.init_data = MultiValueDict(self.init_data) super(ManageOldType, self).__init__(*args, **kwargs) for field in self.TYPES: - if field.key not in self.fields: - continue - self.fields[field.key].choices = field.get_choices( - initial=self.init_data.get(field.key)) - self.fields[field.key].help_text = field.get_help() + self._init_type(field) + + def _init_type(self, field): + if field.key not in self.fields: + return + self.fields[field.key].choices = field.get_choices( + initial=self.init_data.get(field.key)) + self.fields[field.key].help_text = field.get_help() class DocumentGenerationForm(forms.Form): |