#!/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.forms.formsets import formset_factory from django import forms 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): """ Add previous and current steps to manage the wizard path """ 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' 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) ) 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") total_surface = forms.IntegerField(label=_("Total surface")) address = forms.CharField(label=_(u"Address"), widget=forms.Textarea) class TownForm(forms.Form): form_label = _("Towns") # !FIXME hard_link, reverse_lazy doen't seem to work with formsets town = forms.IntegerField(label=_(u"Town"), widget=widgets.JQueryAutoComplete("/" + settings.URL_PATH + \ 'autocomplete-town', associated_model=models.Town), validators=[models.Town.valid_id]) TownFormSet = formset_factory(TownForm) TownFormSet.form_label = _("Towns") 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")) def is_preventive(self, request, storage): if storage.prefix not in request.session or \ 'step_data' not in request.session[storage.prefix] or \ '0' not in request.session[storage.prefix]['step_data'] or\ '0-file_type' not in request.session[storage.prefix]['step_data']['0']: return False try: file_type = int(request.session[storage.prefix]['step_data']['0']\ ['0-file_type']) return file_type == models.PREVENTIVE except ValueError: return False file_creation_wizard = FileWizard([FileForm1, FileForm2, TownFormSet, FileForm3], url_name='file_creation', condition_list={'3':is_preventive})