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