diff options
| -rw-r--r-- | archaeological_files/wizards.py | 8 | ||||
| -rw-r--r-- | archaeological_operations/forms.py | 12 | ||||
| -rw-r--r-- | archaeological_operations/wizards.py | 36 | ||||
| -rw-r--r-- | ishtar_common/widgets.py | 2 | ||||
| -rw-r--r-- | ishtar_common/wizards.py | 21 | 
5 files changed, 62 insertions, 17 deletions
| diff --git a/archaeological_files/wizards.py b/archaeological_files/wizards.py index e2f6722ef..c475de47d 100644 --- a/archaeological_files/wizards.py +++ b/archaeological_files/wizards.py @@ -25,16 +25,18 @@ from django.template import RequestContext  from django.utils.translation import ugettext_lazy as _  from ishtar_common.wizards import Wizard, ClosingWizard -from archaeological_operations.wizards import OperationAdministrativeActWizard,\ +from archaeological_operations.wizards import OperationWizard,\ +                                              OperationAdministrativeActWizard,\                                                AdministrativeActDeletionWizard  from ishtar_common.models import Town  from archaeological_operations.models import AdministrativeAct, Parcel, \                                               Operation  import models -class FileWizard(Wizard): +class FileWizard(OperationWizard):      model = models.File      object_parcel_type = 'associated_file' +    parcel_step_key = 'parcels-'      def get_form(self, step=None, data=None, files=None):          """ @@ -103,7 +105,7 @@ class FileWizard(Wizard):                      continue                  try:                      dct['town'] = models.Town.objects.get(pk=int(dct['town'])) -                except (ValueError, ObjectDoesNotExist): +                except (ValueError, ObjectDoesNotExist, KeyError):                      continue                  dct['associated_file'], dct['operation'] = None, None                  dct[self.object_parcel_type] = obj diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 741dfb2b7..72834d8f0 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -59,13 +59,13 @@ class ParcelForm(forms.Form):      associated_models = {'parcel':models.Parcel, 'town':models.Town}      town = forms.ChoiceField(label=_("Town"), choices=(), required=False,                               validators=[valid_id(models.Town)]) +    year = forms.IntegerField(label=_("Year"), required=False, +                           validators=[validators.MinValueValidator(1900), +                                       validators.MaxValueValidator(2100)])      section = forms.CharField(label=_(u"Section"), required=False,                               validators=[validators.MaxLengthValidator(4)])      parcel_number = forms.CharField(label=_(u"Parcel number"), required=False,                               validators=[validators.MaxLengthValidator(6)]) -    year = forms.IntegerField(label=_("Year"), required=False, -                           validators=[validators.MinValueValidator(1900), -                                       validators.MaxValueValidator(2100)])      def __init__(self, *args, **kwargs):          towns = None          if 'data' in kwargs and 'TOWNS' in kwargs['data']: @@ -85,9 +85,11 @@ class ParcelForm(forms.Form):          """Check required fields"""          if any(self.errors):              return -        if not self.cleaned_data or DELETION_FIELD_NAME in self.cleaned_data \ -           and self.cleaned_data[DELETION_FIELD_NAME]: +        if not self.cleaned_data or (DELETION_FIELD_NAME in self.cleaned_data \ +           and self.cleaned_data[DELETION_FIELD_NAME]):              return +        if not self.cleaned_data.get('parcel_number'): +            return {}          for key in ('town', 'parcel_number', 'section'):              if not key in self.cleaned_data or not self.cleaned_data[key]:                  raise forms.ValidationError(_(u"Town section and parcel number " diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 2b13e3353..4205144e6 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -27,16 +27,19 @@ from django.utils.translation import ugettext_lazy as _  from ishtar_common.wizards import Wizard, ClosingWizard, DeletionWizard, \                                    SourceWizard  import models +from forms import ParcelForm  class OperationWizard(Wizard):      model = models.Operation      object_parcel_type = 'operation' +    parcel_step_key = 'parcelsgeneral-'      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 +            #templates = ['ishtar/wizard/towns_wizard.html'] + templates +            pass          if current_step.startswith('parcels-') or \             current_step.startswith('parcelsgeneral-') :              templates = ['ishtar/wizard/parcels_wizard.html'] + templates @@ -124,7 +127,7 @@ class OperationWizard(Wizard):          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 +        # 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 has_no_af: @@ -133,6 +136,33 @@ class OperationWizard(Wizard):                  + datas          return datas +    def get_form_initial(self, step, data=None): +        initial = super(OperationWizard, self).get_form_initial(step) +        self.form_initialized = False +        if not step.startswith(self.parcel_step_key): +            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 max(numbers) - 1: +                initial = [dict([(k, data[step+'-'+unicode(max(numbers)-1)+'-'+k]) +                                           for k in keys if k != 'parcel_number'])] +            self.form_initialized = True +        return initial +  class OperationModificationWizard(OperationWizard):      modification = True @@ -148,7 +178,7 @@ class OperationDeletionWizard(DeletionWizard):  class OperationSourceWizard(SourceWizard):      model = models.OperationSource -    def get_form_initial(self, step): +    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          general_form_key = 'selec-' + self.url_name diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 1fd0b6ac0..06d6f20f0 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -206,7 +206,7 @@ class JQueryJqGrid(forms.RadioSelect):          self.source_full = source_full
      def render(self, name, value=None, attrs=None):
 -        t = loader.get_template('form_snippet.html')
 +        t = loader.get_template('blocks/form_snippet.html')
          rendered = t.render(Context({'form':self.form}))
          rendered += u"\n</table>\n"\
          u"<button id='search_%s' class='submit'>%s</button>" % (
 diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 8b6e1a50d..e421c25dd 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-2012  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2013  É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 @@ -461,15 +461,26 @@ class Wizard(NamedUrlWizardView):                                  k = u'-'.join(items)                                  data[k] = data.pop(key)[0]                  # get a form key -                base_key = form.form.base_fields.keys()[0] -                init = self.get_form_initial(step) +                frm = form.form +                if callable(frm): +                    frm = frm() +                base_key = frm.base_fields.keys()[-1] +                init = self.get_form_initial(step, data=data)                  total_field = len([key for key in data.keys()                                          if base_key in key.split('-')                                             and data[key]]) -                if init and not to_delete: +                if init and not to_delete and ( +                   not hasattr(self, 'form_initialized') or +                   not self.form_initialized):                      total_field = max((total_field, len(init)))                  data[step + u'-INITIAL_FORMS'] = unicode(total_field)                  data[step + u'-TOTAL_FORMS'] = unicode(total_field + 1) +                # update initialization +                if request.POST and init and hasattr(self, 'form_initialized') \ +                   and self.form_initialized: +                    for k in init[0]: +                        data[step + '-' + unicode(total_field) + '-' + k] = \ +                                                                      init[0][k]          data = data or None          form = super(Wizard, self).get_form(step, data, files)          return form @@ -561,7 +572,7 @@ class Wizard(NamedUrlWizardView):              pass          return current_obj -    def get_form_initial(self, step): +    def get_form_initial(self, step, data=None):          current_obj = self.get_current_object()          current_step = self.steps.current          request = self.request | 
