diff options
Diffstat (limited to 'archaeological_operations')
-rw-r--r-- | archaeological_operations/forms.py | 374 | ||||
-rw-r--r-- | archaeological_operations/urls.py | 24 | ||||
-rw-r--r-- | archaeological_operations/views.py | 122 | ||||
-rw-r--r-- | archaeological_operations/wizards.py | 246 |
4 files changed, 399 insertions, 367 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index d4152d4fa..d87e72423 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -31,37 +31,23 @@ from django.core.exceptions import ObjectDoesNotExist from django.db.models import Max from django.utils.translation import ugettext_lazy as _ +from ishtar_common.models import valid_id, PersonType, Person, Town +from archaeological_files.models import File import models -import widgets -from ishtar_common.forms import Wizard, FinalForm, FormSet, SearchWizard, \ - ClosingWizard, ClosingDateFormSelection, DeletionWizard, formset_factory, \ +from ishtar_common import widgets +from ishtar_common.forms import FinalForm, FormSet, \ + ClosingDateFormSelection, formset_factory, \ get_now, reverse_lazy, get_form_selection from ishtar_common.forms_common import TownForm, TownFormSet, TownFormset, \ - AuthorFormset, SourceForm, SourceWizard, SourceSelect, \ + AuthorFormset, SourceForm, SourceSelect, \ SourceDeletionForm, get_town_field -def is_preventive(form_name, model, type_key='operation_type', key=''): - 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 + '-' + type_key not in \ - request.session[storage.prefix]['step_data'][form_name]: - return False - try: - typ = int(request.session[storage.prefix]['step_data']\ - [form_name][form_name+'-'+type_key]) - return model.is_preventive(typ, key) - except ValueError: - return False - return func - class ParcelForm(forms.Form): form_label = _("Parcels") base_model = 'parcel' associated_models = {'parcel':models.Parcel, 'town':models.Town} town = forms.ChoiceField(label=_("Town"), choices=(), required=False, - validators=[models.valid_id(models.Town)]) + validators=[valid_id(models.Town)]) section = forms.CharField(label=_(u"Section"), required=False, validators=[validators.MaxLengthValidator(4)]) parcel_number = forms.CharField(label=_(u"Parcel number"), required=False, @@ -107,109 +93,6 @@ ParcelFormSet = formset_factory(ParcelForm, can_delete=True, formset=ParcelFormSet) ParcelFormSet.form_label = _(u"Parcels") -class OperationWizard(Wizard): - model = models.Operation - object_parcel_type = 'operation' - - 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): - """ - Return extra context for templates - """ - 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): - """ - Manage specifics fields - """ - if data: - data = data.copy() - else: - data = {} - if not step: - step = self.determine_step(request, storage) - form = self.get_form_list(request, storage)[step] - general_form_key = 'general-' + self.url_name - # manage the dynamic choice of towns - 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") - if file_id: - parcels = [] - try: - for parcel in models.File.objects.get(pk=int(file_id) - ).parcels.all(): - parcels.append((parcel.pk, parcel.short_label())) - except (ValueError, ObjectDoesNotExist): - pass - data['PARCELS'] = sorted(parcels, key=lambda x:x[1]) - else: - town_form_key = step.startswith('parcelsgeneral') \ - and 'townsgeneral-' or 'towns-' - town_form_key += self.url_name - town_ids = self.session_get_value(request, storage, - town_form_key, 'town', multi=True) or [] - towns = [] - for town_id in town_ids: - try: - town = models.Town.objects.get(pk=int(town_id)) - towns.append((town.pk, unicode(town))) - except (ValueError, ObjectDoesNotExist): - pass - data['TOWNS'] = sorted(towns, key=lambda x:x[1]) - data = data or None - form = super(OperationWizard, self).get_form(request, storage, step, - data, files) - return form - - def get_formated_datas(self, forms): - """ - Show a specific warning if no archaelogical file is provided - """ - datas = super(OperationWizard, self).get_formated_datas(forms) - # if the general town form is used the advertissement is pertinent - has_no_af = [form.prefix for form in forms - if form.prefix == 'townsgeneral-operation'] and True - if has_no_af: - datas = [[_(u"Warning: No Archaelogical File is provided. " - u"If you have forget it return to the first step."), []]]\ - + datas - return datas - class OperationSelect(forms.Form): common_name = forms.CharField(label=_(u"Name"), max_length=30) towns = get_town_field() @@ -233,7 +116,7 @@ class OperationFormSelection(forms.Form): widget=widgets.JQueryJqGrid(reverse_lazy('get-operation'), OperationSelect(), models.Operation, source_full=reverse_lazy('get-operation-full')), - validators=[models.valid_id(models.Operation)]) + validators=[valid_id(models.Operation)]) def clean(self): cleaned_data = self.cleaned_data @@ -267,22 +150,22 @@ class OperationCodeInput(forms.TextInput): class OperationFormGeneral(forms.Form): form_label = _(u"General") - associated_models = {'in_charge':models.Person, - 'associated_file':models.File, + associated_models = {'in_charge':Person, + 'associated_file':File, 'operation_type':models.OperationType} - currents = {'associated_file':models.File} + currents = {'associated_file':File} pk = forms.IntegerField(required=False, widget=forms.HiddenInput) in_charge = forms.IntegerField(label=_("Person in charge of the operation"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person', args=["_".join( - [unicode(models.PersonType.objects.get(txt_idx='head_scientist').pk), - unicode(models.PersonType.objects.get(txt_idx='sra_agent').pk)])]), - associated_model=models.Person, new=True), - validators=[models.valid_id(models.Person)], required=False) + [unicode(PersonType.objects.get(txt_idx='head_scientist').pk), + 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=models.File), - validators=[models.valid_id(models.File)], required=False) + 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, @@ -369,9 +252,9 @@ class OperationFormPreventiveDiag(forms.Form): class SelectedTownForm(forms.Form): form_label = _("Towns") - associated_models = {'town':models.Town} + associated_models = {'town':Town} town = forms.ChoiceField(label=_("Town"), choices=(), - validators=[models.valid_id(models.Town)]) + validators=[valid_id(Town)]) def __init__(self, *args, **kwargs): towns = None if 'data' in kwargs and 'TOWNS' in kwargs['data']: @@ -395,7 +278,7 @@ class SelectedParcelForm(forms.Form): form_label = _("Parcels") associated_models = {'parcel':models.Parcel} parcel = forms.ChoiceField(label=_("Parcel"), choices=(), - validators=[models.valid_id(models.Parcel)]) + validators=[valid_id(models.Parcel)]) def __init__(self, *args, **kwargs): parcels = None if 'data' in kwargs and 'PARCELS' in kwargs['data']: @@ -453,10 +336,6 @@ PeriodFormset = formset_factory(PeriodForm, can_delete=True, formset=PeriodFormSet) PeriodFormset.form_label = _("Periods") -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 \ @@ -472,126 +351,18 @@ def has_associated_file(form_name, file_key='associated_file', negate=False): except ValueError: return negate return func - -operation_creation_wizard = OperationWizard([ - ('general-operation_creation', OperationFormGeneral), - ('preventive-operation_creation', OperationFormPreventive), - ('preventivediag-operation_creation', OperationFormPreventiveDiag), - ('townsgeneral-operation_creation', TownFormset), - ('towns-operation_creation', SelectedTownFormset), - ('parcelsgeneral-operation_creation', SelectedParcelGeneralFormSet), - ('parcels-operation_creation', SelectedParcelFormSet), - ('remains-operation_creation', RemainFormset), - ('periods-operation_creation', PeriodFormset), - ('final-operation_creation', FinalForm)], - condition_list={ -'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'), -'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',) - -class OperationModificationWizard(OperationWizard): - modification = True - -operation_modification_wizard = OperationModificationWizard([ - ('selec-operation_modification', OperationFormSelection), - ('general-operation_modification', OperationFormGeneral), - ('preventive-operation_modification', OperationFormPreventive), - ('preventivediag-operation_modification', OperationFormPreventiveDiag), - ('towns-operation_modification', SelectedTownFormset), - ('townsgeneral-operation_modification', TownFormset), - ('parcels-operation_modification', SelectedParcelFormSet), - ('parcelsgeneral-operation_modification', SelectedParcelGeneralFormSet), - ('remains-operation_modification', RemainFormset), - ('periods-operation_modification', PeriodFormset), - ('final-operation_modification', FinalForm)], - condition_list={ -'preventive-operation_modification':is_preventive( - 'general-operation_modification', models.OperationType, - 'operation_type', 'prev_excavation'), -'preventivediag-operation_modification':is_preventive( - 'general-operation_modification', models.OperationType, - '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_modification', negate=True), -'parcels-operation_modification':has_associated_file( - 'general-operation_modification'), - }, - url_name='operation_modification',) - -class OperationClosingWizard(ClosingWizard): - model = models.Operation - fields = ['year', 'operation_code', 'operation_type', 'associated_file', -'in_charge', 'start_date', 'excavation_end_date', 'comment', 'towns', 'remains'] - class FinalOperationClosingForm(FinalForm): confirm_msg = " " confirm_end_msg = _(u"Would you like to close this operation?") -operation_closing_wizard = OperationClosingWizard([ - ('selec-operation_closing', OperationFormSelection), - ('date-operation_closing', ClosingDateFormSelection), - ('final-operation_closing', FinalOperationClosingForm)], - url_name='operation_closing',) - -class OperationDeletionWizard(DeletionWizard): - model = models.Operation - fields = OperationClosingWizard.fields - class OperationDeletionForm(FinalForm): confirm_msg = " " confirm_end_msg = _(u"Would you like to delete this operation?") -operation_deletion_wizard = OperationDeletionWizard([ - ('selec-operation_deletion', OperationFormSelection), - ('final-operation_deletion', OperationDeletionForm)], - url_name='operation_deletion',) - #################################### # Source management for operations # #################################### -class OperationSourceWizard(SourceWizard): - model = models.OperationSource - def get_form_initial(self, request, storage, step): - initial = super(OperationSourceWizard, self).get_form_initial(request, - storage, step) - # put default index and operation_id field in the main source form - general_form_key = 'selec-' + self.url_name - if step.startswith('source-') \ - and self.session_has_key(request, storage, general_form_key): - gen_storage = request.session[storage.prefix]['step_data']\ - [general_form_key] - if general_form_key+"-operation" in gen_storage: - operation_id = int(gen_storage[general_form_key+"-operation"]) - elif general_form_key+"-pk" in gen_storage: - pk = int(gen_storage[general_form_key+"-pk"]) - try: - source = models.OperationSource.objects.get(pk=pk) - operation_id = source.operation.pk - except ObjectDoesNotExist: - pass - if operation_id: - initial['hidden_operation_id'] = operation_id - if 'index' not in initial: - max_val = models.OperationSource.objects.filter( - operation__pk=operation_id).aggregate( - Max('index'))["index__max"] - initial['index'] = max_val and (max_val + 1) or 1 - return initial - class OperationSourceForm(SourceForm): pk = forms.IntegerField(required=False, widget=forms.HiddenInput) index = forms.IntegerField(label=_(u"Index")) @@ -627,13 +398,6 @@ SourceOperationFormSelection = get_form_selection( models.Operation, OperationSelect, 'get-operation', _(u"You should select an operation.")) -operation_source_creation_wizard = OperationSourceWizard([ - ('selec-operation_source_creation', SourceOperationFormSelection), - ('source-operation_source_creation',OperationSourceForm), - ('authors-operation_source_creation', AuthorFormset), - ('final-operation_source_creation', FinalForm)], - url_name='operation_source_creation',) - class OperationSourceSelect(SourceSelect): operation__towns = get_town_field(label=_(u"Operation's town")) operation__operation_type = forms.ChoiceField(label=_(u"Operation type"), @@ -653,66 +417,10 @@ OperationSourceFormSelection = get_form_selection( models.OperationSource, OperationSourceSelect, 'get-operationsource', _(u"You should select a document.")) -operation_source_modification_wizard = OperationSourceWizard([ - ('selec-operation_source_modification', OperationSourceFormSelection), - ('source-operation_source_modification', OperationSourceForm), - ('authors-operation_source_modification', AuthorFormset), - ('final-operation_source_modification', FinalForm)], - url_name='operation_source_modification',) - -class OperationSourceDeletionWizard(DeletionWizard): - model = models.OperationSource - fields = ['operation', 'title', 'source_type', 'authors',] - -operation_source_deletion_wizard = OperationSourceDeletionWizard([ - ('selec-operation_source_deletion', OperationSourceFormSelection), - ('final-operation_source_deletion', SourceDeletionForm)], - url_name='operation_source_deletion',) - ################################################ # Administrative act management for operations # ################################################ -class OperationAdministrativeActWizard(OperationWizard): - edit = False - - def get_extra_model(self, dct, request, storage, form_list): - dct['history_modifier'] = request.user - return dct - - def get_associated_item(self, request, storage, dct): - return self.get_current_object(request, storage) - - def save_model(self, dct, m2m, whole_associated_models, request, storage, - form_list, return_object): - associated_item = self.get_associated_item(request, storage, dct) - if not associated_item: - return self.render(request, storage, form_list[-1]) - if isinstance(associated_item, models.File): - dct['associated_file'] = associated_item - elif isinstance(associated_item, models.Operation): - dct['operation'] = associated_item - dct['history_modifier'] = request.user - if 'pk' in dct: - dct.pop('pk') - if self.edit: - admact = self.get_current_object(request, storage) - for k in dct: - if hasattr(admact, k): - setattr(admact, k, dct[k]) - else: - admact = models.AdministrativeAct(**dct) - admact.save() - res = render_to_response('wizard_done.html', {}, - context_instance=RequestContext(request)) - return res - -class OperationEditAdministrativeActWizard(OperationAdministrativeActWizard): - model = models.AdministrativeAct - edit = True - def get_associated_item(self, request, storage, dct): - return self.get_current_object(request, storage).operation - class AdministrativeActOpeSelect(forms.Form): operation__towns = get_town_field() act_type = forms.ChoiceField(label=_("Act type"), choices=[]) @@ -732,7 +440,7 @@ class AdministrativeActOpeFormSelection(forms.Form): widget=widgets.JQueryJqGrid(reverse_lazy('get-administrativeactop'), AdministrativeActOpeSelect(), models.AdministrativeAct, table_cols='TABLE_COLS_OPE'), - validators=[models.valid_id(models.AdministrativeAct)]) + validators=[valid_id(models.AdministrativeAct)]) def clean(self): cleaned_data = self.cleaned_data @@ -744,12 +452,12 @@ class AdministrativeActOpeFormSelection(forms.Form): class AdministrativeActOpeForm(forms.Form): form_label = _("General") associated_models = {'act_type':models.ActType, - 'signatory':models.Person} + 'signatory':Person} act_type = forms.ChoiceField(label=_("Act type"), choices=[]) signatory = forms.IntegerField(label=_("Signatory"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person'), - associated_model=models.Person, new=True), - validators=[models.valid_id(models.Person)]) + associated_model=Person, new=True), + validators=[valid_id(Person)]) act_object = forms.CharField(label=_(u"Object"), max_length=200, widget=forms.Textarea) signature_date = forms.DateField(label=_(u"Signature date"), @@ -764,41 +472,7 @@ class AdministrativeActOpeForm(forms.Form): self.fields['act_type'].help_text = models.ActType.get_help( dct={'intented_to':'O'}) -class AdministrativeActDeletionWizard(ClosingWizard): - model = models.AdministrativeAct - fields = ['act_type', 'in_charge', 'operator', 'scientific', 'signatory', - 'operation', 'associated_file', 'signature_date', 'act_object',] - if settings.COUNTRY == 'fr': - fields += ['ref_sra'] - - def done(self, request, storage, form_list, **kwargs): - obj = self.get_current_object(request, storage) - obj.delete() - return render_to_response('wizard_done.html', {}, - context_instance=RequestContext(request)) - class FinalAdministrativeActDeleteForm(FinalForm): confirm_msg = " " confirm_end_msg = _(u"Would you like to delete this administrative act?") -operation_administrativeactop_wizard = OperationAdministrativeActWizard([ - ('selec-operation_administrativeactop', OperationFormSelection), - ('administrativeact-operation_administrativeactop', AdministrativeActOpeForm), - ('final-operation_administrativeactop', FinalForm)], - url_name='operation_administrativeactop',) - -operation_administrativeactop_modification_wizard = \ - OperationEditAdministrativeActWizard([ - ('selec-operation_administrativeactop_modification', - AdministrativeActOpeFormSelection), - ('administrativeact-operation_administrativeactop_modification', - AdministrativeActOpeForm), - ('final-operation_administrativeactop_modification', FinalForm)], - url_name='operation_administrativeactop_modification',) - -operation_administrativeactop_deletion_wizard = AdministrativeActDeletionWizard([ - ('selec-operation_administrativeactop_deletion', - AdministrativeActOpeFormSelection), - ('final-operation_administrativeactop_deletion', - FinalAdministrativeActDeleteForm)], - url_name='operation_administrativeactop_deletion',) diff --git a/archaeological_operations/urls.py b/archaeological_operations/urls.py index a761b4ccc..fb3278e31 100644 --- a/archaeological_operations/urls.py +++ b/archaeological_operations/urls.py @@ -20,38 +20,38 @@ from django.conf.urls.defaults import * """ -import forms +import views # forms urlpatterns = patterns('', url(r'operation_search/(?P<step>.+)$', - forms.operation_search_wizard, name='operation_search'), + views.operation_search_wizard, name='operation_search'), url(r'operation_creation/(?P<step>.+)$', - forms.operation_creation_wizard, name='operation_creation'), + views.operation_creation_wizard, name='operation_creation'), url(r'operation_modification/(?P<step>.+)$', - forms.operation_modification_wizard, + views.operation_modification_wizard, name='operation_modification'), url(r'operation_closing/(?P<step>.+)$', - forms.operation_closing_wizard, name='operation_closing'), + views.operation_closing_wizard, name='operation_closing'), url(r'operation_deletion/(?P<step>.+)$', - forms.operation_deletion_wizard, name='operation_deletion'), + views.operation_deletion_wizard, name='operation_deletion'), url(r'operation_administrativeactop/(?P<step>.+)$', - forms.operation_administrativeactop_wizard, + views.operation_administrativeactop_wizard, name='operation_administrativeactop'), url(r'operation_administrativeactop_modification/(?P<step>.+)$', - forms.operation_administrativeactop_modification_wizard, + views.operation_administrativeactop_modification_wizard, name='operation_administrativeactop_modification'), url(r'operation_administrativeactop_deletion/(?P<step>.+)$', - forms.operation_administrativeactop_deletion_wizard, + views.operation_administrativeactop_deletion_wizard, name='operation_administrativeactop_deletion'), url(r'operation_source_creation/(?P<step>.+)$', - forms.operation_source_creation_wizard, + views.operation_source_creation_wizard, name='operation_source_creation'), url(r'operation_source_modification/(?P<step>.+)$', - forms.operation_source_modification_wizard, + views.operation_source_modification_wizard, name='operation_source_modification'), url(r'operation_source_deletion/(?P<step>.+)$', - forms.operation_source_deletion_wizard, + views.operation_source_deletion_wizard, name='operation_source_deletion'), ) diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 27ebd60e9..7866e2c66 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -24,6 +24,9 @@ from django.http import HttpResponse from django.shortcuts import render_to_response from ishtar_common.views import get_item, show_item, revert_item +from ishtar_common.wizards import SearchWizard +from wizards import * +from forms import * import models def autocomplete_operation(request, non_closed=True): @@ -76,11 +79,6 @@ get_operationsource = get_item(models.OperationSource, 'operation__operation_type':'operation__operation_type__pk', 'operation__year':'operation__year'}) -get_administrativeactfile = get_item(models.AdministrativeAct, - 'get_administrativeactfile', 'administrativeactfile', - extra_request_keys={'associated_file__towns':'associated_file__towns__pk', - 'operation__towns':'operation__towns__pk', - 'act_type__intented_to':'act_type__intented_to'}) get_administrativeactop = get_item(models.AdministrativeAct, 'get_administrativeactop', 'administrativeactop', extra_request_keys={'associated_file__towns':'associated_file__towns__pk', @@ -95,3 +93,117 @@ def dashboard_operation(request, dct, obj_id=None, *args, **kwargs): dct = {'dashboard': models.OperationDashboard()} return render_to_response('dashboard_operation.html', dct, context_instance=RequestContext(request)) + +operation_search_wizard = SearchWizard.as_view([ + ('general-operation_search', OperationFormSelection)], + url_name='operation_search',) + +operation_creation_wizard = OperationWizard.as_view([ + ('general-operation_creation', OperationFormGeneral), + ('preventive-operation_creation', OperationFormPreventive), + ('preventivediag-operation_creation', OperationFormPreventiveDiag), + ('townsgeneral-operation_creation', TownFormset), + ('towns-operation_creation', SelectedTownFormset), + ('parcelsgeneral-operation_creation', SelectedParcelGeneralFormSet), + ('parcels-operation_creation', SelectedParcelFormSet), + ('remains-operation_creation', RemainFormset), + ('periods-operation_creation', PeriodFormset), + ('final-operation_creation', FinalForm)], + condition_dict={ +'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'), +'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',) + +operation_modification_wizard = OperationModificationWizard.as_view([ + ('selec-operation_modification', OperationFormSelection), + ('general-operation_modification', OperationFormGeneral), + ('preventive-operation_modification', OperationFormPreventive), + ('preventivediag-operation_modification', OperationFormPreventiveDiag), + ('towns-operation_modification', SelectedTownFormset), + ('townsgeneral-operation_modification', TownFormset), + ('parcels-operation_modification', SelectedParcelFormSet), + ('parcelsgeneral-operation_modification', SelectedParcelGeneralFormSet), + ('remains-operation_modification', RemainFormset), + ('periods-operation_modification', PeriodFormset), + ('final-operation_modification', FinalForm)], + condition_dict={ +'preventive-operation_modification':is_preventive( + 'general-operation_modification', models.OperationType, + 'operation_type', 'prev_excavation'), +'preventivediag-operation_modification':is_preventive( + 'general-operation_modification', models.OperationType, + '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_modification', negate=True), +'parcels-operation_modification':has_associated_file( + 'general-operation_modification'), + }, + url_name='operation_modification',) + +operation_closing_wizard = OperationClosingWizard.as_view([ + ('selec-operation_closing', OperationFormSelection), + ('date-operation_closing', ClosingDateFormSelection), + ('final-operation_closing', FinalOperationClosingForm)], + url_name='operation_closing',) + +operation_deletion_wizard = OperationDeletionWizard.as_view([ + ('selec-operation_deletion', OperationFormSelection), + ('final-operation_deletion', OperationDeletionForm)], + url_name='operation_deletion',) + +operation_source_creation_wizard = OperationSourceWizard.as_view([ + ('selec-operation_source_creation', SourceOperationFormSelection), + ('source-operation_source_creation',OperationSourceForm), + ('authors-operation_source_creation', AuthorFormset), + ('final-operation_source_creation', FinalForm)], + url_name='operation_source_creation',) + +operation_source_modification_wizard = OperationSourceWizard.as_view([ + ('selec-operation_source_modification', OperationSourceFormSelection), + ('source-operation_source_modification', OperationSourceForm), + ('authors-operation_source_modification', AuthorFormset), + ('final-operation_source_modification', FinalForm)], + url_name='operation_source_modification',) + +operation_source_deletion_wizard = OperationSourceDeletionWizard.as_view([ + ('selec-operation_source_deletion', OperationSourceFormSelection), + ('final-operation_source_deletion', SourceDeletionForm)], + url_name='operation_source_deletion',) + +operation_administrativeactop_wizard = \ + OperationAdministrativeActWizard.as_view([ + ('selec-operation_administrativeactop', OperationFormSelection), + ('administrativeact-operation_administrativeactop', AdministrativeActOpeForm), + ('final-operation_administrativeactop', FinalForm)], + url_name='operation_administrativeactop',) + +operation_administrativeactop_modification_wizard = \ + OperationEditAdministrativeActWizard.as_view([ + ('selec-operation_administrativeactop_modification', + AdministrativeActOpeFormSelection), + ('administrativeact-operation_administrativeactop_modification', + AdministrativeActOpeForm), + ('final-operation_administrativeactop_modification', FinalForm)], + url_name='operation_administrativeactop_modification',) + +operation_administrativeactop_deletion_wizard = \ + AdministrativeActDeletionWizard.as_view([ + ('selec-operation_administrativeactop_deletion', + AdministrativeActOpeFormSelection), + ('final-operation_administrativeactop_deletion', + FinalAdministrativeActDeleteForm)], + url_name='operation_administrativeactop_deletion',) + diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py new file mode 100644 index 000000000..df785fe6e --- /dev/null +++ b/archaeological_operations/wizards.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (C) 2012 É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 +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# See the file COPYING for details. + +from django.conf import settings +from django.core.exceptions import ObjectDoesNotExist +from django.shortcuts import render_to_response +from django.template import RequestContext +from django.utils.translation import ugettext_lazy as _ + +from ishtar_common.wizards import Wizard, ClosingWizard, DeletionWizard, \ + SourceWizard +import models + +class OperationWizard(Wizard): + model = models.Operation + object_parcel_type = 'operation' + + 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): + """ + Return extra context for templates + """ + 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): + """ + Manage specifics fields + """ + if data: + data = data.copy() + else: + data = {} + if not step: + step = self.determine_step(request, storage) + form = self.get_form_list(request, storage)[step] + general_form_key = 'general-' + self.url_name + # manage the dynamic choice of towns + 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") + if file_id: + parcels = [] + try: + for parcel in models.File.objects.get(pk=int(file_id) + ).parcels.all(): + parcels.append((parcel.pk, parcel.short_label())) + except (ValueError, ObjectDoesNotExist): + pass + data['PARCELS'] = sorted(parcels, key=lambda x:x[1]) + else: + town_form_key = step.startswith('parcelsgeneral') \ + and 'townsgeneral-' or 'towns-' + town_form_key += self.url_name + town_ids = self.session_get_value(request, storage, + town_form_key, 'town', multi=True) or [] + towns = [] + for town_id in town_ids: + try: + town = models.Town.objects.get(pk=int(town_id)) + towns.append((town.pk, unicode(town))) + except (ValueError, ObjectDoesNotExist): + pass + data['TOWNS'] = sorted(towns, key=lambda x:x[1]) + data = data or None + form = super(OperationWizard, self).get_form(request, storage, step, + data, files) + return form + + def get_formated_datas(self, forms): + """ + Show a specific warning if no archaelogical file is provided + """ + datas = super(OperationWizard, self).get_formated_datas(forms) + # if the general town form is used the advertissement is pertinent + has_no_af = [form.prefix for form in forms + if form.prefix == 'townsgeneral-operation'] and True + if has_no_af: + datas = [[_(u"Warning: No Archaelogical File is provided. " + u"If you have forget it return to the first step."), []]]\ + + datas + return datas + +class OperationModificationWizard(OperationWizard): + modification = True + +class OperationClosingWizard(ClosingWizard): + model = models.Operation + fields = ['year', 'operation_code', 'operation_type', 'associated_file', +'in_charge', 'start_date', 'excavation_end_date', 'comment', 'towns', 'remains'] + +class OperationDeletionWizard(DeletionWizard): + model = models.Operation + fields = OperationClosingWizard.fields + +class OperationSourceWizard(SourceWizard): + model = models.OperationSource + def get_form_initial(self, request, storage, step): + initial = super(OperationSourceWizard, self).get_form_initial(request, + storage, step) + # put default index and operation_id field in the main source form + general_form_key = 'selec-' + self.url_name + if step.startswith('source-') \ + and self.session_has_key(request, storage, general_form_key): + gen_storage = request.session[storage.prefix]['step_data']\ + [general_form_key] + if general_form_key+"-operation" in gen_storage: + operation_id = int(gen_storage[general_form_key+"-operation"]) + elif general_form_key+"-pk" in gen_storage: + pk = int(gen_storage[general_form_key+"-pk"]) + try: + source = models.OperationSource.objects.get(pk=pk) + operation_id = source.operation.pk + except ObjectDoesNotExist: + pass + if operation_id: + initial['hidden_operation_id'] = operation_id + if 'index' not in initial: + max_val = models.OperationSource.objects.filter( + operation__pk=operation_id).aggregate( + Max('index'))["index__max"] + initial['index'] = max_val and (max_val + 1) or 1 + return initial + +class OperationSourceDeletionWizard(DeletionWizard): + model = models.OperationSource + fields = ['operation', 'title', 'source_type', 'authors',] + +class OperationAdministrativeActWizard(OperationWizard): + edit = False + + def get_extra_model(self, dct, request, storage, form_list): + dct['history_modifier'] = request.user + return dct + + def get_associated_item(self, request, storage, dct): + return self.get_current_object(request, storage) + + def save_model(self, dct, m2m, whole_associated_models, request, storage, + form_list, return_object): + associated_item = self.get_associated_item(request, storage, dct) + if not associated_item: + return self.render(request, storage, form_list[-1]) + if isinstance(associated_item, models.File): + dct['associated_file'] = associated_item + elif isinstance(associated_item, models.Operation): + dct['operation'] = associated_item + dct['history_modifier'] = request.user + if 'pk' in dct: + dct.pop('pk') + if self.edit: + admact = self.get_current_object(request, storage) + for k in dct: + if hasattr(admact, k): + setattr(admact, k, dct[k]) + else: + admact = models.AdministrativeAct(**dct) + admact.save() + res = render_to_response('wizard_done.html', {}, + context_instance=RequestContext(request)) + return res + +class OperationEditAdministrativeActWizard(OperationAdministrativeActWizard): + model = models.AdministrativeAct + edit = True + def get_associated_item(self, request, storage, dct): + return self.get_current_object(request, storage).operation + +class AdministrativeActDeletionWizard(ClosingWizard): + model = models.AdministrativeAct + fields = ['act_type', 'in_charge', 'operator', 'scientific', 'signatory', + 'operation', 'associated_file', 'signature_date', 'act_object',] + if settings.COUNTRY == 'fr': + fields += ['ref_sra'] + + def done(self, request, storage, form_list, **kwargs): + obj = self.get_current_object(request, storage) + obj.delete() + return render_to_response('wizard_done.html', {}, + context_instance=RequestContext(request)) + +def is_preventive(form_name, model, type_key='operation_type', key=''): + 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 + '-' + type_key not in \ + request.session[storage.prefix]['step_data'][form_name]: + return False + try: + typ = int(request.session[storage.prefix]['step_data']\ + [form_name][form_name+'-'+type_key]) + return model.is_preventive(typ, key) + except ValueError: + return False + return func + |