diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2014-05-26 20:54:16 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2014-05-26 20:54:16 +0200 | 
| commit | f9043746cbdb4bff525e8e894ed94c3b8a436e06 (patch) | |
| tree | 1e5f1f46e640673d701d74d7783335c4bb795b53 /ishtar_common/forms_common.py | |
| parent | 15f9d571e2012e518a227841dc8223c6e460eeb0 (diff) | |
| download | Ishtar-f9043746cbdb4bff525e8e894ed94c3b8a436e06.tar.bz2 Ishtar-f9043746cbdb4bff525e8e894ed94c3b8a436e06.zip | |
Contextual filter on fields for new items window (refs #1715)
Diffstat (limited to 'ishtar_common/forms_common.py')
| -rw-r--r-- | ishtar_common/forms_common.py | 31 | 
1 files changed, 28 insertions, 3 deletions
| diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 874e2dabf..8964ae57b 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -66,7 +66,29 @@ def get_person_field(label=_(u"Person"), required=True, person_types=[]):      return forms.IntegerField(widget=widget, label=label, required=required,                                validators=[models.valid_id(models.Person)]) -class OrganizationForm(forms.Form): +class NewItemForm(forms.Form): +    def __init__(self, *args, **kwargs): +        self.limits = {} +        if 'limits' in kwargs: +            limits = kwargs.pop('limits') +            if limits: +                for item in limits.split(';'): +                    key, values = item.split('__') +                    self.limits[key] = values.split('-') +        super(NewItemForm, self).__init__(*args, **kwargs) + +    def limit_fields(self): +        for key in self.limits: +            if key in self.fields and hasattr(self.fields[key], 'choices'): +                new_choices = [] +                for value, lbl in self.fields[key].choices: +                    if unicode(value) in self.limits[key]: +                        new_choices.append((value, lbl)) +                self.fields[key].choices = new_choices +                if len(new_choices) == 1: +                    self.fields[key].initial = [new_choices[0][0]] + +class OrganizationForm(NewItemForm):      form_label = _(u"Organization")      associated_models = {'organization_type':models.OrganizationType}      name = forms.CharField(label=_(u"Name"), max_length=300, @@ -93,6 +115,7 @@ class OrganizationForm(forms.Form):                                            models.OrganizationType.get_types()          self.fields['organization_type'].help_text = \                                            models.OrganizationType.get_help() +        self.limit_fields()      def save(self, user):          dct = self.cleaned_data @@ -145,7 +168,7 @@ class PersonFormSelection(forms.Form):                                       PersonSelect, models.Person),           validators=[models.valid_id(models.Person)]) -class SimplePersonForm(forms.Form): +class SimplePersonForm(NewItemForm):      form_label = _("Identity")      associated_models = {'attached_to':models.Organization}      title = forms.ChoiceField(label=_("Title"), choices=models.Person.TYPE) @@ -181,6 +204,7 @@ class PersonForm(SimplePersonForm):          self.fields['person_types'].choices = models.PersonType.get_types(                                                           empty_first=False)          self.fields['person_types'].help_text = models.PersonType.get_help() +        self.limit_fields()      def save(self, user):          dct = self.cleaned_data @@ -320,7 +344,7 @@ class SourceDeletionForm(FinalForm):  # Authors management #  ###################### -class AuthorForm(forms.Form): +class AuthorForm(NewItemForm):      form_label = _(u"Author")      associated_models = {'person':models.Person,                           'author_type':models.AuthorType} @@ -333,6 +357,7 @@ class AuthorForm(forms.Form):      def __init__(self, *args, **kwargs):          super(AuthorForm, self).__init__(*args, **kwargs)          self.fields['author_type'].choices = models.AuthorType.get_types() +        self.limit_fields()      def save(self, user):          dct = self.cleaned_data | 
