summaryrefslogtreecommitdiff
path: root/archaeological_operations/forms.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2016-08-17 13:52:44 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2016-08-17 13:53:44 +0200
commitc3bb366cb00fd1eaf7d1487a7c8e46b14659d081 (patch)
tree8c0b91b41ceb45dcfc72bb3ac8d55bc1b945089c /archaeological_operations/forms.py
parent6f78867c8dfa8adb58f8d072d71ee8e52dd660fa (diff)
downloadIshtar-c3bb366cb00fd1eaf7d1487a7c8e46b14659d081.tar.bz2
Ishtar-c3bb366cb00fd1eaf7d1487a7c8e46b14659d081.zip
Parcels: add public domain field - better management of parcel formsets (refs #2284)
Diffstat (limited to 'archaeological_operations/forms.py')
-rw-r--r--archaeological_operations/forms.py60
1 files changed, 43 insertions, 17 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py
index f71129615..644114619 100644
--- a/archaeological_operations/forms.py
+++ b/archaeological_operations/forms.py
@@ -45,7 +45,7 @@ from widgets import ParcelWidget, SelectParcelWidget
from ishtar_common import widgets
from ishtar_common.forms import FinalForm, FormSet, get_now, \
- reverse_lazy, get_form_selection, TableSelect
+ reverse_lazy, get_form_selection, TableSelect, get_data_from_formset
from ishtar_common.forms_common import TownFormSet, SourceForm, SourceSelect, \
get_town_field
@@ -76,6 +76,22 @@ class ParcelForm(forms.Form):
parcel_number = forms.CharField(
label=_(u"Parcel number"), required=False,
validators=[validators.MaxLengthValidator(6)])
+ public_domain = forms.BooleanField(label=_(u"Public domain"),
+ initial=False, required=False)
+
+ def count_valid_fields(self, data):
+ if not data:
+ return 0
+ data = get_data_from_formset(data)
+ nb = len(data)
+ # remove last non relevant fields
+ for idx, vals in enumerate(reversed(data[:])):
+ if 'public_domain' in vals:
+ break
+ if 'section' in vals and 'parcel_number' in vals:
+ break
+ nb -= 1
+ return nb
def __init__(self, *args, **kwargs):
towns = None
@@ -99,13 +115,12 @@ class ParcelForm(forms.Form):
if not self.cleaned_data or (DELETION_FIELD_NAME in self.cleaned_data
and self.cleaned_data[DELETION_FIELD_NAME]):
return
- if not self.cleaned_data.get('parcel_number') and \
- self.cleaned_data.get('section') != 'DP':
+ if (not self.cleaned_data.get('parcel_number') or
+ not self.cleaned_data.get('section')) and \
+ not self.cleaned_data.get('public_domain'):
return {}
- for key in ('town', 'section'):
- if key not in self.cleaned_data or not self.cleaned_data[key]:
- raise forms.ValidationError(_(u"Town section and parcel number"
- u" fields are required."))
+ if not self.cleaned_data.get('town'):
+ raise forms.ValidationError(_(u"Town section is required."))
return self.cleaned_data
@classmethod
@@ -141,7 +156,9 @@ class ParcelForm(forms.Form):
else:
c_number = 0
values = [town, data.get('year') or '', data.get('section') or '',
- c_number, data.get('parcel_number') or '']
+ c_number, str(data.get('parcel_number') or '')
+ + str(u"Public domain") if data.get('public_domain')
+ else u""]
if data.get('DELETE'):
deleted.append(values)
else:
@@ -247,15 +264,17 @@ class ParcelFormSet(FormSet):
parcel.parcel_number]
continue
if number not in ordering_keys:
- ordering_keys[number] = ['', '', '', '']
- if field == 'town':
+ ordering_keys[number] = ['', '', '', '', '']
+ if field == 'public_domain':
ordering_keys[number][0] = value
- elif field == 'year':
+ elif field == 'town':
ordering_keys[number][1] = value
- elif field == 'section':
+ elif field == 'year':
ordering_keys[number][2] = value
- elif field == 'parcel_number':
+ elif field == 'section':
ordering_keys[number][3] = value
+ elif field == 'parcel_number':
+ ordering_keys[number][4] = value
reverse_ordering_keys = {}
for number in ordering_keys:
@@ -271,7 +290,7 @@ class ParcelFormSet(FormSet):
return new_values
def _parcel_sorting(self, key):
- town, year, section, parcel = key
+ public_domain, town, year, section, parcel = key
# deal with parcel_number such as '34p' and convert to int
parcel_number = ''
for p in parcel:
@@ -283,7 +302,12 @@ class ParcelFormSet(FormSet):
parcel_number = 0
else:
parcel_number = int(parcel_number)
- return (town, year, section, parcel_number)
+ # empty must be at the end
+ if not year and not section and not parcel_number \
+ and not public_domain:
+ Z = 'ZZZZZZZZZZZZZ'
+ return (Z, Z, Z, Z, Z)
+ return (town, year, section, parcel_number, public_domain)
def as_table(self):
# add dynamic widget
@@ -436,7 +460,7 @@ class OperationSelect(TableSelect):
code_patriarche = forms.IntegerField(
label="Numéro d'opération (OA Patriarche)")
towns = get_town_field()
- parcel = ParcelField(label=_("Parcel (section/number)"))
+ parcel = ParcelField(label=_("Parcel (section/number/public domain)"))
if settings.ISHTAR_DPTS:
towns__numero_insee__startswith = forms.ChoiceField(
label=_(u"Department"), choices=[])
@@ -536,6 +560,7 @@ class OperationSelect(TableSelect):
ids.pop(ids.index('parcel'))
ids.append('parcel_0')
ids.append('parcel_1')
+ ids.append('parcel_2')
ids.pop(ids.index('relation_types'))
for idx, c in enumerate(self.fields['relation_types'].choices):
ids.append('relation_types_{}'.format(idx))
@@ -1160,7 +1185,7 @@ class AdministrativeActOpeSelect(TableSelect):
act_type = forms.ChoiceField(label=_("Act type"), choices=[])
indexed = forms.NullBooleanField(label=_(u"Indexed?"))
operation__towns = get_town_field()
- parcel = ParcelField(label=_("Parcel (section/number)"))
+ parcel = ParcelField(label=_("Parcel (section/number/public domain)"))
if settings.ISHTAR_DPTS:
operation__towns__numero_insee__startswith = forms.ChoiceField(
label=_(u"Department"), choices=[])
@@ -1196,6 +1221,7 @@ class AdministrativeActOpeSelect(TableSelect):
ids.pop(ids.index('parcel'))
ids.append('parcel_0')
ids.append('parcel_1')
+ ids.append('parcel_2')
return ids