diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2014-01-16 21:20:32 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2014-01-16 21:20:32 +0100 |
commit | 2c9c125831cd3b91fe9c9899d6a4246ed9962e60 (patch) | |
tree | 3e2437cb74974666c1ad41958bdc85ccf15a31e3 | |
parent | 237bec23f4058618fed454528a31f725da563465 (diff) | |
download | Ishtar-2c9c125831cd3b91fe9c9899d6a4246ed9962e60.tar.bz2 Ishtar-2c9c125831cd3b91fe9c9899d6a4246ed9962e60.zip |
More explicit town choice for parcel full text input (refs #1611)
-rw-r--r-- | archaeological_operations/forms.py | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 76d7055d9..7d7b79d63 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -103,6 +103,8 @@ class ParcelForm(forms.Form): return self.cleaned_data class ParcelSelectionForm(forms.Form): + _town = forms.ChoiceField(label=_("Town"), choices=(), required=False, + validators=[valid_id(models.Town)]) _parcel_selection = forms.CharField(label=_(u"Full text input"), widget=SelectParcelWidget(attrs={'class':'parcel-select'}), max_length=100, help_text=_(u"example: \"2013: XD:1 to "\ @@ -118,6 +120,13 @@ class ParcelFormSet(FormSet): if self.forms[0].__class__.__name__ == 'ParcelForm': self.selection_form = ParcelSelectionForm() self.extra_form = self.selection_form + # copy town choices + town_choices = self.forms[0].fields['town'].choices[:] + if town_choices and not town_choices[0][0]: + # remove empty + town_choices = town_choices[1:] + if town_choices: + self.selection_form.fields['_town'].choices = town_choices def as_table(self): # add dynamic widget @@ -142,25 +151,14 @@ class ParcelFormSet(FormSet): def clean(self): # manage parcel selection - last_town, parcels = None, [] - if hasattr(self, 'cleaned_data') and self.cleaned_data: - for parcel in reversed(self.cleaned_data): - if parcel.get('town'): - last_town = parcel.get('town') - break - if not last_town and 'town' in self.forms[0].fields.keys(): - towns = self.forms[0].fields['town'].choices - if towns: - towns.pop(0) # remove first empty - if towns: - last_town = towns[0][0] + selected_town, parcels = None, [] if self.data.get('_parcel_selection'): parcels = parse_parcels(self.data['_parcel_selection']) - if last_town: - for idx, parcel in enumerate(parcels): - parcel['town'] = last_town - parcel['DELETE'] = False - parcels[idx] = parcel + selected_town = self.data.get('_town') + for idx, parcel in enumerate(parcels): + parcel['town'] = selected_town + parcel['DELETE'] = False + parcels[idx] = parcel c_max = self.total_form_count() # pop the last extra form extra_form = self.forms.pop() |