1
|
# -*- coding: utf-8 -*-
|
2
|
|
3
|
import os
|
4
|
|
5
|
from datetime import datetime, date
|
6
|
|
7
|
from django.conf import settings
|
8
|
from django.db import models
|
9
|
from django.http import HttpResponseRedirect, HttpResponse
|
10
|
from django.views.generic import View
|
11
|
from django.views.generic.edit import DeleteView
|
12
|
from django.contrib import messages
|
13
|
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
14
|
from django.core.files import File
|
15
|
from django.forms import Form
|
16
|
from django.utils import formats
|
17
|
from django.shortcuts import get_object_or_404
|
18
|
|
19
|
from calebasse import cbv
|
20
|
from calebasse.doc_templates import make_doc_from_template
|
21
|
from calebasse.dossiers import forms
|
22
|
from calebasse.dossiers.views_utils import get_next_rdv, get_last_rdv, get_status
|
23
|
from calebasse.dossiers.transport import render_transport
|
24
|
from calebasse.agenda.models import Event, EventWithAct
|
25
|
from calebasse.actes.models import Act
|
26
|
from calebasse.agenda.appointments import Appointment
|
27
|
from calebasse.dossiers.models import (PatientRecord, PatientContact,
|
28
|
PatientAddress, Status, FileState, create_patient, CmppHealthCareTreatment,
|
29
|
CmppHealthCareDiagnostic, SessadHealthCareNotification, HealthCare,
|
30
|
TransportPrescriptionLog, ProtectionState)
|
31
|
from calebasse.dossiers.states import STATES_MAPPING, STATES_BTN_MAPPER
|
32
|
from calebasse.ressources.models import (Service,
|
33
|
SocialisationDuration, MDPHRequest, MDPHResponse)
|
34
|
from calebasse.facturation.list_acts import list_acts_for_billing_CMPP_per_patient
|
35
|
|
36
|
from calebasse.decorators import validator_only
|
37
|
|
38
|
|
39
|
class NewPatientRecordView(cbv.FormView, cbv.ServiceViewMixin):
|
40
|
form_class = forms.NewPatientRecordForm
|
41
|
template_name = 'dossiers/patientrecord_new.html'
|
42
|
success_url = '..'
|
43
|
patient = None
|
44
|
|
45
|
def post(self, request, *args, **kwarg):
|
46
|
self.user = request.user
|
47
|
return super(NewPatientRecordView, self).post(request, *args, **kwarg)
|
48
|
|
49
|
def form_valid(self, form):
|
50
|
self.patient = create_patient(form.data['first_name'], form.data['last_name'], self.service,
|
51
|
self.user, date_selected=datetime.strptime(form.data['date_selected'], "%d/%m/%Y"))
|
52
|
return super(NewPatientRecordView, self).form_valid(form)
|
53
|
|
54
|
def get_success_url(self):
|
55
|
return '%s/view' % self.patient.id
|
56
|
|
57
|
new_patient_record = NewPatientRecordView.as_view()
|
58
|
|
59
|
class RecordPatientRecordIdMixing(object):
|
60
|
def dispatch(self, request, *args, **kwargs):
|
61
|
self.patientrecord_id = request.session['patientrecord_id'] = int(kwargs['patientrecord_id'])
|
62
|
return super(RecordPatientRecordIdMixing, self).dispatch(request, *args, **kwargs)
|
63
|
|
64
|
def get_form_kwargs(self):
|
65
|
kwargs = super(RecordPatientRecordIdMixing, self).get_form_kwargs()
|
66
|
kwargs['patient'] = PatientRecord.objects.get(id=self.patientrecord_id)
|
67
|
return kwargs
|
68
|
|
69
|
class NewPatientContactView(RecordPatientRecordIdMixing, cbv.CreateView):
|
70
|
model = PatientContact
|
71
|
form_class = forms.PatientContactForm
|
72
|
template_name = 'dossiers/patientcontact_new.html'
|
73
|
success_url = '../view#tab=2'
|
74
|
|
75
|
def get_initial(self):
|
76
|
initial = super(NewPatientContactView, self).get_initial()
|
77
|
fields = self.form_class.base_fields.keys()
|
78
|
for arg in self.request.GET.keys():
|
79
|
if arg in fields:
|
80
|
if arg == 'addresses':
|
81
|
value = self.request.GET.getlist(arg)
|
82
|
else:
|
83
|
value = self.request.GET.get(arg)
|
84
|
initial[arg] = value
|
85
|
if initial:
|
86
|
if not initial.has_key('addresses'):
|
87
|
initial['addresses'] = []
|
88
|
patient = PatientRecord.objects.get(id=self.patientrecord_id)
|
89
|
addresses = patient.addresses.order_by('-id')
|
90
|
if addresses:
|
91
|
initial['addresses'].append(addresses[0].pk)
|
92
|
return initial
|
93
|
|
94
|
new_patient_contact = NewPatientContactView.as_view()
|
95
|
|
96
|
class UpdatePatientContactView(RecordPatientRecordIdMixing, cbv.UpdateView):
|
97
|
model = PatientContact
|
98
|
form_class = forms.PatientContactForm
|
99
|
template_name = 'dossiers/patientcontact_new.html'
|
100
|
success_url = '../../view#tab=2'
|
101
|
|
102
|
update_patient_contact = UpdatePatientContactView.as_view()
|
103
|
|
104
|
class DeletePatientContactView(cbv.DeleteView):
|
105
|
model = PatientContact
|
106
|
form_class = forms.PatientContactForm
|
107
|
template_name = 'dossiers/patientcontact_confirm_delete.html'
|
108
|
success_url = '../../view#tab=2'
|
109
|
|
110
|
def post(self, request, *args, **kwargs):
|
111
|
try:
|
112
|
patient = PatientRecord.objects.get(id=kwargs.get('pk'))
|
113
|
except PatientRecord.DoesNotExist:
|
114
|
return super(DeletePatientContactView, self).post(request, *args, **kwargs)
|
115
|
# the contact is also a patient record; it shouldn't be deleted; just
|
116
|
# altered to remove an address
|
117
|
patient.addresses.remove(self.request.GET['address'])
|
118
|
return HttpResponseRedirect(self.get_success_url())
|
119
|
|
120
|
delete_patient_contact = DeletePatientContactView.as_view()
|
121
|
|
122
|
class NewPatientAddressView(cbv.CreateView):
|
123
|
model = PatientAddress
|
124
|
form_class = forms.PatientAddressForm
|
125
|
template_name = 'dossiers/patientaddress_new.html'
|
126
|
success_url = '../view#tab=2'
|
127
|
|
128
|
def get_success_url(self):
|
129
|
return self.success_url
|
130
|
|
131
|
def form_valid(self, form):
|
132
|
patientaddress = form.save()
|
133
|
patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
134
|
patientrecord.addresses.add(patientaddress)
|
135
|
messages.add_message(self.request, messages.INFO, u'Nouvelle adresse enregistrée avec succès.')
|
136
|
return HttpResponseRedirect(self.get_success_url())
|
137
|
|
138
|
new_patient_address = NewPatientAddressView.as_view()
|
139
|
|
140
|
class UpdatePatientAddressView(cbv.UpdateView):
|
141
|
model = PatientAddress
|
142
|
form_class = forms.PatientAddressForm
|
143
|
template_name = 'dossiers/patientaddress_new.html'
|
144
|
success_url = '../../view#tab=2'
|
145
|
|
146
|
def form_valid(self, form):
|
147
|
messages.add_message(self.request,
|
148
|
messages.INFO,
|
149
|
u'Modification enregistrée avec succès.')
|
150
|
return super(UpdatePatientAddressView, self).form_valid(form)
|
151
|
|
152
|
update_patient_address = UpdatePatientAddressView.as_view()
|
153
|
|
154
|
class DeletePatientAddressView(cbv.DeleteView):
|
155
|
model = PatientAddress
|
156
|
form_class = forms.PatientAddressForm
|
157
|
template_name = 'dossiers/patientaddress_confirm_delete.html'
|
158
|
success_url = '../../view#tab=2'
|
159
|
|
160
|
delete_patient_address = DeletePatientAddressView.as_view()
|
161
|
|
162
|
|
163
|
class NewHealthCareView(cbv.CreateView):
|
164
|
|
165
|
def get_initial(self):
|
166
|
initial = super(NewHealthCareView, self).get_initial()
|
167
|
initial['author'] = self.request.user.id
|
168
|
initial['patient'] = self.kwargs['patientrecord_id']
|
169
|
return initial
|
170
|
|
171
|
new_healthcare_treatment = \
|
172
|
NewHealthCareView.as_view(model=CmppHealthCareTreatment,
|
173
|
template_name = 'dossiers/generic_form.html',
|
174
|
success_url = '../view#tab=3',
|
175
|
form_class=forms.CmppHealthCareTreatmentForm)
|
176
|
new_healthcare_diagnostic = \
|
177
|
NewHealthCareView.as_view(model=CmppHealthCareDiagnostic,
|
178
|
template_name = 'dossiers/generic_form.html',
|
179
|
success_url = '../view#tab=3',
|
180
|
form_class=forms.CmppHealthCareDiagnosticForm)
|
181
|
new_healthcare_notification = \
|
182
|
NewHealthCareView.as_view(model=SessadHealthCareNotification,
|
183
|
template_name = 'dossiers/generic_form.html',
|
184
|
success_url = '../view#tab=3',
|
185
|
form_class=forms.SessadHealthCareNotificationForm)
|
186
|
update_healthcare_treatment = \
|
187
|
cbv.UpdateView.as_view(model=CmppHealthCareTreatment,
|
188
|
template_name = 'dossiers/generic_form.html',
|
189
|
success_url = '../../view#tab=3',
|
190
|
form_class=forms.CmppHealthCareTreatmentForm)
|
191
|
update_healthcare_diagnostic = \
|
192
|
cbv.UpdateView.as_view(model=CmppHealthCareDiagnostic,
|
193
|
template_name = 'dossiers/generic_form.html',
|
194
|
success_url = '../../view#tab=3',
|
195
|
form_class=forms.CmppHealthCareDiagnosticForm)
|
196
|
update_healthcare_notification = \
|
197
|
cbv.UpdateView.as_view(model=SessadHealthCareNotification,
|
198
|
template_name = 'dossiers/generic_form.html',
|
199
|
success_url = '../../view#tab=3',
|
200
|
form_class=forms.SessadHealthCareNotificationForm)
|
201
|
delete_healthcare_treatment = \
|
202
|
cbv.DeleteView.as_view(model=CmppHealthCareTreatment,
|
203
|
template_name = 'dossiers/generic_confirm_delete.html',
|
204
|
success_url = '../../view#tab=3')
|
205
|
delete_healthcare_diagnostic = \
|
206
|
cbv.DeleteView.as_view(model=CmppHealthCareDiagnostic,
|
207
|
template_name = 'dossiers/generic_confirm_delete.html',
|
208
|
success_url = '../../view#tab=3')
|
209
|
delete_healthcare_notification = \
|
210
|
cbv.DeleteView.as_view(model=SessadHealthCareNotification,
|
211
|
template_name = 'dossiers/generic_confirm_delete.html',
|
212
|
success_url = '../../view#tab=3')
|
213
|
|
214
|
|
215
|
class StateFormView(cbv.FormView):
|
216
|
template_name = 'dossiers/state.html'
|
217
|
form_class = forms.StateForm
|
218
|
success_url = './view#tab=0'
|
219
|
|
220
|
|
221
|
def post(self, request, *args, **kwarg):
|
222
|
self.user = request.user
|
223
|
return super(StateFormView, self).post(request, *args, **kwarg)
|
224
|
|
225
|
def form_valid(self, form):
|
226
|
service = Service.objects.get(id=form.data['service_id'])
|
227
|
status = Status.objects.filter(services=service).filter(type=form.data['state_type'])
|
228
|
patient = PatientRecord.objects.get(id=form.data['patient_id'])
|
229
|
date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
|
230
|
patient.set_state(status[0], self.user, date_selected, form.data['comment'])
|
231
|
return super(StateFormView, self).form_valid(form)
|
232
|
|
233
|
state_form = StateFormView.as_view()
|
234
|
|
235
|
class PatientRecordView(cbv.UpdateView):
|
236
|
model = PatientRecord
|
237
|
template_name = 'dossiers/patientrecord_update.html'
|
238
|
|
239
|
def get_context_data(self, **kwargs):
|
240
|
ctx = super(PatientRecordView, self).get_context_data(**kwargs)
|
241
|
ctx['object'].create_diag_healthcare(self.request.user)
|
242
|
ctx['object'].automated_switch_state(self.request.user)
|
243
|
current_state = ctx['object'].get_current_state()
|
244
|
if not current_state:
|
245
|
current_state = ctx['object'].get_state()
|
246
|
ctx['future_state'] = True
|
247
|
if STATES_MAPPING.has_key(current_state.status.type):
|
248
|
state = STATES_MAPPING[current_state.status.type]
|
249
|
else:
|
250
|
state = current_state.status.name
|
251
|
ctx['current_state'] = current_state
|
252
|
ctx['service_id'] = self.service.id
|
253
|
ctx['states'] = FileState.objects.filter(patient=self.object) \
|
254
|
.filter(status__services=self.service) \
|
255
|
.order_by('-date_selected')
|
256
|
return ctx
|
257
|
|
258
|
patient_record = PatientRecordView.as_view()
|
259
|
|
260
|
class PatientRecordGeneralView(cbv.UpdateView):
|
261
|
model = PatientRecord
|
262
|
form_class = forms.GeneralForm
|
263
|
template_name = 'dossiers/patientrecord_tab1_general.html'
|
264
|
success_url = './view'
|
265
|
|
266
|
def get_context_data(self, **kwargs):
|
267
|
ctx = super(PatientRecordGeneralView, self).get_context_data(**kwargs)
|
268
|
ctx['nb_place_of_lifes'] = ctx['object'].addresses.filter(place_of_life=True).count()
|
269
|
ctx['initial_state'] = ctx['object'].get_initial_state()
|
270
|
ctx['last_rdv'] = get_last_rdv(ctx['object'])
|
271
|
ctx['next_rdv'] = get_next_rdv(ctx['object'])
|
272
|
current_state = ctx['object'].get_current_state()
|
273
|
if current_state.status and STATES_MAPPING.has_key(current_state.status.type):
|
274
|
state = STATES_MAPPING[current_state.status.type]
|
275
|
elif current_state.status:
|
276
|
state = current_state.status.name
|
277
|
else:
|
278
|
state = "Aucun"
|
279
|
ctx['current_state'] = current_state
|
280
|
ctx['status'], ctx['hc_status'] = get_status(ctx, self.request.user)
|
281
|
ctx['missing_policy'] = False
|
282
|
if not self.object.policyholder or \
|
283
|
not self.object.policyholder.health_center or \
|
284
|
not self.object.policyholder.social_security_id:
|
285
|
ctx['missing_policy'] = True
|
286
|
ctx['missing_birthdate'] = False
|
287
|
if not self.object.birthdate:
|
288
|
ctx['missing_birthdate'] = True
|
289
|
return ctx
|
290
|
|
291
|
tab1_general = PatientRecordGeneralView.as_view()
|
292
|
|
293
|
class PatientRecordAdmView(cbv.UpdateView):
|
294
|
model = PatientRecord
|
295
|
form_class = forms.AdministrativeForm
|
296
|
template_name = 'dossiers/patientrecord_tab2_fiche_adm.html'
|
297
|
success_url = './view#tab=1'
|
298
|
|
299
|
def get_context_data(self, **kwargs):
|
300
|
ctx = super(PatientRecordAdmView, self).get_context_data(**kwargs)
|
301
|
try:
|
302
|
ctx['last_prescription'] = TransportPrescriptionLog.objects.filter(patient=ctx['object']).latest('created')
|
303
|
except:
|
304
|
pass
|
305
|
return ctx
|
306
|
|
307
|
tab2_fiche_adm = PatientRecordAdmView.as_view()
|
308
|
|
309
|
class PatientRecordAddrView(cbv.ServiceViewMixin, cbv.MultiUpdateView):
|
310
|
model = PatientRecord
|
311
|
forms_classes = {
|
312
|
'contact': forms.PatientContactForm,
|
313
|
'policyholder': forms.PolicyHolderForm
|
314
|
}
|
315
|
template_name = 'dossiers/patientrecord_tab3_adresses.html'
|
316
|
success_url = './view#tab=2'
|
317
|
|
318
|
|
319
|
def get_context_data(self, **kwargs):
|
320
|
ctx = super(PatientRecordAddrView, self).get_context_data(**kwargs)
|
321
|
ctx['nb_place_of_lifes'] = ctx['object'].addresses.filter(place_of_life=True).count()
|
322
|
ctx['addresses'] = ctx['object'].addresses.order_by('-place_of_life', 'id')
|
323
|
return ctx
|
324
|
|
325
|
tab3_addresses = PatientRecordAddrView.as_view()
|
326
|
|
327
|
class PatientRecordNotifsView(cbv.DetailView):
|
328
|
model = PatientRecord
|
329
|
template_name = 'dossiers/patientrecord_tab4_notifs.html'
|
330
|
|
331
|
def get_context_data(self, **kwargs):
|
332
|
ctx = super(PatientRecordNotifsView, self).get_context_data(**kwargs)
|
333
|
ctx['status'], ctx['hc_status'] = get_status(ctx, self.request.user)
|
334
|
if ctx['object'].service.slug == "cmpp":
|
335
|
(acts_not_locked, days_not_locked, acts_not_valide,
|
336
|
acts_not_billable, acts_pause, acts_per_hc, acts_losts) = \
|
337
|
list_acts_for_billing_CMPP_per_patient(self.object,
|
338
|
datetime.today(), self.service)
|
339
|
ctx['acts_losts'] = acts_losts
|
340
|
ctx['acts_pause'] = acts_pause
|
341
|
hcs_used = acts_per_hc.keys()
|
342
|
hcs = None
|
343
|
if not hcs_used:
|
344
|
hcs = [(hc, None) for hc in HealthCare.objects.filter(patient=self.object).order_by('-start_date')]
|
345
|
else:
|
346
|
hcs = []
|
347
|
for hc in HealthCare.objects.filter(patient=self.object).order_by('-start_date'):
|
348
|
acts = None
|
349
|
if hasattr(hc, 'cmpphealthcarediagnostic') and hc.cmpphealthcarediagnostic in hcs_used:
|
350
|
acts = acts_per_hc[hc.cmpphealthcarediagnostic]
|
351
|
elif hasattr(hc, 'cmpphealthcaretreatment') and hc.cmpphealthcaretreatment in hcs_used:
|
352
|
acts = acts_per_hc[hc.cmpphealthcaretreatment]
|
353
|
hcs.append((hc, acts))
|
354
|
ctx['hcs'] = []
|
355
|
for hc, acts in hcs:
|
356
|
ctx['hcs'].append((hc, acts, hc.act_set.order_by('date', 'time')))
|
357
|
elif ctx['object'].service.slug == "sessad-ted" or ctx['object'].service.slug == "sessad-dys":
|
358
|
ctx['hcs'] = HealthCare.objects.filter(patient=self.object).order_by('-start_date')
|
359
|
return ctx
|
360
|
|
361
|
tab4_notifs = PatientRecordNotifsView.as_view()
|
362
|
|
363
|
class PatientRecordOldActs(cbv.DetailView):
|
364
|
model = PatientRecord
|
365
|
template_name = 'dossiers/patientrecord_tab5_actes_passes.html'
|
366
|
|
367
|
def get_context_data(self, **kwargs):
|
368
|
ctx = super(PatientRecordOldActs, self).get_context_data(**kwargs)
|
369
|
ctx['last_rdvs'] = []
|
370
|
for act in Act.objects.last_acts(ctx['object']).prefetch_related('doctors'):
|
371
|
state = act.get_state()
|
372
|
if state and not state.previous_state and state.state_name == 'NON_VALIDE':
|
373
|
state = None
|
374
|
missing_workers = []
|
375
|
try:
|
376
|
missing_workers = [participant.worker for participant in act.event.get_missing_participants()]
|
377
|
except:
|
378
|
pass
|
379
|
ctx['last_rdvs'].append((act, state, missing_workers))
|
380
|
history = []
|
381
|
i = 0
|
382
|
for state in ctx['object'].filestate_set.order_by('-date_selected'):
|
383
|
acts = []
|
384
|
try:
|
385
|
while ctx['last_rdvs'][i][0].date >= state.date_selected.date():
|
386
|
acts.append(ctx['last_rdvs'][i])
|
387
|
i += 1
|
388
|
except:
|
389
|
pass
|
390
|
history.append((state, acts))
|
391
|
if i < len(ctx['last_rdvs']) - 1:
|
392
|
history.append((None, ctx['last_rdvs'][i:]))
|
393
|
ctx['history'] = history
|
394
|
return ctx
|
395
|
|
396
|
tab5_old_acts = PatientRecordOldActs.as_view()
|
397
|
|
398
|
class PatientRecordNextAppointmentsView(cbv.DetailView):
|
399
|
model = PatientRecord
|
400
|
template_name = 'dossiers/patientrecord_tab6_next_rdv.html'
|
401
|
|
402
|
def get_context_data(self, **kwargs):
|
403
|
ctx = super(PatientRecordNextAppointmentsView, self).get_context_data(**kwargs)
|
404
|
ctx['next_rdvs'] = []
|
405
|
Q = models.Q
|
406
|
today = date.today()
|
407
|
qs = EventWithAct.objects.filter(patient=ctx['object']) \
|
408
|
.filter(exception_to__isnull=True, canceled=False) \
|
409
|
.filter(Q(start_datetime__gte=today) \
|
410
|
| Q(exceptions__isnull=False) \
|
411
|
| ( Q(recurrence_periodicity__isnull=False) \
|
412
|
& (Q(recurrence_end_date__gte=today) \
|
413
|
| Q(recurrence_end_date__isnull=True) \
|
414
|
))) \
|
415
|
.distinct() \
|
416
|
.select_related() \
|
417
|
.prefetch_related('participants', 'exceptions__eventwithact',
|
418
|
'act_set__actvalidationstate_set')
|
419
|
occurrences = []
|
420
|
for event in qs:
|
421
|
occurrences.extend(filter(lambda e: e.start_datetime.date() >= today,
|
422
|
event.all_occurences(limit=180)))
|
423
|
occurrences = sorted(occurrences, key=lambda e: e.start_datetime)
|
424
|
for event in occurrences:
|
425
|
state = None
|
426
|
if event.act:
|
427
|
state = event.act.get_state()
|
428
|
if state and not state.previous_state and state.state_name == 'NON_VALIDE':
|
429
|
state = None
|
430
|
ctx['next_rdvs'].append((event, state, event.get_missing_participants(), event.get_inactive_participants()))
|
431
|
return ctx
|
432
|
|
433
|
tab6_next_rdv = PatientRecordNextAppointmentsView.as_view()
|
434
|
|
435
|
class PatientRecordSocialisationView(cbv.DetailView):
|
436
|
model = PatientRecord
|
437
|
template_name = 'dossiers/patientrecord_tab7_socialisation.html'
|
438
|
|
439
|
tab7_socialisation = PatientRecordSocialisationView.as_view()
|
440
|
|
441
|
class PatientRecordMedicalView(cbv.UpdateView):
|
442
|
model = PatientRecord
|
443
|
form_class = forms.PhysiologyForm
|
444
|
template_name = 'dossiers/patientrecord_tab8_medical.html'
|
445
|
success_url = './view#tab=7'
|
446
|
|
447
|
tab8_medical = PatientRecordMedicalView.as_view()
|
448
|
|
449
|
class PatientRecordsHomepageView(cbv.ListView):
|
450
|
model = PatientRecord
|
451
|
template_name = 'dossiers/index.html'
|
452
|
|
453
|
|
454
|
def _get_search_result(self, paginate_patient_records):
|
455
|
patient_records = []
|
456
|
for patient_record in paginate_patient_records:
|
457
|
next_rdv = get_next_rdv(patient_record)
|
458
|
last_rdv = get_last_rdv(patient_record)
|
459
|
current_status = patient_record.last_state.status
|
460
|
state = current_status.name
|
461
|
state_class = current_status.type.lower()
|
462
|
patient_records.append(
|
463
|
{
|
464
|
'object': patient_record,
|
465
|
'next_rdv': next_rdv,
|
466
|
'last_rdv': last_rdv,
|
467
|
'state': state,
|
468
|
'state_class': state_class
|
469
|
}
|
470
|
)
|
471
|
return patient_records
|
472
|
|
473
|
def get_queryset(self):
|
474
|
first_name = self.request.GET.get('first_name')
|
475
|
last_name = self.request.GET.get('last_name')
|
476
|
paper_id = self.request.GET.get('paper_id')
|
477
|
id = self.request.GET.get('id')
|
478
|
social_security_id = self.request.GET.get('social_security_id')
|
479
|
if not (first_name or last_name or paper_id or id or social_security_id):
|
480
|
return None
|
481
|
if (first_name and len(first_name) < 2) or (last_name and len(last_name) < 2):
|
482
|
return None
|
483
|
qs = super(PatientRecordsHomepageView, self).get_queryset()
|
484
|
states = self.request.GET.getlist('states')
|
485
|
if last_name:
|
486
|
qs = qs.filter(last_name__istartswith=last_name)
|
487
|
if first_name:
|
488
|
qs = qs.filter(first_name__istartswith=first_name)
|
489
|
if paper_id:
|
490
|
qs = qs.filter(paper_id__startswith=paper_id)
|
491
|
if id:
|
492
|
qs = qs.filter(id__startswith=id)
|
493
|
if social_security_id:
|
494
|
qs = qs.filter(models.Q(social_security_id__startswith=social_security_id) | \
|
495
|
models.Q(contacts__social_security_id__startswith=social_security_id))
|
496
|
if states:
|
497
|
qs = qs.filter(last_state__status__id__in=states)
|
498
|
else:
|
499
|
qs = qs.filter(last_state__status__type__in="")
|
500
|
qs = qs.filter(service=self.service).order_by('last_name').\
|
501
|
prefetch_related('last_state',
|
502
|
'patientcontact', 'last_state__status')
|
503
|
return qs
|
504
|
|
505
|
def get_context_data(self, **kwargs):
|
506
|
ctx = super(PatientRecordsHomepageView, self).get_context_data(**kwargs)
|
507
|
ctx['search_form'] = forms.SearchForm(service=self.service, data=self.request.GET or None)
|
508
|
ctx['stats'] = [["Dossiers", 0]]
|
509
|
for status in Status.objects.filter(services=self.service):
|
510
|
ctx['stats'].append([status.name, 0])
|
511
|
|
512
|
page = self.request.GET.get('page')
|
513
|
if ctx['object_list']:
|
514
|
patient_records = ctx['object_list'].filter()
|
515
|
else:
|
516
|
patient_records = []
|
517
|
|
518
|
# TODO: use a sql query to do this
|
519
|
for patient_record in patient_records:
|
520
|
ctx['stats'][0][1] += 1
|
521
|
for elem in ctx['stats']:
|
522
|
if elem[0] == patient_record.last_state.status.name:
|
523
|
elem[1] += 1
|
524
|
paginator = Paginator(patient_records, 50)
|
525
|
try:
|
526
|
paginate_patient_records = paginator.page(page)
|
527
|
except PageNotAnInteger:
|
528
|
paginate_patient_records = paginator.page(1)
|
529
|
except EmptyPage:
|
530
|
paginate_patient_records = paginator.page(paginator.num_pages)
|
531
|
|
532
|
query = self.request.GET.copy()
|
533
|
if 'page' in query:
|
534
|
del query['page']
|
535
|
ctx['query'] = query.urlencode()
|
536
|
|
537
|
ctx['paginate_patient_records'] = paginate_patient_records
|
538
|
ctx['patient_records'] = self._get_search_result(paginate_patient_records)
|
539
|
return ctx
|
540
|
|
541
|
patientrecord_home = PatientRecordsHomepageView.as_view()
|
542
|
|
543
|
class PatientRecordDeleteView(DeleteView):
|
544
|
model = PatientRecord
|
545
|
success_url = ".."
|
546
|
template_name = 'dossiers/patientrecord_confirm_delete.html'
|
547
|
|
548
|
patientrecord_delete = validator_only(PatientRecordDeleteView.as_view())
|
549
|
|
550
|
|
551
|
class PatientRecordPaperIDUpdateView(cbv.UpdateView):
|
552
|
model = PatientRecord
|
553
|
form_class = forms.PaperIDForm
|
554
|
template_name = 'dossiers/generic_form.html'
|
555
|
success_url = '../..'
|
556
|
|
557
|
update_paper_id = PatientRecordPaperIDUpdateView.as_view()
|
558
|
|
559
|
|
560
|
class NewSocialisationDurationView(cbv.CreateView):
|
561
|
model = SocialisationDuration
|
562
|
form_class = forms.SocialisationDurationForm
|
563
|
template_name = 'dossiers/generic_form.html'
|
564
|
success_url = '../view#tab=6'
|
565
|
|
566
|
def get_success_url(self):
|
567
|
return self.success_url
|
568
|
|
569
|
def get(self, request, *args, **kwargs):
|
570
|
if kwargs.has_key('patientrecord_id'):
|
571
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
572
|
return super(NewSocialisationDurationView, self).get(request, *args, **kwargs)
|
573
|
|
574
|
def form_valid(self, form):
|
575
|
duration = form.save()
|
576
|
patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
577
|
patientrecord.socialisation_durations.add(duration)
|
578
|
return HttpResponseRedirect(self.get_success_url())
|
579
|
|
580
|
new_socialisation_duration = NewSocialisationDurationView.as_view()
|
581
|
|
582
|
class UpdateSocialisationDurationView(cbv.UpdateView):
|
583
|
model = SocialisationDuration
|
584
|
form_class = forms.SocialisationDurationForm
|
585
|
template_name = 'dossiers/generic_form.html'
|
586
|
success_url = '../../view#tab=6'
|
587
|
|
588
|
def get(self, request, *args, **kwargs):
|
589
|
if kwargs.has_key('patientrecord_id'):
|
590
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
591
|
return super(UpdateSocialisationDurationView, self).get(request, *args, **kwargs)
|
592
|
|
593
|
update_socialisation_duration = UpdateSocialisationDurationView.as_view()
|
594
|
|
595
|
class DeleteSocialisationDurationView(cbv.DeleteView):
|
596
|
model = SocialisationDuration
|
597
|
form_class = forms.SocialisationDurationForm
|
598
|
template_name = 'dossiers/socialisationduration_confirm_delete.html'
|
599
|
success_url = '../../view#tab=6'
|
600
|
|
601
|
delete_socialisation_duration = DeleteSocialisationDurationView.as_view()
|
602
|
|
603
|
|
604
|
class NewMDPHRequestView(cbv.CreateView):
|
605
|
def get(self, request, *args, **kwargs):
|
606
|
if kwargs.has_key('patientrecord_id'):
|
607
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
608
|
return super(NewMDPHRequestView, self).get(request, *args, **kwargs)
|
609
|
|
610
|
def form_valid(self, form):
|
611
|
request = form.save()
|
612
|
patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
613
|
patientrecord.mdph_requests.add(request)
|
614
|
return HttpResponseRedirect(self.success_url)
|
615
|
|
616
|
class UpdateMDPHRequestView(cbv.UpdateView):
|
617
|
def get(self, request, *args, **kwargs):
|
618
|
if kwargs.has_key('patientrecord_id'):
|
619
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
620
|
return super(UpdateMDPHRequestView, self).get(request, *args, **kwargs)
|
621
|
|
622
|
|
623
|
new_mdph_request = \
|
624
|
NewMDPHRequestView.as_view(model=MDPHRequest,
|
625
|
template_name = 'dossiers/generic_form.html',
|
626
|
success_url = '../view#tab=6',
|
627
|
form_class=forms.MDPHRequestForm)
|
628
|
update_mdph_request = \
|
629
|
UpdateMDPHRequestView.as_view(model=MDPHRequest,
|
630
|
template_name = 'dossiers/generic_form.html',
|
631
|
success_url = '../../view#tab=6',
|
632
|
form_class=forms.MDPHRequestForm)
|
633
|
delete_mdph_request = \
|
634
|
cbv.DeleteView.as_view(model=MDPHRequest,
|
635
|
template_name = 'dossiers/generic_confirm_delete.html',
|
636
|
success_url = '../../view#tab=6')
|
637
|
|
638
|
class NewMDPHResponseView(cbv.CreateView):
|
639
|
def get(self, request, *args, **kwargs):
|
640
|
if kwargs.has_key('patientrecord_id'):
|
641
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
642
|
return super(NewMDPHResponseView, self).get(request, *args, **kwargs)
|
643
|
|
644
|
def form_valid(self, form):
|
645
|
response = form.save()
|
646
|
patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
647
|
patientrecord.mdph_responses.add(response)
|
648
|
return HttpResponseRedirect(self.success_url)
|
649
|
|
650
|
class UpdateMDPHResponseView(cbv.UpdateView):
|
651
|
def get(self, request, *args, **kwargs):
|
652
|
if kwargs.has_key('patientrecord_id'):
|
653
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
654
|
return super(UpdateMDPHResponseView, self).get(request, *args, **kwargs)
|
655
|
|
656
|
|
657
|
new_mdph_response = \
|
658
|
NewMDPHResponseView.as_view(model=MDPHResponse,
|
659
|
template_name = 'dossiers/generic_form.html',
|
660
|
success_url = '../view#tab=6',
|
661
|
form_class=forms.MDPHResponseForm)
|
662
|
update_mdph_response = \
|
663
|
UpdateMDPHResponseView.as_view(model=MDPHResponse,
|
664
|
template_name = 'dossiers/generic_form.html',
|
665
|
success_url = '../../view#tab=6',
|
666
|
form_class=forms.MDPHResponseForm)
|
667
|
delete_mdph_response = \
|
668
|
cbv.DeleteView.as_view(model=MDPHResponse,
|
669
|
template_name = 'dossiers/generic_confirm_delete.html',
|
670
|
success_url = '../../view#tab=6')
|
671
|
|
672
|
|
673
|
class UpdatePatientStateView(cbv.ServiceFormMixin, cbv.UpdateView):
|
674
|
|
675
|
def get_initial(self):
|
676
|
initial = super(UpdatePatientStateView, self).get_initial()
|
677
|
initial['date_selected'] = self.object.date_selected.date()
|
678
|
return initial
|
679
|
|
680
|
def get(self, request, *args, **kwargs):
|
681
|
if kwargs.has_key('patientrecord_id'):
|
682
|
request.session['patientrecord_id'] = kwargs['patientrecord_id']
|
683
|
return super(UpdatePatientStateView, self).get(request, *args, **kwargs)
|
684
|
|
685
|
class DeletePatientView(cbv.DeleteView):
|
686
|
|
687
|
def delete(self, request, *args, **kwargs):
|
688
|
self.object = self.get_object()
|
689
|
if self.object == self.object.patient.last_state:
|
690
|
status = self.object.patient.filestate_set.all().order_by('-created')
|
691
|
if len(status) > 1:
|
692
|
self.object.patient.last_state = status[1]
|
693
|
self.object.patient.save()
|
694
|
else:
|
695
|
# TODO return an error here
|
696
|
return HttpResponseRedirect(self.get_success_url())
|
697
|
self.object.delete()
|
698
|
return HttpResponseRedirect(self.get_success_url())
|
699
|
|
700
|
|
701
|
update_patient_state = \
|
702
|
UpdatePatientStateView.as_view(model=FileState,
|
703
|
template_name = 'dossiers/generic_form.html',
|
704
|
success_url = '../../view#tab=0',
|
705
|
form_class=forms.PatientStateForm)
|
706
|
delete_patient_state = \
|
707
|
DeletePatientView.as_view(model=FileState,
|
708
|
template_name = 'dossiers/generic_confirm_delete.html',
|
709
|
success_url = '../../view#tab=0')
|
710
|
|
711
|
|
712
|
class GenerateRtfFormView(cbv.FormView):
|
713
|
template_name = 'dossiers/generate_rtf_form.html'
|
714
|
form_class = forms.GenerateRtfForm
|
715
|
success_url = './view#tab=0'
|
716
|
|
717
|
def get_context_data(self, **kwargs):
|
718
|
ctx = super(GenerateRtfFormView, self).get_context_data(**kwargs)
|
719
|
ctx['object'] = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
720
|
ctx['service_id'] = self.service.id
|
721
|
if self.request.GET.get('event-id'):
|
722
|
date = self.request.GET.get('date')
|
723
|
date = datetime.strptime(date, '%Y-%m-%d').date()
|
724
|
appointment = Appointment()
|
725
|
event = EventWithAct.objects.get(id=self.request.GET.get('event-id'))
|
726
|
event = event.today_occurrence(date)
|
727
|
appointment.init_from_event(event, self.service)
|
728
|
ctx['event'] = event
|
729
|
ctx['appointment'] = appointment
|
730
|
return ctx
|
731
|
|
732
|
def form_valid(self, form, **kwargs):
|
733
|
ctx = self.get_context_data(**kwargs)
|
734
|
patient = ctx['object']
|
735
|
appointment = ctx['appointment']
|
736
|
event = ctx['event']
|
737
|
template_filename = form.cleaned_data.get('template_filename')
|
738
|
dest_filename = datetime.now().strftime('%Y-%m-%d--%H:%M:%S') + '--' + template_filename
|
739
|
from_path = os.path.join(settings.RTF_TEMPLATES_DIRECTORY, template_filename)
|
740
|
to_path = ''
|
741
|
persistent = True
|
742
|
if settings.USE_PATIENT_FILE_RTF_REPOSITORY_DIRECTORY \
|
743
|
or settings.RTF_REPOSITORY_DIRECTORY:
|
744
|
if settings.USE_PATIENT_FILE_RTF_REPOSITORY_DIRECTORY:
|
745
|
to_path = patient.get_ondisk_directory(self.service.name)
|
746
|
if settings.RTF_REPOSITORY_DIRECTORY:
|
747
|
to_path = os.path.join(to_path,
|
748
|
settings.RTF_REPOSITORY_DIRECTORY)
|
749
|
to_path = os.path.join(to_path, dest_filename)
|
750
|
else:
|
751
|
# else : use temporary files
|
752
|
persistent = False
|
753
|
to_path = dest_filename
|
754
|
variables = {'AD11': '', 'AD12': '', 'AD13': '', 'AD14': '',
|
755
|
'AD15': '', 'AD18': '',
|
756
|
'JOU1': formats.date_format(datetime.today(), "DATE_FORMAT"),
|
757
|
'VIL1': u'Saint-Étienne',
|
758
|
'RDV1': '', 'HEU1': '', 'THE1': '', 'DPA1': '',
|
759
|
'NOM1': '', 'PRE1': '', 'NAI1': '', 'NUM2': '',
|
760
|
'NOM2': '', 'PRE2': '', 'TPA1': '', 'NSS1': '',
|
761
|
'TR01': '', 'TR02': '', 'TR03': '', 'TR04': '', 'TR05': '',
|
762
|
'AD16': '', 'AD17': '', 'AD19': '',
|
763
|
'AD21': '', 'AD22': '', 'AD23': '', 'AD24': '', 'AD25': '',
|
764
|
'AD26': '', 'AD27': '', 'AD28': '', 'AD29': '',
|
765
|
'RDV2': '' ,
|
766
|
}
|
767
|
list_rdvs = []
|
768
|
for act in Act.objects.last_acts(patient):
|
769
|
state = act.get_state()
|
770
|
if state and state.state_name in ('VALIDE', 'ACT_DOUBLE'):
|
771
|
rdv = "\t- %s" % formats.date_format(act.date, "DATE_FORMAT")
|
772
|
if act.time:
|
773
|
rdv += " à %s" % formats.date_format(act.time, "TIME_FORMAT")
|
774
|
list_rdvs.append(rdv)
|
775
|
variables['RDV2'] = '\par'.join(list_rdvs)
|
776
|
if appointment:
|
777
|
variables['RDV1'] = formats.date_format(appointment.date, "DATE_FORMAT")
|
778
|
variables['HEU1'] = appointment.begin_hour
|
779
|
variables['THE1'] = ' '.join([str(i) for i in appointment.workers])# ou DPA1?
|
780
|
variables['DPA1'] = variables['THE1']
|
781
|
if patient:
|
782
|
variables['NOM1'] = patient.last_name
|
783
|
variables['PRE1'] = patient.first_name
|
784
|
if patient.birthdate :
|
785
|
variables['NAI1'] = patient.birthdate.strftime('%d/%m/%Y')
|
786
|
variables['NUM2'] = patient.paper_id
|
787
|
if patient.policyholder:
|
788
|
variables['NOM2'] = patient.policyholder.last_name
|
789
|
variables['PRE2'] = patient.policyholder.first_name
|
790
|
if patient.policyholder.health_center:
|
791
|
variables['TPA1'] = patient.policyholder.health_center.name
|
792
|
if patient.policyholder.social_security_id:
|
793
|
key = str(patient.policyholder.get_control_key())
|
794
|
if len(key) == 1:
|
795
|
key = '0' + key
|
796
|
variables['NSS1'] = \
|
797
|
' '.join([patient.policyholder.social_security_id,
|
798
|
key])
|
799
|
if patient.transportcompany:
|
800
|
variables['TR01'] = patient.transportcompany.name
|
801
|
variables['TR02'] = patient.transportcompany.address
|
802
|
variables['TR03'] = patient.transportcompany.address_complement
|
803
|
variables['TR04'] = patient.transportcompany.zip_code
|
804
|
variables['TR05'] = patient.transportcompany.city
|
805
|
variables['AD18'] = form.cleaned_data.get('phone_address') or ''
|
806
|
for i, line in enumerate(form.cleaned_data.get('address').splitlines()):
|
807
|
variables['AD%d' % (11+i)] = line
|
808
|
if i == 4:
|
809
|
break
|
810
|
|
811
|
filename = make_doc_from_template(from_path, to_path, variables,
|
812
|
persistent)
|
813
|
|
814
|
client_dir = patient.get_client_side_directory(self.service.name)
|
815
|
event.convocation_sent = True
|
816
|
event.save()
|
817
|
if not client_dir:
|
818
|
content = File(file(filename))
|
819
|
response = HttpResponse(content,'text/rtf')
|
820
|
response['Content-Length'] = content.size
|
821
|
response['Content-Disposition'] = 'attachment; filename="%s"' \
|
822
|
% dest_filename.encode('utf-8')
|
823
|
return response
|
824
|
else:
|
825
|
class LocalFileHttpResponseRedirect(HttpResponseRedirect):
|
826
|
allowed_schemes = ['file']
|
827
|
client_filepath = os.path.join(client_dir, dest_filename)
|
828
|
return LocalFileHttpResponseRedirect('file://' + client_filepath)
|
829
|
|
830
|
generate_rtf_form = GenerateRtfFormView.as_view()
|
831
|
|
832
|
|
833
|
class PatientRecordsQuotationsView(cbv.ListView):
|
834
|
model = PatientRecord
|
835
|
template_name = 'dossiers/quotations.html'
|
836
|
|
837
|
def _get_search_result(self, paginate_patient_records):
|
838
|
patient_records = []
|
839
|
for patient_record in paginate_patient_records:
|
840
|
current_state = patient_record.get_current_state() or patient_record.get_state()
|
841
|
state = current_state.status.name
|
842
|
state_class = current_state.status.type.lower()
|
843
|
deficiencies = [getattr(patient_record, field) \
|
844
|
for field in self.deficience_fields]
|
845
|
anap = reduce(lambda f1, f2: f1 or f2, deficiencies)
|
846
|
mises = reduce(lambda m1, m2: m1+m2, [list(getattr(patient_record, field).all()) for field in self.mises_fields])
|
847
|
next_rdv = get_next_rdv(patient_record)
|
848
|
last_rdv = get_last_rdv(patient_record)
|
849
|
|
850
|
if next_rdv:
|
851
|
next_rdv_datetime = next_rdv.start_datetime
|
852
|
else:
|
853
|
next_rdv_datetime = None
|
854
|
if last_rdv:
|
855
|
last_rdv_datetime = last_rdv['start_datetime']
|
856
|
else:
|
857
|
last_rdv_datetime = None
|
858
|
patient_records.append(
|
859
|
{
|
860
|
'object': patient_record,
|
861
|
'state': state,
|
862
|
'state_class': state_class,
|
863
|
'anap': anap,
|
864
|
'mises': mises,
|
865
|
'next_rdv_date': next_rdv_datetime,
|
866
|
'last_rdv_date': last_rdv_datetime
|
867
|
}
|
868
|
)
|
869
|
return patient_records
|
870
|
|
871
|
def get_queryset(self):
|
872
|
form = forms.QuotationsForm(data=self.request.GET or None)
|
873
|
qs = super(PatientRecordsQuotationsView, self).get_queryset()
|
874
|
all_field_names = self.model._meta.get_all_field_names()
|
875
|
self.deficience_fields = [field for field in all_field_names if field.startswith('deficiency_')]
|
876
|
self.mises_fields = [field for field in all_field_names if field.startswith('mises_')]
|
877
|
|
878
|
without_quotations = self.request.GET.get('without_quotations')
|
879
|
without_anap_quotations = self.request.GET.get('without_anap_quotations')
|
880
|
if without_quotations:
|
881
|
for field in self.mises_fields:
|
882
|
mise_field = {'%s__isnull' % field: True}
|
883
|
qs = qs.filter(**mise_field)
|
884
|
|
885
|
if without_anap_quotations:
|
886
|
for field in self.deficience_fields:
|
887
|
anap_field = {field: 0}
|
888
|
qs = qs.filter(**anap_field)
|
889
|
|
890
|
states = self.request.GET.getlist('states')
|
891
|
qs = qs.filter(last_state__status__id__in=states)
|
892
|
|
893
|
try:
|
894
|
date_actes_start = datetime.strptime(form.data['date_actes_start'], "%d/%m/%Y")
|
895
|
qs = qs.filter(act__date__gte=date_actes_start.date()).distinct()
|
896
|
except (ValueError, KeyError):
|
897
|
pass
|
898
|
try:
|
899
|
date_actes_end = datetime.strptime(form.data['date_actes_end'], "%d/%m/%Y")
|
900
|
qs = qs.filter(act__date__lte=date_actes_end.date()).distinct()
|
901
|
except (ValueError, KeyError):
|
902
|
pass
|
903
|
qs = qs.filter(service=self.service).order_by('last_name').prefetch_related()
|
904
|
return qs
|
905
|
|
906
|
def get_context_data(self, **kwargs):
|
907
|
ctx = super(PatientRecordsQuotationsView, self).get_context_data(**kwargs)
|
908
|
ctx['search_form'] = forms.QuotationsForm(data=self.request.GET or None,
|
909
|
service=self.service)
|
910
|
patient_records = []
|
911
|
page = self.request.GET.get('page')
|
912
|
all = 'all' in self.request.GET
|
913
|
if all:
|
914
|
patient_records = ctx['object_list']
|
915
|
ctx['all'] = all
|
916
|
self.template_name = 'dossiers/quotations_print.html'
|
917
|
else:
|
918
|
paginator = Paginator(ctx['object_list'].filter(), 50)
|
919
|
try:
|
920
|
patient_records = paginator.page(page)
|
921
|
except PageNotAnInteger:
|
922
|
patient_records = paginator.page(1)
|
923
|
except EmptyPage:
|
924
|
patient_records = paginator.page(paginator.num_pages)
|
925
|
ctx['paginate_patient_records'] = patient_records
|
926
|
|
927
|
ctx['patient_records'] = self._get_search_result(patient_records)
|
928
|
|
929
|
query = self.request.GET.copy()
|
930
|
if 'page' in query:
|
931
|
del query['page']
|
932
|
ctx['query'] = query.urlencode()
|
933
|
|
934
|
return ctx
|
935
|
|
936
|
patientrecord_quotations = PatientRecordsQuotationsView.as_view()
|
937
|
|
938
|
class NewProtectionStateView(cbv.CreateView):
|
939
|
model = ProtectionState
|
940
|
template_name = 'dossiers/generic_form.html'
|
941
|
success_url = '../view#tab=1'
|
942
|
form_class = forms.ProtectionStateForm
|
943
|
|
944
|
def form_valid(self, form):
|
945
|
self.patient = get_object_or_404(PatientRecord, id=self.kwargs.get('patientrecord_id',None))
|
946
|
form.instance.patient = self.patient
|
947
|
return super(NewProtectionStateView, self).form_valid(form)
|
948
|
|
949
|
new_protection = NewProtectionStateView.as_view()
|
950
|
|
951
|
class UpdateProtectionStateView(cbv.UpdateView):
|
952
|
model = ProtectionState
|
953
|
template_name = 'dossiers/generic_form.html'
|
954
|
success_url = '../../view#tab=1'
|
955
|
form_class = forms.ProtectionStateForm
|
956
|
|
957
|
def form_valid(self, form):
|
958
|
self.patient = get_object_or_404(PatientRecord, id=self.kwargs.get('patientrecord_id',None))
|
959
|
form.instance.patient = self.patient
|
960
|
return super(UpdateProtectionStateView, self).form_valid(form)
|
961
|
|
962
|
update_protection = UpdateProtectionStateView.as_view()
|
963
|
|
964
|
class DeleteProtectionStateView(cbv.DeleteView):
|
965
|
model = ProtectionState
|
966
|
template_name = 'dossiers/protection_confirm_delete.html'
|
967
|
success_url = '../../view#tab=1'
|
968
|
|
969
|
delete_protection = DeleteProtectionStateView.as_view()
|
970
|
|
971
|
class PatientRecordsWaitingQueueView(cbv.ListView):
|
972
|
model = PatientRecord
|
973
|
template_name = 'dossiers/waiting_queue.html'
|
974
|
|
975
|
def _get_search_result(self, paginate_patient_records,
|
976
|
all_patient_records):
|
977
|
patient_records = []
|
978
|
if paginate_patient_records:
|
979
|
position = 1
|
980
|
for patient_record in paginate_patient_records:
|
981
|
while patient_record.id != all_patient_records[position - 1].id:
|
982
|
position += 1
|
983
|
patient_records.append(
|
984
|
{
|
985
|
'object': patient_record,
|
986
|
'position': position,
|
987
|
}
|
988
|
)
|
989
|
return patient_records
|
990
|
|
991
|
def get_queryset(self):
|
992
|
form = forms.QuotationsForm(data=self.request.GET or None)
|
993
|
qs = super(PatientRecordsWaitingQueueView, self).get_queryset()
|
994
|
first_name = self.request.GET.get('first_name')
|
995
|
last_name = self.request.GET.get('last_name')
|
996
|
paper_id = self.request.GET.get('paper_id')
|
997
|
id = self.request.GET.get('id')
|
998
|
social_security_id = self.request.GET.get('social_security_id')
|
999
|
qs = qs.filter(service=self.service,
|
1000
|
last_state__status__type='ACCUEIL')
|
1001
|
if last_name:
|
1002
|
qs = qs.filter(last_name__istartswith=last_name)
|
1003
|
if first_name:
|
1004
|
qs = qs.filter(first_name__istartswith=first_name)
|
1005
|
if paper_id:
|
1006
|
qs = qs.filter(paper_id__startswith=paper_id)
|
1007
|
if id:
|
1008
|
qs = qs.filter(id__startswith=id)
|
1009
|
if social_security_id:
|
1010
|
qs = qs.filter(models.Q(
|
1011
|
social_security_id__startswith=social_security_id)
|
1012
|
| models.Q(
|
1013
|
contacts__social_security_id__startswith=social_security_id))
|
1014
|
qs = qs.order_by('last_state__date_selected', 'created')
|
1015
|
return qs
|
1016
|
|
1017
|
def get_context_data(self, **kwargs):
|
1018
|
ctx = super(PatientRecordsWaitingQueueView, self).get_context_data(**kwargs)
|
1019
|
ctx['search_form'] = forms.QuotationsForm(data=self.request.GET or None,
|
1020
|
service=self.service)
|
1021
|
patient_records = []
|
1022
|
page = self.request.GET.get('page')
|
1023
|
paginator = Paginator(ctx['object_list'].filter(), 50)
|
1024
|
try:
|
1025
|
paginate_patient_records = paginator.page(page)
|
1026
|
except PageNotAnInteger:
|
1027
|
paginate_patient_records = paginator.page(1)
|
1028
|
except EmptyPage:
|
1029
|
paginate_patient_records = paginator.page(paginator.num_pages)
|
1030
|
|
1031
|
all_patient_records = PatientRecord.objects.filter(
|
1032
|
service=self.service,
|
1033
|
last_state__status__type='ACCUEIL').order_by(
|
1034
|
'last_state__date_selected', 'created')
|
1035
|
ctx['patient_records'] = self._get_search_result(
|
1036
|
paginate_patient_records, all_patient_records)
|
1037
|
ctx['paginate_patient_records'] = paginate_patient_records
|
1038
|
ctx['len_patient_records'] = all_patient_records.count()
|
1039
|
|
1040
|
query = self.request.GET.copy()
|
1041
|
if 'page' in query:
|
1042
|
del query['page']
|
1043
|
ctx['query'] = query.urlencode()
|
1044
|
|
1045
|
return ctx
|
1046
|
|
1047
|
patientrecord_waiting_queue = PatientRecordsWaitingQueueView.as_view()
|
1048
|
|
1049
|
class CreateDirectoryView(View, cbv.ServiceViewMixin):
|
1050
|
def post(self, request, *args, **kwargs):
|
1051
|
patient = PatientRecord.objects.get(id=kwargs['patientrecord_id'])
|
1052
|
service = Service.objects.get(slug=kwargs['service'])
|
1053
|
patient.get_ondisk_directory(service.name)
|
1054
|
messages.add_message(self.request, messages.INFO, u'Répertoire patient créé.')
|
1055
|
return HttpResponseRedirect('view')
|
1056
|
|
1057
|
create_directory = CreateDirectoryView.as_view()
|
1058
|
|
1059
|
class GenerateTransportPrescriptionFormView(cbv.FormView):
|
1060
|
template_name = 'dossiers/generate_transport_prescription_form.html'
|
1061
|
form_class = Form
|
1062
|
success_url = './view#tab=1'
|
1063
|
|
1064
|
def get_context_data(self, **kwargs):
|
1065
|
ctx = super(GenerateTransportPrescriptionFormView, self).get_context_data(**kwargs)
|
1066
|
ctx['lieu'] = 'Saint-Etienne'
|
1067
|
ctx['date'] = formats.date_format(datetime.today(), "SHORT_DATE_FORMAT")
|
1068
|
ctx['id_etab'] = '''%s SAINT ETIENNE
|
1069
|
66/68, RUE MARENGO
|
1070
|
42000 SAINT ETIENNE''' % ctx['service'].upper()
|
1071
|
try:
|
1072
|
patient = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
1073
|
ctx['object'] = patient
|
1074
|
last_log = TransportPrescriptionLog.objects.filter(patient=patient).latest('created')
|
1075
|
if last_log:
|
1076
|
ctx['choices'] = last_log.get_choices()
|
1077
|
if 'lieu' in ctx['choices'] and ctx['choices']['lieu']:
|
1078
|
ctx['lieu'] = ctx['choices']['lieu']
|
1079
|
if 'date' in ctx['choices'] and ctx['choices']['date']:
|
1080
|
ctx['date'] = ctx['choices']['date']
|
1081
|
if 'id_etab' in ctx['choices'] and ctx['choices']['id_etab']:
|
1082
|
ctx['id_etab'] = ctx['choices']['id_etab']
|
1083
|
except:
|
1084
|
pass
|
1085
|
return ctx
|
1086
|
|
1087
|
def form_valid(self, form):
|
1088
|
patient = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
|
1089
|
address = PatientAddress.objects.get(id=form.data['address_id'])
|
1090
|
path = render_transport(patient, address, form.data)
|
1091
|
content = File(file(path))
|
1092
|
log = TransportPrescriptionLog(patient=patient)
|
1093
|
log.set_choices(form.data)
|
1094
|
log.save()
|
1095
|
response = HttpResponse(content,'application/pdf')
|
1096
|
response['Content-Length'] = content.size
|
1097
|
dest_filename = "%s--prescription-transport-%s-%s.pdf" \
|
1098
|
% (datetime.now().strftime('%Y-%m-%d--%H:%M:%S'),
|
1099
|
patient.last_name.upper().encode('utf-8'),
|
1100
|
patient.first_name.encode('utf-8'))
|
1101
|
response['Content-Disposition'] = \
|
1102
|
'attachment; filename="%s"' % dest_filename
|
1103
|
return response
|
1104
|
|
1105
|
prescription_transport = GenerateTransportPrescriptionFormView.as_view()
|