diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2014-04-03 13:42:20 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2014-04-03 13:42:20 +0200 |
commit | f03c9a7f969d66d4b7ccb945e14f567022f59528 (patch) | |
tree | 4124a46812c68edb65c50679567e93213c6507e6 | |
parent | 3bf841c2a36981417115fa4168e5565b53fa29ff (diff) | |
download | Ishtar-f03c9a7f969d66d4b7ccb945e14f567022f59528.tar.bz2 Ishtar-f03c9a7f969d66d4b7ccb945e14f567022f59528.zip |
Automatic copy of associated archaeological file on operation creation (refs #1745)
-rw-r--r-- | archaeological_operations/forms.py | 35 | ||||
-rw-r--r-- | archaeological_operations/views.py | 1 | ||||
-rw-r--r-- | archaeological_operations/wizards.py | 85 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 5 | ||||
-rw-r--r-- | ishtar_common/wizards.py | 2 |
5 files changed, 86 insertions, 42 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index d10019aa8..af0dbb1ac 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -275,6 +275,15 @@ class OperationCodeInput(forms.HiddenInput): 'url':reverse_lazy('get_available_operation_code')} return mark_safe(rendered + js) +class OperationFormFileChoice(forms.Form): + form_label = _(u"Associated file") + associated_models = {'associated_file':File,} + currents = {'associated_file':File} + associated_file = forms.IntegerField(label=_(u"Archaelogical file"), + widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), + associated_model=File), + validators=[valid_id(File)], required=False) + class OperationFormGeneral(forms.Form): form_label = _(u"General") base_model = 'archaeological_site' @@ -282,10 +291,8 @@ class OperationFormGeneral(forms.Form): 'in_charge':Person, 'cira_rapporteur':Person, 'operator':Organization, - 'associated_file':File, 'operation_type':models.OperationType, 'archaeological_site':models.ArchaeologicalSite} - currents = {'associated_file':File} pk = forms.IntegerField(required=False, widget=forms.HiddenInput) scientist = forms.IntegerField(label=_("Head scientist"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person', @@ -306,10 +313,6 @@ class OperationFormGeneral(forms.Form): [unicode(PersonType.objects.get(txt_idx='sra_agent').pk)])]), associated_model=Person, new=True), validators=[valid_id(Person)], required=False) - associated_file = forms.IntegerField(label=_(u"Archaelogical file"), - widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), - associated_model=File), - validators=[valid_id(File)], required=False) operation_type = forms.ChoiceField(label=_(u"Operation type"), choices=[]) start_date = forms.DateField(label=_(u"Start date"), required=False, @@ -394,8 +397,24 @@ class OperationFormGeneral(forms.Form): return self.cleaned_data class OperationFormModifGeneral(OperationFormGeneral): - operation_code = forms.IntegerField(label=_(u"Operation code"), - initial=models.Operation.get_available_operation_code) + currents = {'associated_file':File} + operation_code = forms.IntegerField(label=_(u"Operation code")) + associated_file = forms.IntegerField(label=_(u"Archaelogical file"), + widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), + associated_model=File), + validators=[valid_id(File)], required=False) + + def __init__(self, *args, **kwargs): + super(OperationFormModifGeneral, self).__init__(*args, **kwargs) + self.fields.keyOrder = list(self.fields.keyOrder) + self.fields.keyOrder.pop(self.fields.keyOrder.index('associated_file')) + self.fields.keyOrder.insert(self.fields.keyOrder.index('in_charge'), + 'associated_file') + +OperationFormModifGeneral.associated_models = \ + OperationFormGeneral.associated_models.copy() +OperationFormModifGeneral.associated_models['associated_file'] = File + class OperationFormPreventive(forms.Form): form_label = _(u"Preventive informations - excavation") diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 1f698b4e1..0ed9cb9c7 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -172,6 +172,7 @@ operation_search_wizard = SearchWizard.as_view([ url_name='operation_search',) operation_creation_wizard = OperationWizard.as_view([ + ('filechoice-operation_creation', OperationFormFileChoice), ('general-operation_creation', OperationFormGeneral), ('preventive-operation_creation', OperationFormPreventive), ('preventivediag-operation_creation', OperationFormPreventiveDiag), diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index cc9e87118..821d5eedc 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -91,11 +91,13 @@ class OperationWizard(Wizard): """ Obtention des villes disponibles """ - general_form_key = 'general-' + self.url_name if not FILES_AVAILABLE: return -1 + file_form_key = 'general-' + self.url_name + if self.url_name == 'operation_creation': + file_form_key = 'filechoice-' + self.url_name towns = [] - file_id = self.session_get_value(general_form_key, "associated_file") + file_id = self.session_get_value(file_form_key, "associated_file") if file_id: try: for town in File.objects.get(pk=int(file_id) @@ -129,13 +131,14 @@ class OperationWizard(Wizard): #step = self.determine_step(request, storage) step = self.steps.current form = self.get_form_list()[step] - general_form_key = 'general-' + self.url_name + file_form_key = 'general-' + self.url_name + if self.url_name == 'operation_creation': + file_form_key = 'filechoice-' + self.url_name # manage the dynamic choice of towns if step.startswith('towns-') and hasattr(form, 'management_form'): data['TOWNS'] = self.get_towns() elif step.startswith('parcels') and hasattr(form, 'management_form'): - file_id = self.session_get_value(general_form_key, - "associated_file") + file_id = self.session_get_value(file_form_key, "associated_file") if file_id: data['PARCELS'] = self.get_available_parcels(file_id) else: @@ -172,32 +175,49 @@ class OperationWizard(Wizard): def get_form_initial(self, step, data=None): initial = super(OperationWizard, self).get_form_initial(step) + if step == 'general-operation_creation': + initial.update(self._copy_from_associated_field()) return initial - ''' - self.form_initialized = False - if not step.startswith(self.parcel_step_key): + + def __copy_fields(self, item, keys): + initial = {} + for orig_keys, dest_key in keys: + value, c_value = None, item + for orig_key in orig_keys: + c_value = getattr(c_value, orig_key) + if not c_value: + break + else: + value = c_value + if not value: + continue + initial[dest_key] = value + return initial + + def _copy_from_associated_field(self): + initial = {} + file_form_key = 'filechoice-' + self.url_name + file_id = self.session_get_value(file_form_key, + "associated_file") + if not file_id: return initial - if initial: - default = initial[-1].copy() - if 'parcel_number' in default: - default.pop('parcel_number') - initial.append(default) - # necessary to get the appropriate form number - #self.form_initialized = True - elif data: - numbers, keys = set(), set() - for k in data: - items = k.split('-') - try: - numbers.add(int(items[-2])) - except (ValueError, IndexError): - continue - keys.add(items[-1]) - if numbers and max(numbers) - 1 > 0: - initial = [dict([(k, data[step+'-'+unicode(max(numbers)-1)+'-'+k]) - for k in keys if k != 'parcel_number'])] - #self.form_initialized = True - return initial''' + try: + file = File.objects.get(pk=file_id) + except File.DoesNotExist: + return + keys = ((('in_charge', 'pk'), 'in_charge'), + (('name',), 'common_name'), + (('total_surface',), 'surface'), + ) + initial.update(self.__copy_fields(file, keys)) + if file.is_preventive(): + return initial + keys = ((('scientist', 'pk'), 'scientist'), + (('requested_operation_type', 'pk'), 'operation_type'), + (('organization', 'pk'), 'operator'), + ) + initial.update(self.__copy_fields(file, keys)) + return initial def post(self, *args, **kwargs): request = self.request @@ -206,8 +226,11 @@ class OperationWizard(Wizard): # add all parcel from available in the archaelogical file if not post_data.get('add_all_parcels'): return super(OperationWizard, self).post(*args, **kwargs) - general_form_key = 'general-' + self.url_name - file_id = self.session_get_value(general_form_key, + + file_form_key = 'general-' + self.url_name + if self.url_name == 'operation_creation': + file_form_key = 'filechoice-' + self.url_name + file_id = self.session_get_value(file_form_key, "associated_file") if not file_id: return super(OperationWizard, self).post(*args, **kwargs) diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index fc3ada283..2105b73e3 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -211,8 +211,9 @@ class JQueryAutoComplete(forms.TextInput): selects.append("")
attrs_select['value'] = ", ".join(selects)
else:
- attrs_hidden['value'] = hiddens[0]
- attrs_select['value'] = selects[0]
+ if hiddens and selects:
+ attrs_hidden['value'] = hiddens[0]
+ attrs_select['value'] = selects[0]
if not self.attrs.has_key('id'):
attrs_hidden['id'] = 'id_%s' % name
attrs_select['id'] = 'id_select_%s' % name
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index d506c8552..878f083a3 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2013 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as |