#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2010 Étienne Loks # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU 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 General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # See the file COPYING for details. """ Forms definition """ import datetime from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.template import Context, RequestContext from django.shortcuts import render_to_response from django import forms from merlin.wizards.utils import Step as BasicStep from merlin.wizards.session import SessionWizard from formwizard.forms import NamedUrlSessionFormWizard import models import widgets from ishtar import settings from django.utils.functional import lazy reverse_lazy = lazy(reverse, unicode) class Wizard(NamedUrlSessionFormWizard): def get_template_context(self, request, storage, form=None): context = super(Wizard, self).get_template_context(request, storage, form) step = self.get_first_step(request, storage) current_step = storage.get_current_step() context.update({'current_step':self.form_list[current_step]}) if step == current_step: return context previous_steps = [] while step: if step == current_step: context.update({'previous_steps':previous_steps}) return context else: previous_steps.append(self.form_list[step]) step = self.get_next_step(request, storage, step) context.update({'previous_steps':previous_steps}) return context class FileWizard(Wizard): def get_template(self, request, form): return ['file_wizard.html'] def done(self, request, storage, form_list): return render_to_response( 'file_done.html', {'form_list': [form.cleaned_data for form in form_list]}, context_instance=RequestContext(request) ) """ def process_step(self, request, step, form): if models.PREVENTIVE and 'file_type' in form.cleaned_data: file_type = int(form.cleaned_data['file_type']) if file_type == models.PREVENTIVE: preventive_step = Step('preventive', _(u"Preventive informations"), FileForm3) localisation_step = self.get_step(request, 'localisation') self.insert_after(request, localisation_step, preventive_step) else: preventive_step = self.get_step(request, 'preventive') self.remove_step(request, preventive_step) """ class FileForm1(forms.Form): form_label = _("General") in_charge = forms.IntegerField(label=_("Person in charge"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person'), associated_model=models.Person), validators=[models.Person.valid_id]) year = forms.IntegerField(label=_("Year"), initial=lambda:datetime.datetime.now().year) internal_reference = forms.CharField(label=_(u"Internal reference"), max_length=60) creation_date = forms.DateField(label=_(u"Creation date"), initial=datetime.datetime.now) file_type = forms.ChoiceField(label=_("File type"), choices=models.FileType.get_types()) comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea, required=False) class FileForm2(forms.Form): form_label = _("Address") town = forms.IntegerField(label=_(u"Town"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-town'), associated_model=models.Town), validators=[models.Town.valid_id]) total_surface = forms.IntegerField(label=_("Total surface")) address = forms.CharField(label=_(u"Address"), widget=forms.Textarea) class FileForm3(forms.Form): form_label = _("Preventive informations") general_contractor = forms.IntegerField(label=_(u"General contractor"), widget=widgets.JQueryAutoComplete( reverse_lazy('autocomplete-organization'), associated_model=models.Organization), validators=[models.Organization.valid_id]) total_developed_surface = forms.IntegerField( label=_("Total developed surface")) if settings.COUNTRY == 'fr': saisine_type = forms.ChoiceField(label=_("Saisine type"), choices=models.SaisineType.get_types()) reception_date = forms.DateField(label=_(u"Reception date")) file_creation_wizard = FileWizard([FileForm1, FileForm2, FileForm3], url_name='file_creation')