diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-05-11 01:03:22 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-05-11 01:03:22 +0200 |
commit | f3b115804f8d4266f7ffb761eaa77012defff553 (patch) | |
tree | c597c6810846be8bf8901b228cd939d3f3722071 | |
parent | 2ecc83fd1ed703626195c2189b537dfa5e8c897c (diff) | |
download | Ishtar-f3b115804f8d4266f7ffb761eaa77012defff553.tar.bz2 Ishtar-f3b115804f8d4266f7ffb761eaa77012defff553.zip |
Correct the town selection (closes #433)
-rw-r--r-- | ishtar/furnitures/forms.py | 109 |
1 files changed, 75 insertions, 34 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index a8fc5d216..54d1d6af7 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -1387,26 +1387,44 @@ class OperationWizard(FileWizard): model = models.Operation object_parcel_type = 'operation' - def process_post_request(self, request, storage, *args, **kwargs): + def get_template(self, request, storage): + templates = super(OperationWizard, self).get_template(request, storage) + current_step = storage.get_current_step() or self.get_first_step( + request, storage) + if current_step.startswith('towns-'): + templates = ['towns_wizard.html'] + templates + return templates + + def get_extra_context(self, request, storage): """ - Change the town (and parcel) form to a free selection town's (parcel's) - form if no archaelogical file is provided + Return extra context for templates """ - #!TODO manage with condition list - file_key = 'general-' + self.url_name + '-associated_file' - if file_key in request.POST.keys(): - town_form_key = 'towns-' + self.url_name - parcel_form_key = 'parcels-' + self.url_name - if request.POST[file_key]: - self.form_list[unicode(town_form_key)] = SelectedTownFormSet - self.form_list[unicode(parcel_form_key)] = SelectedParcelFormSet - else: - self.form_list[unicode(town_form_key)] = \ - SelectedTownGeneralFormSet - self.form_list[unicode(parcel_form_key)] = \ - SelectedParcelGeneralFormSet - return super(OperationWizard, self).process_post_request(request, - storage, *args, **kwargs) + context = super(OperationWizard, self).get_extra_context(request, + storage) + step = self.determine_step(request, storage) + if not step.startswith('towns-'): + return context + context['TOWNS'] = self.get_towns(request, storage) + return context + + def get_towns(self, request, storage): + """ + Obtention des villes disponibles + """ + general_form_key = 'general-' + self.url_name + towns = [] + file_id = self.session_get_value(request, storage, general_form_key, + "associated_file") + if file_id: + try: + for town in models.File.objects.get(pk=int(file_id) + ).towns.all(): + towns.append((town.pk, unicode(town))) + except (ValueError, ObjectDoesNotExist): + pass + return sorted(towns, key=lambda x:x[1]) + else: + return -1 def get_form(self, request, storage, step=None, data=None, files=None): """ @@ -1430,19 +1448,8 @@ class OperationWizard(FileWizard): data[prefix+'-hidden_year'] = year data[prefix+'-hidden_ope'] = True # manage the dynamic choice of towns - if step.startswith('towns-') and hasattr(form, 'management_form') \ - and self.session_has_key(request, storage, general_form_key): - towns = [] - file_id = self.session_get_value(request, storage, general_form_key, - "associated_file") - if file_id: - try: - for town in models.File.objects.get(pk=int(file_id) - ).towns.all(): - towns.append((town.pk, unicode(town))) - except (ValueError, ObjectDoesNotExist): - pass - data['TOWNS'] = sorted(towns, key=lambda x:x[1]) + if step.startswith('towns-') and hasattr(form, 'management_form'): + data['TOWNS'] = self.get_towns(request, storage) elif step.startswith('parcels-') and hasattr(form, 'management_form'): file_id = self.session_get_value(request, storage, general_form_key, "associated_file") @@ -1632,7 +1639,7 @@ class SelectedTownForm(forms.Form): if 'files' in kwargs: kwargs.pop('files') super(SelectedTownForm, self).__init__(*args, **kwargs) - if towns: + if towns and towns != -1: self.fields['town'].choices = [('', '--')] + towns SelectedTownFormSet = formset_factory(SelectedTownForm, can_delete=True, @@ -1705,12 +1712,30 @@ operation_search_wizard = SearchWizard([ ('general-operation_search', OperationFormSelection)], url_name='operation_search',) +def has_associated_file(form_name, file_key='associated_file', negate=False): + def func(self, request, storage): + if storage.prefix not in request.session or \ + 'step_data' not in request.session[storage.prefix] or \ + form_name not in request.session[storage.prefix]['step_data'] or\ + form_name + '-' + file_key not in \ + request.session[storage.prefix]['step_data'][form_name]: + return negate + try: + file_id = int(request.session[storage.prefix]['step_data']\ + [form_name][form_name+'-'+file_key]) + return not negate + except ValueError: + return negate + return func + operation_creation_wizard = OperationWizard([ ('general-operation_creation', OperationFormGeneral), ('refs-operation_creation', OperationFormReference), ('preventive-operation_creation', OperationFormPreventive), ('preventivediag-operation_creation', OperationFormPreventiveDiag), + ('townsgeneral-operation_creation', SelectedTownGeneralFormSet), ('towns-operation_creation', SelectedTownFormSet), + ('parcelsgeneral-operation_creation', SelectedParcelGeneralFormSet), ('parcels-operation_creation', SelectedParcelFormSet), ('remains-operation_creation', RemainFormset), ('periods-operation_creation', PeriodFormset), @@ -1719,7 +1744,13 @@ operation_creation_wizard = OperationWizard([ 'preventive-operation_creation':is_preventive('general-operation_creation', models.OperationType, 'operation_type', 'prev_excavation'), 'preventivediag-operation_creation':is_preventive('general-operation_creation', - models.OperationType, 'operation_type', 'arch_diagnostic') + models.OperationType, 'operation_type', 'arch_diagnostic'), +'townsgeneral-operation_creation':has_associated_file( + 'general-operation_creation', negate=True), +'towns-operation_creation':has_associated_file('general-operation_creation'), +'parcelsgeneral-operation_creation':has_associated_file( + 'general-operation_creation', negate=True), +'parcels-operation_creation':has_associated_file('general-operation_creation'), }, url_name='operation_creation',) @@ -1730,7 +1761,9 @@ operation_modification_wizard = OperationWizard([ ('preventive-operation_modification', OperationFormPreventive), ('preventivediag-operation_modification', OperationFormPreventiveDiag), ('towns-operation_modification', SelectedTownFormSet), + ('townsgeneral-operation_modification', SelectedTownGeneralFormSet), ('parcels-operation_modification', SelectedParcelFormSet), + ('parcelsgeneral-operation_modification', SelectedParcelGeneralFormSet), ('remains-operation_modification', RemainFormset), ('periods-operation_modification', PeriodFormset), ('final-operation_modification', FinalForm)], @@ -1740,7 +1773,15 @@ operation_modification_wizard = OperationWizard([ 'operation_type', 'prev_excavation'), 'preventivediag-operation_modification':is_preventive( 'general-operation_modification', models.OperationType, - 'operation_type', 'arch_diagnostic') + 'operation_type', 'arch_diagnostic'), +'townsgeneral-operation_modification':has_associated_file( + 'general-operation_modification', negate=True), +'towns-operation_modification':has_associated_file( + 'general-operation_modification'), +'parcelsgeneral-operation_modification':has_associated_file( + 'general-operation_creation', negate=True), +'parcels-operation_modification':has_associated_file( + 'general-operation_modification'), }, url_name='operation_modification',) |