summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--archaeological_files/forms.py8
-rw-r--r--archaeological_operations/forms.py17
-rw-r--r--ishtar_common/forms_common.py31
-rw-r--r--ishtar_common/static/media/style.css1
-rw-r--r--ishtar_common/urls.py6
-rw-r--r--ishtar_common/views.py7
-rw-r--r--ishtar_common/widgets.py12
7 files changed, 63 insertions, 19 deletions
diff --git a/archaeological_files/forms.py b/archaeological_files/forms.py
index 0aab8d370..f155f4261 100644
--- a/archaeological_files/forms.py
+++ b/archaeological_files/forms.py
@@ -127,6 +127,7 @@ class FileFormGeneral(forms.Form):
in_charge = forms.IntegerField(label=_("Person in charge"),
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person',
args=[PersonType.objects.get(txt_idx='sra_agent').pk]),
+ limit={'person_types':[PersonType.objects.get(txt_idx='sra_agent').pk]},
associated_model=Person, new=True),
validators=[valid_id(Person)])
year = forms.IntegerField(label=_("Year"),
@@ -186,6 +187,8 @@ class FileFormPreventive(forms.Form):
widget=widgets.JQueryAutoComplete(
reverse_lazy('autocomplete-person',
args=[PersonType.objects.get(txt_idx='general_contractor').pk]),
+ limit={'person_types':
+ [PersonType.objects.get(txt_idx='general_contractor').pk]},
associated_model=Person, new=True),
validators=[valid_id(Person)])
responsible_town_planning_service = forms.IntegerField(required=False,
@@ -193,6 +196,8 @@ class FileFormPreventive(forms.Form):
widget=widgets.JQueryAutoComplete(
reverse_lazy('autocomplete-person',
args=[RESPONSIBLE_PLANNING_SERVICE.pk]),
+ limit={'person_types':
+ [RESPONSIBLE_PLANNING_SERVICE.pk]},
associated_model=Person, new=True),
validators=[valid_id(Person)])
permit_type = forms.ChoiceField(label=_(u"Permit type"), required=False,
@@ -232,6 +237,9 @@ class FileFormResearch(forms.Form):
args=["_".join(
[unicode(PersonType.objects.get(txt_idx='head_scientist').pk),
unicode(PersonType.objects.get(txt_idx='sra_agent').pk)])]),
+ limit={'person_types':
+ [unicode(PersonType.objects.get(txt_idx='head_scientist').pk),
+ unicode(PersonType.objects.get(txt_idx='sra_agent').pk)]},
associated_model=Person, new=True), label=_(u"Scientist in charge"))
requested_operation_type = forms.ChoiceField(
label=_(u"Requested operation type"),
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py
index 4473d7858..a426eaf09 100644
--- a/archaeological_operations/forms.py
+++ b/archaeological_operations/forms.py
@@ -315,18 +315,22 @@ class OperationFormGeneral(forms.Form):
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person',
args=["_".join(
[unicode(HEAD_SCIENTIST.pk), unicode(SRA_AGENT.pk)])]),
- associated_model=Person, new=True),
+ associated_model=Person,
+ limit={'person_types':(HEAD_SCIENTIST.pk, SRA_AGENT.pk)},
+ new=True),
validators=[valid_id(Person)], required=False)
operator = forms.IntegerField(label=_("Operator"),
widget=widgets.JQueryAutoComplete(reverse_lazy(
'autocomplete-organization', args=[OPERATOR.pk]),
+ limit={'organization_type':(OPERATOR.pk,)},
associated_model=Organization, new=True),
validators=[valid_id(Organization)], required=False)
in_charge = forms.IntegerField(label=_("In charge"),
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person',
- args=["_".join(
- [unicode(PersonType.objects.get(txt_idx='sra_agent').pk)])]),
- associated_model=Person, new=True),
+ args=["_".join([unicode(SRA_AGENT.pk)])]),
+ associated_model=Person,
+ limit={'person_types':[SRA_AGENT.pk]},
+ new=True),
validators=[valid_id(Person)], required=False)
operation_type = forms.ChoiceField(label=_(u"Operation type"),
choices=[])
@@ -368,9 +372,8 @@ class OperationFormGeneral(forms.Form):
widget=widgets.JQueryDate)
cira_rapporteur = forms.IntegerField(label=u"Rapporteur CIRA",
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person',
- args=["_".join(
- [unicode(PersonType.objects.get(txt_idx='head_scientist').pk),
- unicode(PersonType.objects.get(txt_idx='sra_agent').pk)])]),
+ args=["_".join([unicode(HEAD_SCIENTIST.pk), unicode(SRA_AGENT.pk)])]),
+ limit={'person_types':[SRA_AGENT.pk, HEAD_SCIENTIST.pk]},
associated_model=Person, new=True),
validators=[valid_id(Person)], required=False)
comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea,
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<parent_name>.+)?/$',
+ url(r'new-person/(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$',
'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<parent_name>.+)?/$',
+ url(r'new-author/(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$',
'new_author', name='new-author'),
url(r'autocomplete-author/$', 'autocomplete_author',
name='autocomplete-author'),
- url(r'new-organization/(?P<parent_name>.+)?/$',
+ url(r'new-organization/(?:(?P<parent_name>[^/]+)/)?(?:(?P<limits>[^/]+)/)?$',
'new_organization', name='new-organization'),
url(r'get-organization/(?P<type>.+)?$', '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' <a href="#" class="add-button" '\
u'onclick="open_window(\'%s\');">+</a>' % url_new
html = u'''<input%(attrs_select)s/>%(new)s\