summaryrefslogtreecommitdiff
path: root/archaeological_operations/wizards.py
diff options
context:
space:
mode:
Diffstat (limited to 'archaeological_operations/wizards.py')
-rw-r--r--archaeological_operations/wizards.py246
1 files changed, 246 insertions, 0 deletions
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
+