diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-13 09:49:09 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-13 09:49:09 +0200 |
commit | 077dea01ed2d9afc97412631e4207eaf4175e06d (patch) | |
tree | 57a32eeff03668f481cbd91ccd6803e56666b36d | |
parent | e3ff6616d2596a4e8d8fa83c1382c48d89fbc9dd (diff) | |
download | Ishtar-077dea01ed2d9afc97412631e4207eaf4175e06d.tar.bz2 Ishtar-077dea01ed2d9afc97412631e4207eaf4175e06d.zip |
Custom forms: manage customization of search forms
-rw-r--r-- | archaeological_context_records/forms.py | 52 | ||||
-rw-r--r-- | ishtar_common/forms.py | 23 | ||||
-rw-r--r-- | ishtar_common/models.py | 2 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 6 | ||||
-rw-r--r-- | ishtar_common/wizards.py | 20 |
5 files changed, 67 insertions, 36 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py index 3238eb771..acfb47d2a 100644 --- a/archaeological_context_records/forms.py +++ b/archaeological_context_records/forms.py @@ -38,7 +38,7 @@ from bootstrap_datepicker.widgets import DatePicker from ishtar_common import widgets from ishtar_common.forms import FinalForm, FormSet, \ reverse_lazy, get_form_selection, TableSelect, ManageOldType, CustomForm, \ - FieldType + FieldType, CustomFormSearch from ishtar_common.forms_common import get_town_field from ishtar_common.models import valid_id, IshtarSiteProfile, Town @@ -57,7 +57,9 @@ class OperationFormSelection(CustomForm, forms.Form): validators=[valid_id(Operation)]) -class RecordSelect(TableSelect): +class RecordSelect(CustomForm, TableSelect): + form_admin_name = _(u"Context record - 001 - Search") + form_slug = "contextrecord-001-search" search_vector = forms.CharField(label=_(u"Full text search"), widget=widgets.SearchWidget) label = forms.CharField(label=_(u"ID"), max_length=100) @@ -88,33 +90,41 @@ class RecordSelect(TableSelect): def __init__(self, *args, **kwargs): super(RecordSelect, self).__init__(*args, **kwargs) - self.fields['datings__period'].choices = Period.get_types() - self.fields['datings__period'].help_text = Period.get_help() - self.fields['unit'].choices = models.Unit.get_types() - self.fields['unit'].help_text = models.Unit.get_help() - self.fields['cr_relation_types'].choices = \ - models.RelationType.get_types(empty_first=False) - self.fields['ope_relation_types'].choices = OpeRelationType.get_types( - empty_first=False) + if 'datings__period' in self.fields: + self.fields['datings__period'].choices = Period.get_types() + self.fields['datings__period'].help_text = Period.get_help() + if 'unit' in self.fields: + self.fields['unit'].choices = models.Unit.get_types() + self.fields['unit'].help_text = models.Unit.get_help() + if 'cr_relation_types' in self.fields: + self.fields['cr_relation_types'].choices = \ + models.RelationType.get_types(empty_first=False) + if 'ope_relation_types' in self.fields: + self.fields['ope_relation_types'].choices = \ + OpeRelationType.get_types(empty_first=False) def get_input_ids(self): ids = super(RecordSelect, self).get_input_ids() - ids.pop(ids.index('parcel')) - ids.append('parcel_0') - ids.append('parcel_1') - ids.append('parcel_2') - ids.pop(ids.index('cr_relation_types')) - for idx, c in enumerate(self.fields['cr_relation_types'].choices): - ids.append('cr_relation_types_{}'.format(idx)) - ids.pop(ids.index('ope_relation_types')) - for idx, c in enumerate(self.fields['ope_relation_types'].choices): - ids.append('ope_relation_types_{}'.format(idx)) + if 'parcel' in ids: + ids.pop(ids.index('parcel')) + ids.append('parcel_0') + ids.append('parcel_1') + ids.append('parcel_2') + if 'cr_relation_types' in ids: + ids.pop(ids.index('cr_relation_types')) + for idx, c in enumerate(self.fields['cr_relation_types'].choices): + ids.append('cr_relation_types_{}'.format(idx)) + if 'ope_relation_types' in ids: + ids.pop(ids.index('ope_relation_types')) + for idx, c in enumerate(self.fields['ope_relation_types'].choices): + ids.append('ope_relation_types_{}'.format(idx)) return ids -class RecordFormSelection(forms.Form): +class RecordFormSelection(CustomFormSearch): form_label = _("Context record search") SEARCH_AND_SELECT = True + associated_models = {'pk': models.ContextRecord} currents = {'pk': models.ContextRecord} pk = forms.IntegerField( diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index 1d17aece7..13ad1cd30 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -266,16 +266,31 @@ class CustomForm(object): # formset fields = cls.form.base_fields customs = [] - for key in fields: + keys = fields.keys() + for key in keys: field = fields[key] - # cannot customize display of required and hidden field - # field with no label are also rejected - if field.required or field.widget.is_hidden or not field.label: + # cannot customize display of required (except in search form) and + # hidden field, search_vector and field with no label + if ('search_vector' not in keys and field.required) or \ + key == 'search_vector' or field.widget.is_hidden or \ + not field.label: continue customs.append((key, field.label)) return sorted(customs, key=lambda x: x[1]) +class CustomFormSearch(forms.Form): + need_user_for_initialization = True + + def __init__(self, *args, **kwargs): + user = None + if 'user' in kwargs: + user = kwargs.pop('user') + super(CustomFormSearch, self).__init__(*args, **kwargs) + if user and 'pk' in self.fields: + self.fields['pk'].widget.user = user + + class FormSet(CustomForm, BaseFormSet): delete_widget = widgets.DeleteWidget diff --git a/ishtar_common/models.py b/ishtar_common/models.py index d073b02c8..e1c4e825d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1836,7 +1836,7 @@ class CustomForm(models.Model): if app_name == "archaeological_files_pdl": app_name = "archaeological_files" for form in dir(app_form): - if 'Form' not in form: + if 'Form' not in form and 'Select' not in form: # not very clean... but do not treat inappropriate items continue form = getattr(app_form, form) diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 6ec0220eb..d1f6e49d3 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -905,6 +905,7 @@ class DataTable(forms.RadioSelect): self.source_full = source_full self.sortname = sortname self.col_prefix = col_prefix + self.user = None if self.col_prefix and not self.col_prefix.endswith('__'): self.col_prefix += "__" @@ -956,7 +957,10 @@ class DataTable(forms.RadioSelect): def render(self, name, value, attrs=None, renderer=None): # t = loader.get_template('blocks/form_flex_snippet.html') t = loader.get_template('blocks/bs_form_snippet.html') - form = self.form() + if self.user: + form = self.form(user=self.user) + else: + form = self.form() rendered = t.render({'form': form, 'search': True}) dct = {} if self.new: diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 979f611ce..4a55345d2 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -87,7 +87,16 @@ def filter_no_fields_form(form, other_check=None): return func -class Wizard(NamedUrlWizardView): +class IshtarWizard(NamedUrlWizardView): + def get_form_kwargs(self, step=None): + kwargs = super(IshtarWizard, self).get_form_kwargs(step) + if hasattr(self.form_list[step], 'need_user_for_initialization') and \ + self.form_list[step].need_user_for_initialization: + kwargs['user'] = self.request.user + return kwargs + + +class Wizard(IshtarWizard): model = None label = '' translated_keys = [] @@ -167,13 +176,6 @@ class Wizard(NamedUrlWizardView): return super(Wizard, self).dispatch(request, *args, **kwargs) - def get_form_kwargs(self, step=None): - kwargs = super(Wizard, self).get_form_kwargs(step) - if hasattr(self.form_list[step], 'need_user_for_initialization') and\ - self.form_list[step].need_user_for_initialization: - kwargs['user'] = self.request.user - return kwargs - def get_prefix(self, request, *args, **kwargs): """As the class name can interfere when reused prefix with the url_name """ @@ -1382,7 +1384,7 @@ class Wizard(NamedUrlWizardView): return initial -class SearchWizard(NamedUrlWizardView): +class SearchWizard(IshtarWizard): model = None label = '' modification = None # True when the wizard modify an item |