summaryrefslogtreecommitdiff
path: root/archaeological_context_records/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-06-04 16:07:33 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-06-12 08:52:35 +0200
commit6d011e9fe04da5969404f6cdd3c082773f0ab936 (patch)
tree52ef9eebdda38ee7575204ad2b54dec0c1966776 /archaeological_context_records/forms.py
parent3bcfbc77e8fe7ba5cffb345514b20bf9c4c20994 (diff)
downloadIshtar-6d011e9fe04da5969404f6cdd3c082773f0ab936.tar.bz2
Ishtar-6d011e9fe04da5969404f6cdd3c082773f0ab936.zip
Context record: add town field for context records (when parcel is not mandatory)
Diffstat (limited to 'archaeological_context_records/forms.py')
-rw-r--r--archaeological_context_records/forms.py25
1 files changed, 22 insertions, 3 deletions
diff --git a/archaeological_context_records/forms.py b/archaeological_context_records/forms.py
index 59700bf15..17e557283 100644
--- a/archaeological_context_records/forms.py
+++ b/archaeological_context_records/forms.py
@@ -28,7 +28,7 @@ from django.core import validators
from django.forms.formsets import formset_factory
from django.utils.translation import ugettext_lazy as _
-from ishtar_common.models import valid_id, IshtarSiteProfile
+from ishtar_common.models import valid_id, IshtarSiteProfile, Town
from archaeological_operations.models import Period, Parcel, Operation, \
ArchaeologicalSite, RelationType as OpeRelationType
import models
@@ -143,12 +143,13 @@ class RecordFormGeneral(CustomForm, ManageOldType):
base_models = ["documentation"]
associated_models = {
'archaeological_site': ArchaeologicalSite,
- 'parcel': Parcel, 'unit': models.Unit,
+ 'parcel': Parcel, 'unit': models.Unit, 'town': Town,
'documentation': models.DocumentationType,
'excavation_technic': models.ExcavationTechnicType}
pk = forms.IntegerField(required=False, widget=forms.HiddenInput)
operation_id = forms.IntegerField(widget=forms.HiddenInput)
- parcel = forms.ChoiceField(label=_("Parcel"), choices=[])
+ parcel = forms.ChoiceField(label=_(u"Parcel"), choices=[])
+ town = forms.ChoiceField(label=_(u"Town"), choices=[], required=False)
archaeological_site = forms.ChoiceField(
label=" ", choices=[], required=False,
help_text=_(u"Only the items associated to the operation can be "
@@ -228,6 +229,9 @@ class RecordFormGeneral(CustomForm, ManageOldType):
self.fields['parcel'].choices = [('', '--')]
if not profile.parcel_mandatory:
self.fields['parcel'].required = False
+ self.fields['town'].choices = [('', '--')]
+ else:
+ self.fields.pop('town')
site_label = IshtarSiteProfile.get_default_site_label()
self.fields['archaeological_site'].label = site_label
@@ -243,6 +247,17 @@ class RecordFormGeneral(CustomForm, ManageOldType):
(" - ".join([k for k in key if k]),
[(parcel.pk, parcel.short_label) for parcel in gparcels])
)
+ if len(self.fields['parcel'].choices) == 1:
+ # only the empty choice is available
+ self.fields.pop('parcel')
+ if 'town' in self.fields:
+ self.fields['town'].required = True
+ if 'town' in self.fields:
+ if self.fields['town'].required:
+ self.fields['town'].choices = [] # remove the empty choice
+ self.fields['town'].choices += [(t.pk, unicode(t))
+ for t in operation.towns.all()]
+
self.fields['archaeological_site'].choices += [
(site.pk, unicode(site))
for site in operation.archaeological_sites.all()
@@ -260,6 +275,10 @@ class RecordFormGeneral(CustomForm, ManageOldType):
if cr.count():
raise forms.ValidationError(_(u"This ID already exists for "
u"this operation."))
+ if not self.cleaned_data.get('parcel', None) and not \
+ self.cleaned_data.get('town', None):
+ raise forms.ValidationError(_(u"You have to choose a town or a "
+ u"parcel."))
return cleaned_data