summaryrefslogtreecommitdiff
path: root/ishtar/furnitures/forms.py
blob: de221da70e34a14aa4621365c474dc89894a2d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010  É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 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 <http://www.gnu.org/licenses/>.

# 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})