From 077dea01ed2d9afc97412631e4207eaf4175e06d Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 13 Jun 2018 09:49:09 +0200 Subject: Custom forms: manage customization of search forms --- ishtar_common/forms.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'ishtar_common/forms.py') 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 -- cgit v1.2.3