1
|
# -*- coding: utf-8 -*-
|
2
|
|
3
|
import os
|
4
|
|
5
|
from datetime import date
|
6
|
|
7
|
from django import forms
|
8
|
from django.conf import settings
|
9
|
from django.forms import ModelForm, Form
|
10
|
import django.contrib.admin.widgets
|
11
|
|
12
|
from calebasse.dossiers.models import (PatientRecord,
|
13
|
PatientAddress, PatientContact, DEFAULT_ACT_NUMBER_TREATMENT,
|
14
|
CmppHealthCareTreatment, CmppHealthCareDiagnostic,
|
15
|
SessadHealthCareNotification, FileState, Status)
|
16
|
from calebasse.ressources.models import (HealthCenter, LargeRegime,
|
17
|
CodeCFTMEA, SocialisationDuration, MDPHRequest, MDPHResponse)
|
18
|
|
19
|
from ajax_select import make_ajax_field
|
20
|
|
21
|
|
22
|
class EditPatientRecordForm(ModelForm):
|
23
|
class Meta:
|
24
|
model = PatientRecord
|
25
|
|
26
|
class SearchForm(Form):
|
27
|
last_name = forms.CharField(label=u'Nom', required=False)
|
28
|
first_name = forms.CharField(label=u'Prénom', required=False)
|
29
|
folder_id = forms.CharField(label=u'Numéro de dossier', required=False)
|
30
|
social_security_id = forms.CharField(label=u"Numéro d'assuré social", required=False)
|
31
|
states = forms.ModelMultipleChoiceField(queryset=Status.objects.all(),
|
32
|
initial=Status.objects.all(),
|
33
|
widget=forms.CheckboxSelectMultiple(attrs={'class':'checkbox_state'}))
|
34
|
|
35
|
def __init__(self, service=None, *args, **kwargs):
|
36
|
self.service = service
|
37
|
super(SearchForm, self).__init__(*args, **kwargs)
|
38
|
self.fields['states'].queryset = Status.objects.filter(services=service)
|
39
|
|
40
|
|
41
|
class StateForm(Form):
|
42
|
patient_id = forms.IntegerField()
|
43
|
service_id = forms.IntegerField()
|
44
|
state_type = forms.CharField(max_length=40)
|
45
|
date = forms.DateField(label=u'Date', localize=True)
|
46
|
comment = forms.CharField(label='Commentaire',
|
47
|
required=False, widget=forms.Textarea)
|
48
|
|
49
|
def clean_date(self):
|
50
|
patient = PatientRecord.objects.get(id=self.cleaned_data['patient_id'])
|
51
|
date_selected = self.cleaned_data['date']
|
52
|
current_state = patient.get_state()
|
53
|
if date_selected < current_state.date_selected.date():
|
54
|
raise forms.ValidationError(u"La date ne peut pas être antérieure à celle du précédent changement d'état.")
|
55
|
return self.cleaned_data['date']
|
56
|
|
57
|
class PatientStateForm(ModelForm):
|
58
|
date_selected = forms.DateTimeField(label=u'Date', localize=True)
|
59
|
comment = forms.CharField(label='Commentaire',
|
60
|
required=False, widget=forms.Textarea)
|
61
|
|
62
|
class Meta:
|
63
|
model = FileState
|
64
|
fields = ('status', 'date_selected', 'comment')
|
65
|
widgets = {
|
66
|
'comment': forms.Textarea(attrs={'cols': 39, 'rows': 4}),
|
67
|
}
|
68
|
|
69
|
def __init__(self, service=None, *args, **kwargs):
|
70
|
self.service = service
|
71
|
super(PatientStateForm, self).__init__(*args, **kwargs)
|
72
|
self.fields['status'].queryset = Status.objects.filter(services=service)
|
73
|
|
74
|
def clean_date_selected(self):
|
75
|
date_selected = self.cleaned_data['date_selected']
|
76
|
next_state = self.instance.get_next_state()
|
77
|
if self.instance.previous_state:
|
78
|
if date_selected < self.instance.previous_state.date_selected:
|
79
|
raise forms.ValidationError(u"La date ne peut pas être antérieure à celle du précédent changement d'état.")
|
80
|
if next_state:
|
81
|
if date_selected > next_state.date_selected:
|
82
|
raise forms.ValidationError(u"La date ne peut pas être postérieure à celle du changement d'état suivant.")
|
83
|
return self.cleaned_data['date_selected']
|
84
|
|
85
|
class NewPatientRecordForm(ModelForm):
|
86
|
date_selected = forms.DateField(label=u"Date de contact", initial=date.today(), localize=True)
|
87
|
|
88
|
class Meta:
|
89
|
model = PatientRecord
|
90
|
fields = ('last_name', 'first_name')
|
91
|
|
92
|
class GeneralForm(ModelForm):
|
93
|
class Meta:
|
94
|
model = PatientRecord
|
95
|
fields = ('comment', 'pause', 'confidential')
|
96
|
widgets = {
|
97
|
'comment': forms.Textarea(attrs={'cols': 50, 'rows': 5}),
|
98
|
}
|
99
|
|
100
|
class CivilStatusForm(ModelForm):
|
101
|
class Meta:
|
102
|
model = PatientRecord
|
103
|
fields = ('first_name', 'last_name', 'birthdate', 'birthplace', 'gender', 'nationality')
|
104
|
|
105
|
|
106
|
class FilteredSelectMultipleMise(django.contrib.admin.widgets.FilteredSelectMultiple):
|
107
|
def __init__(self, **kwargs):
|
108
|
super(FilteredSelectMultipleMise, self).__init__(u'Catégorie', False)
|
109
|
|
110
|
|
111
|
class PhysiologyForm(ModelForm):
|
112
|
cranium_perimeter = forms.DecimalField(label=u"Périmètre cranien",
|
113
|
max_digits=5, decimal_places=2, localize=True,
|
114
|
required=False)
|
115
|
chest_perimeter = forms.DecimalField(label=u"Périmètre thoracique",
|
116
|
max_digits=5, decimal_places=2, localize=True,
|
117
|
required=False)
|
118
|
|
119
|
class Meta:
|
120
|
model = PatientRecord
|
121
|
fields = ('size', 'weight', 'pregnancy_term',
|
122
|
'cranium_perimeter', 'chest_perimeter', 'apgar_score_one',
|
123
|
'apgar_score_two', 'mises_1', 'mises_2', 'mises_3')
|
124
|
widgets = {
|
125
|
'mises_1': FilteredSelectMultipleMise,
|
126
|
'mises_2': FilteredSelectMultipleMise,
|
127
|
'mises_3': FilteredSelectMultipleMise,
|
128
|
}
|
129
|
|
130
|
def __init__(self, instance, **kwargs):
|
131
|
super(PhysiologyForm, self).__init__(instance=instance, **kwargs)
|
132
|
self.fields['mises_1'].queryset = \
|
133
|
CodeCFTMEA.objects.filter(axe=1)
|
134
|
self.fields['mises_2'].queryset = \
|
135
|
CodeCFTMEA.objects.filter(axe=2)
|
136
|
self.fields['mises_3'].queryset = \
|
137
|
CodeCFTMEA.objects.filter(axe=3)
|
138
|
|
139
|
|
140
|
class InscriptionForm(ModelForm):
|
141
|
class Meta:
|
142
|
model = PatientRecord
|
143
|
fields = ('analysemotive', 'familymotive', 'provenance', 'advicegiver')
|
144
|
widgets = {}
|
145
|
|
146
|
class OutForm(ModelForm):
|
147
|
class Meta:
|
148
|
model = PatientRecord
|
149
|
fields = ('outmotive', 'outto')
|
150
|
widgets = {}
|
151
|
|
152
|
class FamilyForm(ModelForm):
|
153
|
class Meta:
|
154
|
model = PatientRecord
|
155
|
fields = ('sibship_place', 'nb_children_family', 'parental_authority',
|
156
|
'family_situation', 'child_custody', 'job_mother', 'job_father',
|
157
|
'rm_mother', 'rm_father', 'family_comment')
|
158
|
widgets = {
|
159
|
'family_comment': forms.Textarea(attrs={'cols': 100, 'rows': 1}),
|
160
|
}
|
161
|
|
162
|
class TransportFrom(ModelForm):
|
163
|
class Meta:
|
164
|
model = PatientRecord
|
165
|
fields = ('transporttype', 'transportcompany')
|
166
|
|
167
|
class PaperIDForm(ModelForm):
|
168
|
class Meta:
|
169
|
model = PatientRecord
|
170
|
fields = ('paper_id', )
|
171
|
|
172
|
class PolicyHolderForm(ModelForm):
|
173
|
class Meta:
|
174
|
model = PatientRecord
|
175
|
fields = ('policyholder', )
|
176
|
widgets = { 'policyholder': forms.RadioSelect() }
|
177
|
|
178
|
class FollowUpForm(ModelForm):
|
179
|
coordinators = make_ajax_field(PatientRecord, 'coordinators', 'worker', True)
|
180
|
class Meta:
|
181
|
model = PatientRecord
|
182
|
fields = ('coordinators', 'externaldoctor', 'externalintervener')
|
183
|
|
184
|
class PatientContactForm(ModelForm):
|
185
|
health_org = forms.CharField(label=u"Numéro de l'organisme destinataire", required=False)
|
186
|
|
187
|
class Meta:
|
188
|
model = PatientContact
|
189
|
widgets = {
|
190
|
'contact_comment': forms.Textarea(attrs={'cols': 50, 'rows': 2}),
|
191
|
'key': forms.TextInput(attrs={'size': 4}),
|
192
|
'twinning_rank': forms.TextInput(attrs={'size': 4}),
|
193
|
'health_org': forms.TextInput(attrs={'size': 9}),
|
194
|
'addresses': forms.CheckboxSelectMultiple(),
|
195
|
}
|
196
|
|
197
|
def __init__(self, *args, **kwargs):
|
198
|
self.patient = kwargs.pop('patient', None)
|
199
|
super(PatientContactForm, self).__init__(*args,**kwargs)
|
200
|
if self.instance and self.instance.health_center:
|
201
|
self.fields['health_org'].initial = self.instance.health_center.large_regime.code + self.instance.health_center.health_fund + self.instance.health_center.code
|
202
|
if self.patient:
|
203
|
self.fields['addresses'].queryset = self.patient.addresses
|
204
|
|
205
|
def clean(self):
|
206
|
cleaned_data = super(PatientContactForm, self).clean()
|
207
|
health_org = cleaned_data.get('health_org')
|
208
|
other_health_center = cleaned_data.get('other_health_center')
|
209
|
if health_org:
|
210
|
msg = None
|
211
|
lr = None
|
212
|
hc = None
|
213
|
if len(health_org) < 5:
|
214
|
msg = u"Numéro inférieur à 5 chiffres."
|
215
|
else:
|
216
|
try:
|
217
|
lr = LargeRegime.objects.get(code=health_org[:2])
|
218
|
except:
|
219
|
msg = u"Grand régime %s inconnu." % health_org[:2]
|
220
|
else:
|
221
|
hcs = HealthCenter.objects.filter(health_fund=health_org[2:5], large_regime=lr)
|
222
|
if not hcs:
|
223
|
msg = u"Caisse %s inconnue." % health_org[2:5]
|
224
|
elif len(hcs) == 1:
|
225
|
hc = hcs[0]
|
226
|
if not other_health_center and len(health_org) == 9:
|
227
|
other_health_center = health_org[5:9]
|
228
|
else:
|
229
|
if len(health_org) == 9:
|
230
|
hcs = hcs.filter(code=health_org[5:9])
|
231
|
if not hcs:
|
232
|
msg = u"Centre %s inconnu." % health_org[5:9]
|
233
|
elif len(hcs) == 1:
|
234
|
hc = hcs[0]
|
235
|
else:
|
236
|
msg = u"Ceci ne devrait pas arriver, %s n'est pas unique." % health_org
|
237
|
else:
|
238
|
msg = "Plusieurs centres possibles, précisez parmi :"
|
239
|
for hc in hcs:
|
240
|
msg += " %s" % str(hc)
|
241
|
if msg:
|
242
|
self._errors["health_org"] = self.error_class([msg])
|
243
|
else:
|
244
|
cleaned_data['large_regime'] = lr.code
|
245
|
cleaned_data['health_center'] = hc
|
246
|
cleaned_data['other_health_center'] = other_health_center
|
247
|
return cleaned_data
|
248
|
|
249
|
|
250
|
class PatientAddressForm(ModelForm):
|
251
|
|
252
|
class Meta:
|
253
|
model = PatientAddress
|
254
|
widgets = {
|
255
|
'comment': forms.Textarea(attrs={'cols': 40, 'rows': 4}),
|
256
|
'zip_code': forms.TextInput(attrs={'size': 10}),
|
257
|
'number': forms.TextInput(attrs={'size': 10}),
|
258
|
}
|
259
|
|
260
|
|
261
|
class CmppHealthCareTreatmentForm(ModelForm):
|
262
|
class Meta:
|
263
|
model = CmppHealthCareTreatment
|
264
|
fields = ('start_date', 'request_date',
|
265
|
'agree_date', 'insist_date', 'end_date', 'act_number',
|
266
|
'prolongation', 'comment', 'patient', 'author')
|
267
|
widgets = {
|
268
|
'comment': forms.Textarea(attrs={'cols': 40, 'rows': 4}),
|
269
|
'patient': forms.HiddenInput(),
|
270
|
'author': forms.HiddenInput(),
|
271
|
}
|
272
|
|
273
|
def clean(self):
|
274
|
cleaned_data = super(CmppHealthCareTreatmentForm, self).clean()
|
275
|
if self.instance.pk and cleaned_data.get('act_number') < self.instance.get_nb_acts_cared():
|
276
|
msg = u"Le nombre d'actes ne peut être inférieur au \
|
277
|
nombre d'actes déja pris en charge (%d)." \
|
278
|
% self.instance.get_nb_acts_cared()
|
279
|
self._errors["act_number"] = self.error_class([msg])
|
280
|
return cleaned_data
|
281
|
|
282
|
|
283
|
class CmppHealthCareDiagnosticForm(ModelForm):
|
284
|
class Meta:
|
285
|
model = CmppHealthCareDiagnostic
|
286
|
fields = ('start_date', 'request_date',
|
287
|
'agree_date', 'insist_date', 'act_number',
|
288
|
'comment', 'patient', 'author')
|
289
|
widgets = {
|
290
|
'comment': forms.Textarea(attrs={'cols': 39, 'rows': 4}),
|
291
|
'patient': forms.HiddenInput(),
|
292
|
'author': forms.HiddenInput(),
|
293
|
}
|
294
|
|
295
|
def clean(self):
|
296
|
cleaned_data = super(CmppHealthCareDiagnosticForm, self).clean()
|
297
|
if self.instance.pk and cleaned_data.get('act_number') < self.instance.get_nb_acts_cared():
|
298
|
msg = u"Le nombre d'actes ne peut être inférieur au \
|
299
|
nombre d'actes déja pris en charge (%d)." \
|
300
|
% self.instance.get_nb_acts_cared()
|
301
|
self._errors["act_number"] = self.error_class([msg])
|
302
|
return cleaned_data
|
303
|
|
304
|
|
305
|
class SessadHealthCareNotificationForm(ModelForm):
|
306
|
class Meta:
|
307
|
model = SessadHealthCareNotification
|
308
|
fields = ('start_date', 'end_date',
|
309
|
'request_date', 'agree_date', 'insist_date',
|
310
|
'comment', 'patient', 'author')
|
311
|
widgets = {
|
312
|
'comment': forms.Textarea(attrs={'cols': 40, 'rows': 4}),
|
313
|
'patient': forms.HiddenInput(),
|
314
|
'author': forms.HiddenInput(),
|
315
|
}
|
316
|
|
317
|
|
318
|
class SocialisationDurationForm(ModelForm):
|
319
|
school = make_ajax_field(SocialisationDuration, 'school', 'school', False)
|
320
|
class Meta:
|
321
|
model = SocialisationDuration
|
322
|
fields = ('school', 'start_date',
|
323
|
'end_date', 'level', 'contact', 'comment')
|
324
|
widgets = {
|
325
|
'contact': forms.Textarea(attrs={'cols': 39, 'rows': 2}),
|
326
|
'comment': forms.Textarea(attrs={'cols': 39, 'rows': 4}),
|
327
|
}
|
328
|
|
329
|
class MDPHRequestForm(ModelForm):
|
330
|
class Meta:
|
331
|
model = MDPHRequest
|
332
|
fields = ('start_date', 'mdph', 'comment')
|
333
|
widgets = {
|
334
|
'comment': forms.Textarea(attrs={'cols': 39, 'rows': 4}),
|
335
|
}
|
336
|
|
337
|
class MDPHResponseForm(ModelForm):
|
338
|
class Meta:
|
339
|
model = MDPHResponse
|
340
|
fields = ('start_date', 'end_date', 'mdph', 'comment',
|
341
|
'type_aide', 'name', 'rate')
|
342
|
widgets = {
|
343
|
'comment': forms.Textarea(attrs={'cols': 39, 'rows': 4}),
|
344
|
}
|
345
|
|
346
|
class AvailableRtfTemplates:
|
347
|
def __iter__(self):
|
348
|
if not settings.RTF_TEMPLATES_DIRECTORY:
|
349
|
return iter([])
|
350
|
templates = []
|
351
|
for filename in os.listdir(settings.RTF_TEMPLATES_DIRECTORY):
|
352
|
templates.append((filename, filename[:-4]))
|
353
|
return iter(templates)
|
354
|
|
355
|
class GenerateRtfForm(Form):
|
356
|
template_filename = forms.ChoiceField(choices=AvailableRtfTemplates())
|
357
|
address = forms.CharField(widget=forms.Textarea(attrs={'rows':5}), required=False)
|
358
|
first_name = forms.CharField(required=False)
|
359
|
last_name = forms.CharField(required=False)
|
360
|
birthdate = forms.CharField(required=False)
|
361
|
appointment_date = forms.CharField(required=False)
|
362
|
appointment_begin_hour = forms.CharField(required=False)
|
363
|
appointment_intervenants = forms.CharField(required=False)
|
364
|
|
365
|
class QuotationsForm(Form):
|
366
|
|
367
|
def __init__(self, service=None, *args, **kwargs):
|
368
|
self.service = service
|
369
|
super(QuotationsForm, self).__init__(*args, **kwargs)
|
370
|
self.fields['states'].queryset = Status.objects.filter(services=service)
|
371
|
|
372
|
states = forms.ModelMultipleChoiceField(queryset=Status.objects.all(),
|
373
|
initial=Status.objects.all(),
|
374
|
widget=forms.CheckboxSelectMultiple(attrs={'class':'checkbox_state'}))
|
375
|
date_actes_start = forms.DateField(label=u'Date', localize=True)
|
376
|
date_actes_end = forms.DateField(label=u'Date', localize=True)
|
377
|
without_quotations = forms.BooleanField()
|