diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-14 07:52:45 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-14 08:21:39 +0200 |
commit | f94253032fa1bece95b546c36375ecba8331f9f4 (patch) | |
tree | c79af30ff74553198c5fa5844c74f2c0c2fc1850 | |
parent | 241354cc20c9f2c19a73930d9d25bec38829bf52 (diff) | |
download | Ishtar-f94253032fa1bece95b546c36375ecba8331f9f4.tar.bz2 Ishtar-f94253032fa1bece95b546c36375ecba8331f9f4.zip |
Multi select form: small refactoring
-rw-r--r-- | ishtar_common/forms.py | 55 | ||||
-rw-r--r-- | ishtar_common/forms_common.py | 9 |
2 files changed, 39 insertions, 25 deletions
diff --git a/ishtar_common/forms.py b/ishtar_common/forms.py index ae7c6ca14..04cd5a327 100644 --- a/ishtar_common/forms.py +++ b/ishtar_common/forms.py @@ -372,13 +372,44 @@ class CustomForm(BSForm): return sorted(customs, key=lambda x: x[1]) -class MultiSearchForm(object): - current_model = None - pk_key = None +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 MultiSearchForm(CustomFormSearch): + SEARCH_AND_SELECT = True + pk_key = 'pks' + associated_models = {} + + def __init__(self, *args, **kwargs): + super(MultiSearchForm, self).__init__(*args, **kwargs) + if "pk" not in self.fields: + raise NotImplementedError("A \"pk\" field must be defined") + if self.pk_key not in self.associated_models: + raise NotImplementedError("\"{}\" must be defined in " + "associated_models") + self.fields['pk'].required = True + self.fields[self.pk_key] = self.fields.pop('pk') + + @classmethod + def get_current_model(cls): + return cls.associated_models[cls.pk_key] @classmethod def get_formated_datas(cls, cleaned_datas): - if not cls.current_model or not cls.pk_key: + if hasattr(cls, "current_model"): + current_model = cls.current_model + else: + current_model = cls.get_current_model() + if not current_model or not cls.pk_key: return [] items = [] for data in cleaned_datas: @@ -390,9 +421,9 @@ class MultiSearchForm(object): continue try: items.append( - str(cls.current_model.objects.get(pk=int(pk))) + str(current_model.objects.get(pk=int(pk))) ) - except (cls.current_model.DoesNotExist, ValueError): + except (current_model.DoesNotExist, ValueError): continue return [ (u"", @@ -403,18 +434,6 @@ class MultiSearchForm(object): ] -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/forms_common.py b/ishtar_common/forms_common.py index 42bf90691..395b54b02 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -516,8 +516,8 @@ class PersonFormSelection(CustomFormSearch): validators=[models.valid_id(models.Person)]) -class PersonFormMultiSelection(MultiSearchForm, PersonFormSelection): - pk_key = 'pks' +class PersonFormMultiSelection(MultiSearchForm): + form_label = _(u"Person search") associated_models = {'pks': models.Person} pk = forms.CharField( @@ -529,11 +529,6 @@ class PersonFormMultiSelection(MultiSearchForm, PersonFormSelection): source_full=reverse_lazy('get-person-full')), validators=[models.valid_ids(models.Person)]) - def __init__(self, *args, **kwargs): - super(MultiSearchForm, self).__init__(*args, **kwargs) - self.fields['pk'].required = True - self.fields[self.pk_key] = self.fields.pop('pk') - class QAPersonFormMulti(QAForm): form_admin_name = _(u"Person - Quick action - Modify") |