diff options
Diffstat (limited to 'ishtar/furnitures/forms.py')
| -rw-r--r-- | ishtar/furnitures/forms.py | 98 |
1 files changed, 80 insertions, 18 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index c5284e46d..924ea8b94 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -57,7 +57,8 @@ class Wizard(NamedUrlSessionFormWizard): model = None def get_template(self, request, storage): templates = ['default_wizard.html'] - current_step = storage.get_current_step() or '0' + current_step = storage.get_current_step() or self.get_first_step( + request, storage) if current_step == self.get_last_step(request, storage): templates = ['confirm_wizard.html'] + templates return templates @@ -69,7 +70,8 @@ class Wizard(NamedUrlSessionFormWizard): context = super(Wizard, self).get_template_context(request, storage, form) step = self.get_first_step(request, storage) - current_step = storage.get_current_step() or '0' + current_step = storage.get_current_step() or self.get_first_step( + request, storage) context.update({'current_step':self.form_list[current_step]}) if step == current_step: return context @@ -99,24 +101,39 @@ class Wizard(NamedUrlSessionFormWizard): """ datas = [] for form in forms: + form_datas = [] base_form = hasattr(form, 'forms') and form.forms[0] or form associated_models = hasattr(base_form, 'associated_models') and \ base_form.associated_models or {} - if not hasattr(form, 'cleaned_data'): + if not hasattr(form, 'cleaned_data') and hasattr(form, 'forms'): + cleaned_datas = [frm.cleaned_data for frm in form.forms + if frm.is_valid()] + if not cleaned_datas: + continue + elif not hasattr(form, 'cleaned_data'): continue - cleaned_datas = type(form.cleaned_data) == list and \ + else: + cleaned_datas = type(form.cleaned_data) == list and \ form.cleaned_data \ or [form.cleaned_data] for cleaned_data in cleaned_datas: + if cleaned_data and form_datas: + form_datas.append(("", "", "spacer")) for key in cleaned_data: - lbl = key + lbl = None if hasattr(base_form, 'fields') and key in base_form.fields: lbl = base_form.fields[key].label + if not lbl: + continue value = cleaned_data[key] if key in associated_models: value = unicode(associated_models[key].objects.get( pk=value)) - datas.append((lbl, value)) + if not value: + continue + form_datas.append((lbl, value, '')) + if form_datas: + datas.append((form.form_label, form_datas)) return datas def get_extra_model(self, dct, request, storage, form_list): @@ -217,6 +234,22 @@ class Wizard(NamedUrlSessionFormWizard): return super(Wizard, self).render_next_step(request, storage, form, **kwargs) + def process_post_request(self, request, storage, *args, **kwargs): + """ + Convert numerical step number to step name + """ + if request.POST.has_key('form_prev_step'): + try: + step_number = int(request.POST['form_prev_step']) + post_data = request.POST.copy() + post_data['form_prev_step'] = self.get_form_list(request, + storage).keys()[step_number] + request.POST = post_data + except ValueError: + pass + return super(Wizard, self).process_post_request(request, storage, *args, + **kwargs) + class FileWizard(Wizard): model = models.File def get_form(self, request, storage, step=None, data=None, files=None): @@ -231,12 +264,13 @@ class FileWizard(Wizard): if not step: step = self.determine_step(request, storage) form = self.get_form_list(request, storage)[step] - if step == '3' and hasattr(form, 'management_form') \ + town_form_key = 'towns-' + self.url_name + if step.startswith('parcels-') and hasattr(form, 'management_form') \ and storage.prefix in request.session \ and 'step_data' in request.session[storage.prefix] \ - and '2' in request.session[storage.prefix]['step_data']: + and town_form_key in request.session[storage.prefix]['step_data']: towns = [] - qdict = request.session[storage.prefix]['step_data']['2'] + qdict = request.session[storage.prefix]['step_data'][town_form_key] for k in qdict.keys(): if k.endswith("town") and qdict[k]: try: @@ -268,7 +302,8 @@ class FileWizard(Wizard): return r obj, res = r for form in form_list: - if not hasattr(form, 'prefix') or form.prefix != '3' \ + if not hasattr(form, 'prefix') \ + or not form.prefix.startswith('parcels-') \ or not hasattr(form, 'forms'): continue for frm in form.forms: @@ -293,7 +328,14 @@ def get_now(): value = datetime.datetime.now().strftime(format) return value -class FileForm1(forms.Form): +class FileFormSelection(forms.Form): + form_label = _("Archaelogical file") + archaelogical_file = forms.IntegerField(label=_("Archaelogical file"), + widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'), + associated_model=models.File), + validators=[models.valid_id(models.File)]) + +class FileFormGeneral(forms.Form): form_label = _("General") associated_models = {'in_charge':models.Person, 'file_type':models.FileType} @@ -312,7 +354,7 @@ max_length=60, validators=[models.is_unique(models.File, 'internal_reference')]) comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea, required=False) -class FileForm2(forms.Form): +class FileFormAddress(forms.Form): form_label = _("Address") total_surface = forms.IntegerField(label=_("Total surface")) address = forms.CharField(label=_(u"Address"), widget=forms.Textarea) @@ -346,7 +388,7 @@ TownFormSet.form_label = _("Towns") class ParcelForm(forms.Form): form_label = _("Parcels") - associated_models = {'parcel':models.Parcel} + associated_models = {'parcel':models.Parcel, 'town':models.Town} town = forms.ChoiceField(label=_("Town"), choices=(), validators=[models.valid_id(models.Town)]) section = forms.CharField(label=_(u"Section"), @@ -393,7 +435,7 @@ ParcelFormSet = formset_factory(ParcelForm, can_delete=True, formset=ParcelFormSet) ParcelFormSet.form_label = _("Parcels") -class FileForm4(forms.Form): +class FileFormPreventive(forms.Form): form_label = _("Preventive informations") associated_models = {'general_contractor':models.Organization, 'saisine_type':models.SaisineType} @@ -426,7 +468,27 @@ def is_preventive(form_name, file_type_key='file_type'): return False return func -file_creation_wizard = FileWizard([FileForm1, FileForm2, TownFormSet, - ParcelFormSet, FileForm4, FinalForm], - url_name='file_creation', condition_list={'4':is_preventive('0')}) - +file_creation_wizard = FileWizard([ + ('general-file_creation', FileFormGeneral), + ('address-file_creation', FileFormAddress), + ('towns-file_creation', TownFormSet), + ('parcels-file_creation', ParcelFormSet), + ('preventive-file_creation', FileFormPreventive), + ('final-file_creation', FinalForm)], + condition_list={ + 'preventive-file_creation':is_preventive('general-file_creation') + }, + url_name='file_creation',) + +file_modification_wizard = FileWizard([ + ('selec-file_modification', FileFormSelection), + ('general-file_modification', FileFormGeneral), + ('adress-file_modification', FileFormAddress), + ('towns-file_modification', TownFormSet), + ('parcels-file_modification', ParcelFormSet), + ('preventive-file_modification', FileFormPreventive), + ('final-file_modification', FinalForm)], + condition_list={ + 'preventive-file_modification':is_preventive('general-file_modif') + }, + url_name='file_modification',) |
