diff options
Diffstat (limited to 'archaeological_operations/wizards.py')
| -rw-r--r-- | archaeological_operations/wizards.py | 98 | 
1 files changed, 56 insertions, 42 deletions
| diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 9e6b9d03a..644bc7390 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -27,30 +27,28 @@ from django.utils.translation import ugettext_lazy as _  from ishtar_common.forms import reverse_lazy  from ishtar_common.wizards import Wizard, ClosingWizard, DeletionWizard, \ -                                  SourceWizard +    SourceWizard  import models -from forms import ParcelForm, GenerateDocForm +from forms import GenerateDocForm  FILES_AVAILABLE = 'archaeological_files' in settings.INSTALLED_APPS  if FILES_AVAILABLE:      from archaeological_files.models import File +  class OperationWizard(Wizard):      model = models.Operation      object_parcel_type = 'operation'      parcel_step_key = 'parcelsgeneral-'      # step contening the current(s) town(s)      town_step_keys = ['towns-', 'townsgeneral-'] -    town_input_id = 'town' # input id of the current(s) town(s) -    multi_towns = True # true if current town are multi +    town_input_id = 'town'  # input id of the current(s) town(s) +    multi_towns = True  # true if current town are multi      wizard_done_window = reverse_lazy('show-operation')      def get_template_names(self):          templates = super(OperationWizard, self).get_template_names()          current_step = self.steps.current -        if current_step.startswith('towns-'): -            #templates = ['ishtar/wizard/towns_wizard.html'] + templates -            pass          if current_step.startswith(self.parcel_step_key):              templates = ['ishtar/wizard/parcels_wizard.html'] + templates          return templates @@ -88,10 +86,10 @@ class OperationWizard(Wizard):          elif step.startswith('parcels-') and self.get_current_file():              # if a file is acciated to the operation add the button "Add all"              context['add_all'] = True -        if step.startswith('parcels') and\ +        if step.startswith('parcels') and \             hasattr(self, 'automatic_parcel_association'):              context['automatic_parcel_association'] = \ -                                            self.automatic_parcel_association +                self.automatic_parcel_association          # reminder of the current file          reminder = self.get_reminder()          if reminder: @@ -112,7 +110,7 @@ class OperationWizard(Wizard):              towns = [(town.pk, unicode(town)) for town in file.towns.all()]          except (ValueError, ObjectDoesNotExist):              pass -        return sorted(towns, key=lambda x:x[1]) +        return sorted(towns, key=lambda x: x[1])      def get_available_parcels(self, file):          self.automatic_parcel_association = False @@ -136,7 +134,7 @@ class OperationWizard(Wizard):                  self.automatic_parcel_association = True          except (ValueError, ObjectDoesNotExist):              pass -        return sorted(parcels, key=lambda x:x[1]) +        return sorted(parcels, key=lambda x: x[1])      def get_form(self, step=None, data=None, files=None):          """ @@ -147,14 +145,13 @@ class OperationWizard(Wizard):          else:              data = {}          if not step: -            #step = self.determine_step(request, storage)              step = self.steps.current          form = self.get_form_list()[step]          # manage the dynamic choice of towns          if step.startswith('towns') and hasattr(form, 'management_form'):              data['TOWNS'] = self.get_towns()          elif step.startswith(self.parcel_step_key) \ -             and hasattr(form, 'management_form'): +                and hasattr(form, 'management_form'):              file = self.get_current_file()              if file:                  data['PARCELS'] = self.get_available_parcels(file) @@ -162,12 +159,13 @@ class OperationWizard(Wizard):                  town_ids = []                  for town_step_key in self.town_step_keys:                      town_form_key = town_step_key + self.url_name -                    town_ids = self.session_get_value(town_form_key, -                                                  self.town_input_id, -                                                  multi=self.multi_towns) or [] -                    if town_ids: +                    town_ids = self.session_get_value( +                        town_form_key, self.town_input_id, +                        multi=self.multi_towns) or [] +                    if town_ids and type(town_ids) == unicode: +                        town_ids = town_ids.split(',')                          break -                if not self.multi_towns and town_ids: +                if type(town_ids) not in (list, tuple):                      town_ids = [town_ids]                  towns = []                  for town_id in town_ids: @@ -176,7 +174,7 @@ class OperationWizard(Wizard):                          towns.append((town.pk, unicode(town)))                      except (ValueError, ObjectDoesNotExist):                          pass -                data['TOWNS'] = sorted(towns, key=lambda x:x[1]) +                data['TOWNS'] = sorted(towns, key=lambda x: x[1])          data = data or None          form = super(OperationWizard, self).get_form(step, data, files)          return form @@ -188,7 +186,7 @@ class OperationWizard(Wizard):          datas = super(OperationWizard, self).get_formated_datas(forms)          # if the general town form is used the advertissement is relevant          has_no_af = [form.prefix for form in forms -                  if form.prefix == 'townsgeneral-operation'] and True +                     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."), []]]\ @@ -259,21 +257,26 @@ class OperationWizard(Wizard):          request.POST = post_data          return super(OperationWizard, self).post(*args, **kwargs) +  class OperationModificationWizard(OperationWizard):      modification = True +  class OperationClosingWizard(ClosingWizard):      model = models.Operation      fields = ['year', 'operation_code', 'operation_type', 'associated_file',                'in_charge', 'scientist', '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, step, data=None):          initial = super(OperationSourceWizard, self).get_form_initial(step)          # put default index and operation_id field in the main source form @@ -297,14 +300,16 @@ class OperationSourceWizard(SourceWizard):                  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"] +                        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',] +    fields = ['operation', 'title', 'source_type', 'authors'] +  class OperationAdministrativeActWizard(OperationWizard):      edit = False @@ -317,8 +322,10 @@ class OperationAdministrativeActWizard(OperationWizard):              # and '_deletion' so it is creation              operation_id = self.session_get_value(form_key, "pk")              try: -                return ((_(u"Operation"), -                      unicode(models.Operation.objects.get(pk=operation_id))),) +                return ( +                    (_(u"Operation"), +                     unicode(models.Operation.objects.get(pk=operation_id))), +                )              except models.Operation.DoesNotExist:                  return          else: @@ -337,8 +344,8 @@ class OperationAdministrativeActWizard(OperationWizard):      def get_context_data(self, form, **kwargs):          # manage document generation -        context = super(OperationAdministrativeActWizard, self -                                      ).get_context_data(form, **kwargs) +        context = super(OperationAdministrativeActWizard, +                        self).get_context_data(form, **kwargs)          step = self.steps.current          if step.startswith('final-'):              general_form_key = 'administrativeact-' + self.url_name @@ -349,7 +356,7 @@ class OperationAdministrativeActWizard(OperationWizard):                  pass              if act_type and act_type.associated_template.count():                  context['extra_form'] = GenerateDocForm( -                                   choices=act_type.associated_template.all()) +                    choices=act_type.associated_template.all())          return context      def get_associated_item(self, dct): @@ -386,7 +393,7 @@ class OperationAdministrativeActWizard(OperationWizard):                  r = r[k]              else:                  break -        if k == keys[-1]: # the whole list as been traversed +        if k == keys[-1]:  # the whole list as been traversed              wizard_done_window = unicode(self.wizard_done_window)              if wizard_done_window:                  dct['wizard_done_window'] = wizard_done_window @@ -399,29 +406,34 @@ class OperationAdministrativeActWizard(OperationWizard):                                   context_instance=RequestContext(self.request))          return res +  class OperationEditAdministrativeActWizard(OperationAdministrativeActWizard):      model = models.AdministrativeAct      edit = True +      def get_associated_item(self, dct):          return self.get_current_object().operation +  class AdministrativeActDeletionWizard(ClosingWizard):      wizard_templates = { -        'final-operation_administrativeactop_deletion':\ -                    'ishtar/wizard/wizard_adminact_deletion.html', -        'final-file_administrativeactfile_deletion':\ -                    'ishtar/wizard/wizard_adminact_deletion.html'} +        'final-operation_administrativeactop_deletion': +            'ishtar/wizard/wizard_adminact_deletion.html', +        'final-file_administrativeactfile_deletion': +            'ishtar/wizard/wizard_adminact_deletion.html'}      model = models.AdministrativeAct      fields = ['act_type', 'in_charge', 'operator', 'scientist', 'signatory', -              'operation', 'associated_file', 'signature_date', 'act_object',] +              'operation', 'associated_file', 'signature_date', 'act_object']      if settings.COUNTRY == 'fr':          fields += ['ref_sra']      def done(self, form_list, **kwargs):          obj = self.get_current_object()          obj.delete() -        return render_to_response('ishtar/wizard/wizard_delete_done.html', {}, -                                  context_instance=RequestContext(self.request)) +        return render_to_response( +            'ishtar/wizard/wizard_delete_done.html', {}, +            context_instance=RequestContext(self.request)) +  def is_preventive(form_name, model, type_key='operation_type', key=''):      def func(self): @@ -431,11 +443,11 @@ def is_preventive(form_name, model, type_key='operation_type', key=''):             '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]: +           request.session[storage.prefix]['step_data'][form_name]:              return False          try: -            typ = request.session[storage.prefix]['step_data']\ -                                 [form_name][form_name+'-'+type_key] +            typ = request.session[storage.prefix][ +                'step_data'][form_name][form_name+'-'+type_key]              if type(typ) in (list, tuple):                  typ = typ[0]              typ = int(typ) @@ -444,11 +456,13 @@ def is_preventive(form_name, model, type_key='operation_type', key=''):              return False      return func +  def is_not_preventive(form_name, model, type_key='operation_type', key=''):      def func(self):          return not is_preventive(form_name, model, type_key, key)(self)      return func +  def has_associated_file(form_name, file_key='associated_file', negate=False):      def func(self):          request = self.request @@ -457,11 +471,11 @@ def has_associated_file(form_name, file_key='associated_file', negate=False):             '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]: +           request.session[storage.prefix]['step_data'][form_name]:              return negate          try: -            file_id = request.session[storage.prefix]['step_data']\ -                                     [form_name][form_name+'-'+file_key] +            file_id = request.session[storage.prefix][ +                'step_data'][form_name][form_name+'-'+file_key]              if type(file_id) in (list, tuple):                  file_id = file_id[0]              file_id = int(file_id) | 
