From e50e6712077a7b4911672088838e203db41ffb80 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 26 May 2014 20:54:16 +0200 Subject: Contextual filter on fields for new items window (refs #1715) --- ishtar_common/forms_common.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'ishtar_common/forms_common.py') 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 -- cgit v1.2.3