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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2014-2015 É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
# 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 Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# See the file COPYING for details.
import datetime
from django import forms
from django.core import validators
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from ishtar_common.models import Person, PersonType, Town, valid_id
from archaeological_files import models
from ishtar_common.forms import get_now, reverse_lazy
from archaeological_files.forms import GENERAL_CONTRACTOR, \
GENERAL_CONTRACTOR_ORGA, RESPONSIBLE_PLANNING_SERVICE, \
RESPONSIBLE_PLANNING_SERVICE_ORGA
from ishtar_common import widgets
class FileFormGeneral(forms.Form):
form_label = _("General")
associated_models = {'file_type': models.FileType}
file_type = forms.ChoiceField(label=_("File type"), choices=[])
year = forms.IntegerField(label=_("Year"),
initial=lambda: datetime.datetime.now().year,
validators=[validators.MinValueValidator(1900),
validators.MaxValueValidator(2100)])
creation_date = forms.DateField(label=_(u"Creation date"),
initial=get_now, widget=widgets.JQueryDate)
reception_date = forms.DateField(label=_(u"Reception date"),
widget=widgets.JQueryDate)
def __init__(self, *args, **kwargs):
super(FileFormGeneral, self).__init__(*args, **kwargs)
self.fields['file_type'].choices = models.FileType.get_types()
self.fields['file_type'].help_text = models.FileType.get_help()
class FileFormPreventiveType(forms.Form):
form_label = u"Saisine"
associated_models = {'saisine_type': models.SaisineType,
'permit_type': models.PermitType}
permit_type = forms.ChoiceField(label=_(u"Permit type"), required=False,
choices=[])
saisine_type = forms.ChoiceField(label=_(u"Saisine type"),
choices=[])
def __init__(self, *args, **kwargs):
super(FileFormPreventiveType, self).__init__(*args, **kwargs)
self.fields['saisine_type'].choices = models.SaisineType.get_types()
self.fields['saisine_type'].help_text = models.SaisineType.get_help()
self.fields['permit_type'].choices = \
models.PermitType.get_types(default='NP')
self.fields['permit_type'].help_text = models.PermitType.get_help()
class FileFormPlanning(forms.Form):
form_label = _(u"Planning")
base_model = 'town'
associated_models = {'town': Town}
name = forms.CharField(label=_(u"Planning name"), required=False,
max_length=100)
town = widgets.MultipleAutocompleteField(
model=Town, label=_("Towns"), required=False)
locality = forms.CharField(label=_(u"Locality"), max_length=100,
required=False)
address = forms.CharField(
label=_(u"Address (number/street)"),
widget=forms.Textarea(attrs={"placeholder": _(u"Number/street")}),
required=False)
postal_code = forms.CharField(label=_(u"Postal code"), max_length=10,
required=False)
total_surface = forms.IntegerField(
required=False,
widget=widgets.AreaWidget,
label=_(u"Total surface (m²)"),
validators=[validators.MinValueValidator(0),
validators.MaxValueValidator(999999999)])
total_developed_surface = forms.IntegerField(
widget=widgets.AreaWidget,
label=_(u"Total developed surface (m²)"),
required=False,
validators=[validators.MinValueValidator(0),
validators.MaxValueValidator(999999999)])
class FileFormResearchAddress(forms.Form):
form_label = _(u"Address")
base_model = 'town'
associated_models = {'town': Town}
town = widgets.MultipleAutocompleteField(
model=Town, label=_("Towns"), required=False)
locality = forms.CharField(label=_(u"Locality"), max_length=100,
required=False)
address = forms.CharField(
label=_(u"Address (number/street)"),
widget=forms.Textarea(attrs={"placeholder": _(u"Number/street")}),
required=False)
postal_code = forms.CharField(label=_(u"Postal code"), max_length=10,
required=False)
class PersonOrgaForm(forms.Form):
PERSON_FIELD = 'TO BE DEFINED'
PERSON_TYPE = GENERAL_CONTRACTOR
PERSON_LABEL = ""
ORGA_FIELD = 'TO BE DEFINED'
ORGA_TYPE = GENERAL_CONTRACTOR_ORGA
ORGA_LABEL = ""
def _media(self):
if self.status == 'corporation':
return forms.Media(js=('pdl/JQueryCorporation.js',))
media = property(_media)
def __init__(self, *args, **kwargs):
# get the status: natural person or corporation
DEFAULT_STATUS = 'natural'
current_status = ''
if 'data' in kwargs:
# the order is important: PERSON can have an ORGA
for field in [self.ORGA_FIELD, self.PERSON_FIELD]:
current_item_key = (
(kwargs['prefix'] + '-')
if kwargs.get('prefix') else '') + field
if kwargs['data'] and kwargs['data'].get(current_item_key):
model = self.associated_models[field]
try:
model.objects.get(
pk=kwargs['data'][current_item_key])
current_status = 'natural' \
if field == self.PERSON_FIELD else 'corporation'
except (model.DoesNotExist, ValueError):
pass
initial = kwargs.get("initial", {})
if not current_status:
# the order is important: PERSON can have an ORGA
for field in [self.ORGA_FIELD, self.PERSON_FIELD]:
value = initial.get(field)
model = self.associated_models[field]
try:
model.objects.get(pk=value)
current_status = 'natural' if field == self.PERSON_FIELD \
else 'corporation'
except (model.DoesNotExist, ValueError):
pass
status = ''
if 'status' in kwargs:
status = kwargs.pop('status')
if current_status != status:
if kwargs.get('data'):
# status is different from the existing - clear fields
kwargs.pop('data')
elif current_status:
status = current_status
else:
status = DEFAULT_STATUS
self.status = status
if status not in ('natural', 'corporation'):
status = DEFAULT_STATUS
super(PersonOrgaForm, self).__init__(*args, **kwargs)
# distinct widget for natural and corporation
if status == 'natural':
self.fields[self.PERSON_FIELD] = forms.IntegerField(
label=self.PERSON_LABEL,
required=False,
initial=initial.get(self.PERSON_FIELD, None),
widget=widgets.JQueryPersonOrganization(
reverse_lazy('autocomplete-person',
args=[self.PERSON_TYPE.pk]),
reverse_lazy('person_create'),
model=Person,
limit={'person_types': [self.PERSON_TYPE.pk],
'attached_to__isnull': True},
js_template='ishtar/blocks/JQueryNaturalPerson.js',
new=True),
validators=[valid_id(Person)])
else:
self.fields[self.ORGA_FIELD] = forms.IntegerField(
label=self.ORGA_LABEL,
required=False,
initial=initial.get(self.ORGA_FIELD, None),
widget=widgets.JQueryAutoComplete(
reverse_lazy('autocomplete-organization',
args=[self.ORGA_TYPE.pk]),
limit={'organization_type': [self.ORGA_TYPE.pk]},
associated_model=models.Organization,
new=True),
validators=[valid_id(models.Organization)])
class FileFormGeneralContractor(PersonOrgaForm):
form_label = _(u"General contractor")
associated_models = {'general_contractor': models.Person,
'corporation_general_contractor': models.Organization}
PERSON_FIELD = 'general_contractor'
PERSON_TYPE = GENERAL_CONTRACTOR
PERSON_LABEL = _(u"General contractor")
ORGA_FIELD = 'corporation_general_contractor'
ORGA_TYPE = GENERAL_CONTRACTOR_ORGA
ORGA_LABEL = _(u"General contractor")
# default initialisation before dynamic
general_contractor = forms.IntegerField(
label=" ", widget=forms.HiddenInput, required=False)
corporation_general_contractor = forms.IntegerField(
label=" ", widget=forms.HiddenInput, required=False)
class FileFormPlanningService(PersonOrgaForm):
form_label = _(u"Town planning service")
associated_models = {'responsible_town_planning_service': models.Person,
'planning_service': models.Organization}
reference_number = forms.IntegerField(label=_(u"File reference"),
required=False)
PERSON_FIELD = 'responsible_town_planning_service'
PERSON_TYPE = RESPONSIBLE_PLANNING_SERVICE
PERSON_LABEL = _(u"Responsible town planning service")
ORGA_FIELD = 'planning_service'
ORGA_TYPE = RESPONSIBLE_PLANNING_SERVICE_ORGA
ORGA_LABEL = _(u"Planning service")
# default initialisation before dynamic
responsible_town_planning_service = forms.IntegerField(
label=" ", widget=forms.HiddenInput, required=False)
planning_service = forms.IntegerField(label=" ", widget=forms.HiddenInput,
required=False)
class FileFormInstruction(forms.Form):
form_label = u"Instruction SRA"
associated_models = {'in_charge': models.Person,
'related_file': models.File}
in_charge = forms.IntegerField(
label=_("Person in charge"),
widget=widgets.JQueryAutoComplete(
reverse_lazy(
'autocomplete-person',
args=[PersonType.objects.get(txt_idx='sra_agent').pk]),
limit={
'person_types': [
PersonType.objects.get(txt_idx='sra_agent').pk]
},
associated_model=Person, new=True),
validators=[valid_id(Person)])
related_file = forms.IntegerField(
label=_("Related file"), required=False,
widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-file'),
associated_model=models.File),
validators=[valid_id(models.File)])
comment = forms.CharField(label=_(u"Comment"), widget=forms.Textarea,
required=False)
instruction_deadline = forms.DateField(widget=widgets.JQueryDate,
required=False)
year = forms.IntegerField(label=_("Year"),
validators=[validators.MinValueValidator(1900),
validators.MaxValueValidator(2100)])
numeric_reference = forms.IntegerField(label=_("Numeric reference"),
required=False)
end_date = forms.DateField(widget=widgets.JQueryDate, required=False)
def __init__(self, *args, **kwargs):
c_year = datetime.date.today().year
if 'year' in kwargs:
c_year = kwargs.pop('year')
super(FileFormInstruction, self).__init__(*args, **kwargs)
self.fields['year'].initial = c_year
self.fields['year'].widget.attrs.update({'readonly': 'readonly'})
c_num, lasts = 0, ""
q = models.File.objects.filter(numeric_reference__isnull=False,
year=c_year
).order_by('-numeric_reference')
if q.count():
c_num = q.all()[0].numeric_reference
lasts = u"SRA %s-%d" % (unicode(c_year), c_num)
lbl = self.fields['numeric_reference'].label
if lasts:
lbl += u"<br/>(dernière entrée : %s)" % lasts
self.fields['numeric_reference'].label = mark_safe(lbl)
self.fields['numeric_reference'].initial = c_num + 1
|