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 ++++++++++++++++++++++++++++--- ishtar_common/static/media/style.css | 1 + ishtar_common/urls.py | 6 +++--- ishtar_common/views.py | 7 +++---- ishtar_common/widgets.py | 12 ++++++++++-- 5 files changed, 45 insertions(+), 12 deletions(-) (limited to 'ishtar_common') 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 diff --git a/ishtar_common/static/media/style.css b/ishtar_common/static/media/style.css index 8b05603df..d2dda0758 100644 --- a/ishtar_common/static/media/style.css +++ b/ishtar_common/static/media/style.css @@ -414,6 +414,7 @@ div.form { margin-left:auto; margin-right:auto; width:600px; + background-color:#F1F2F6; } .form table th{ diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index 94e50d161..c46f555e9 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -65,7 +65,7 @@ urlpatterns += patterns('ishtar_common.views', name='dashboard-main'), url(r'update-current-item/$', 'update_current_item', name='update-current-item'), - url(r'new-person/(?P.+)?/$', + url(r'new-person/(?:(?P[^/]+)/)?(?:(?P[^/]+)/)?$', 'new_person', name='new-person'), url(r'autocomplete-person(?:/([0-9_]+))?/(user)?$', 'autocomplete_person', name='autocomplete-person'), @@ -77,11 +77,11 @@ urlpatterns += patterns('ishtar_common.views', name='autocomplete-town'), url(r'autocomplete-department/?$', 'autocomplete_department', name='autocomplete-department'), - url(r'new-author/(?P.+)?/$', + url(r'new-author/(?:(?P[^/]+)/)?(?:(?P[^/]+)/)?$', 'new_author', name='new-author'), url(r'autocomplete-author/$', 'autocomplete_author', name='autocomplete-author'), - url(r'new-organization/(?P.+)?/$', + url(r'new-organization/(?:(?P[^/]+)/)?(?:(?P[^/]+)/)?$', 'new_organization', name='new-organization'), url(r'get-organization/(?P.+)?$', 'get_organization', name='get-organization'), diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 8fbf41759..11351d7c0 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -636,15 +636,14 @@ def autocomplete_author(request): return HttpResponse(data, mimetype='text/plain') def new_item(model, frm): - def func(request, parent_name): + def func(request, parent_name, limits=''): model_name = model._meta.object_name if not check_permission(request, 'add_'+model_name.lower()): not_permitted_msg = ugettext(u"Operation not permitted.") return HttpResponse(not_permitted_msg) - #frm = getattr(ishtar_forms, model_name + 'Form') dct = {'title':unicode(_(u'New %s' % model_name.lower()))} if request.method == 'POST': - dct['form'] = frm(request.POST) + dct['form'] = frm(request.POST, limits=limits) if dct['form'].is_valid(): new_item = dct['form'].save(request.user) dct['new_item_label'] = unicode(new_item) @@ -657,7 +656,7 @@ def new_item(model, frm): return render_to_response('window.html', dct, context_instance=RequestContext(request)) else: - dct['form'] = frm() + dct['form'] = frm(limits=limits) return render_to_response('window.html', dct, context_instance=RequestContext(request)) return func diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 2105b73e3..71d88d062 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -134,7 +134,7 @@ class JQueryDate(forms.TextInput): class JQueryAutoComplete(forms.TextInput): def __init__(self, source, associated_model=None, options={}, attrs={}, - new=False, multiple=False): + new=False, multiple=False, limit={}): """ Source can be a list containing the autocomplete values or a string containing the url used for the request. @@ -148,6 +148,7 @@ class JQueryAutoComplete(forms.TextInput): self.attrs.update(attrs) self.new = new self.multiple = multiple + self.limit = limit def value_from_datadict(self, data, files, name): if self.multiple: @@ -222,7 +223,14 @@ class JQueryAutoComplete(forms.TextInput): new = '' if self.new: model_name = self.associated_model._meta.object_name.lower() - url_new = reverse('new-' + model_name, args=[attrs_select['id']]) + limits = [] + for k in self.limit: + limits.append(k + "__" + "-".join( + [unicode(v) for v in self.limit[k]])) + args = [attrs_select['id']] + if limits: + args.append(';'.join(limits)) + url_new = reverse('new-' + model_name, args=args) new = u' +' % url_new html = u'''%(new)s\ -- cgit v1.2.3