diff options
Diffstat (limited to 'ishtar_common/forms.py')
-rw-r--r-- | ishtar_common/forms.py | 55 |
1 files changed, 37 insertions, 18 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 |