diff options
| -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") | 
