Project

General

Profile

Download (16.1 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / dossiers / views.py @ d694f19f

1
from datetime import datetime
2

    
3
from django.core.urlresolvers import reverse_lazy
4
from django.http import HttpResponseRedirect
5
from django.views.generic.edit import DeleteView, FormMixin
6

    
7
from calebasse import cbv
8
from calebasse.dossiers import forms
9
from calebasse.agenda.models import Occurrence
10
from calebasse.dossiers.models import (PatientRecord, PatientContact,
11
        PatientAddress, Status, FileState, create_patient, CmppHealthCareTreatment,
12
        CmppHealthCareDiagnostic, SessadHealthCareNotification, HealthCare)
13
from calebasse.dossiers.states import STATES_MAPPING, STATE_CHOICES_TYPE, STATES_BTN_MAPPER
14
from calebasse.ressources.models import Service
15

    
16

    
17
def get_next_rdv(patient_record):
18
    next_rdv = {}
19
    occurrence = Occurrence.objects.next_appoinment(patient_record)
20
    if occurrence:
21
        next_rdv['start_datetime'] = occurrence.start_time
22
        next_rdv['participants'] = occurrence.event.participants.all()
23
        next_rdv['act_type'] = occurrence.event.eventact.act_type
24
    return next_rdv
25

    
26
def get_last_rdv(patient_record):
27
    last_rdv = {}
28
    occurrence = Occurrence.objects.last_appoinment(patient_record)
29
    if occurrence:
30
        last_rdv['start_datetime'] = occurrence.start_time
31
        last_rdv['participants'] = occurrence.event.participants.all()
32
        last_rdv['act_type'] = occurrence.event.eventact.act_type
33
    return last_rdv
34

    
35
class NewPatientRecordView(cbv.FormView, cbv.ServiceViewMixin):
36
    form_class = forms.NewPatientRecordForm
37
    template_name = 'dossiers/patientrecord_new.html'
38
    success_url = '..'
39

    
40
    def post(self, request, *args, **kwarg):
41
        self.user = request.user
42
        return super(NewPatientRecordView, self).post(request, *args, **kwarg)
43

    
44
    def form_valid(self, form):
45
        self.patient = create_patient(form.data['first_name'], form.data['last_name'], self.service,
46
                self.user, date_selected=datetime.strptime(form.data['date_selected'], "%d/%m/%Y"))
47
        return super(NewPatientRecordView, self).form_valid(form)
48

    
49
new_patient_record = NewPatientRecordView.as_view()
50

    
51
class NewPatientContactView(cbv.CreateView):
52
    model = PatientContact
53
    form_class = forms.PatientContactForm
54
    template_name = 'dossiers/patientcontact_new.html'
55
    success_url = '../view#tab=2'
56

    
57
    def get(self, request, *args, **kwargs):
58
        if kwargs.has_key('patientrecord_id'):
59
            request.session['patientrecord_id'] = kwargs['patientrecord_id']
60
        return super(NewPatientContactView, self).get(request, *args, **kwargs)
61

    
62
new_patient_contact = NewPatientContactView.as_view()
63

    
64
class UpdatePatientContactView(cbv.UpdateView):
65
    model = PatientContact
66
    form_class = forms.PatientContactForm
67
    template_name = 'dossiers/patientcontact_new.html'
68
    success_url = '../../view#tab=2'
69

    
70
    def get(self, request, *args, **kwargs):
71
        if kwargs.has_key('patientrecord_id'):
72
            request.session['patientrecord_id'] = kwargs['patientrecord_id']
73
        return super(UpdatePatientContactView, self).get(request, *args, **kwargs)
74

    
75
update_patient_contact = UpdatePatientContactView.as_view()
76

    
77
class DeletePatientContactView(cbv.DeleteView):
78
    model = PatientContact
79
    form_class = forms.PatientContactForm
80
    template_name = 'dossiers/patientcontact_confirm_delete.html'
81
    success_url = '../../view#tab=2'
82

    
83
delete_patient_contact = DeletePatientContactView.as_view()
84

    
85
class NewPatientAddressView(cbv.CreateView):
86
    model = PatientAddress
87
    form_class = forms.PatientAddressForm
88
    template_name = 'dossiers/patientaddress_new.html'
89
    success_url = '../view#tab=2'
90

    
91
    def get_success_url(self):
92
        return self.success_url
93

    
94
    def form_valid(self, form):
95
        patientaddress = form.save()
96
        patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
97
        patientrecord.addresses.add(patientaddress)
98
        return HttpResponseRedirect(self.get_success_url())
99

    
100
new_patient_address = NewPatientAddressView.as_view()
101

    
102
class UpdatePatientAddressView(cbv.UpdateView):
103
    model = PatientAddress
104
    form_class = forms.PatientAddressForm
105
    template_name = 'dossiers/patientaddress_new.html'
106
    success_url = '../../view#tab=2'
107

    
108
update_patient_address = UpdatePatientAddressView.as_view()
109

    
110
class DeletePatientAddressView(cbv.DeleteView):
111
    model = PatientAddress
112
    form_class = forms.PatientAddressForm
113
    template_name = 'dossiers/patientaddress_confirm_delete.html'
114
    success_url = '../../view#tab=2'
115

    
116
delete_patient_address = DeletePatientAddressView.as_view()
117

    
118
class NewHealthCareView(cbv.CreateView):
119
    template_name = 'dossiers/generic_form.html'
120
    success_url = '../view#tab=3'
121

    
122
    def get_success_url(self):
123
        return self.success_url
124

    
125
    def get_initial(self):
126
        initial = super(NewHealthCareView, self).get_initial()
127
        initial['author'] = self.request.user.id
128
        initial['patient'] = self.kwargs['patientrecord_id']
129
        return initial
130

    
131

    
132
new_healthcare_treatment = NewHealthCareView.as_view(model=CmppHealthCareTreatment,
133
        form_class=forms.CmppHealthCareTreatmentForm)
134
new_healthcare_diagnostic = NewHealthCareView.as_view(model=CmppHealthCareDiagnostic,
135
        form_class=forms.CmppHealthCareDiagnosticForm)
136
new_healthcare_notification = NewHealthCareView.as_view(model=SessadHealthCareNotification,
137
        form_class=forms.SessadHealthCareNotificationForm)
138

    
139

    
140
class StateFormView(cbv.FormView):
141
    template_name = 'dossiers/state.html'
142
    form_class = forms.StateForm
143
    success_url = './view#tab=0'
144

    
145
    def post(self, request, *args, **kwarg):
146
        self.user = request.user
147
        return super(StateFormView, self).post(request, *args, **kwarg)
148

    
149
    def form_valid(self, form):
150
        service = Service.objects.get(id=form.data['service_id'])
151
        status = Status.objects.filter(services=service).filter(type=form.data['state_type'])
152
        patient = PatientRecord.objects.get(id=form.data['patient_id'])
153
        date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
154
        patient.set_state(status[0], self.user, date_selected, form.data['comment'])
155
        return super(StateFormView, self).form_valid(form)
156

    
157
state_form = StateFormView.as_view()
158

    
159

    
160
class PatientRecordView(cbv.ServiceViewMixin, cbv.MultiUpdateView):
161
    model = PatientRecord
162
    forms_classes = {
163
            'general': forms.GeneralForm,
164
            'id': forms.CivilStatusForm,
165
            'physiology': forms.PhysiologyForm,
166
            'inscription': forms.InscriptionForm,
167
            'family': forms.FamilyForm,
168
            'transport': forms.TransportFrom,
169
            'followup': forms.FollowUpForm,
170
            'policyholder': forms.PolicyHolderForm
171
            }
172
    template_name = 'dossiers/patientrecord_update.html'
173
    success_url = './view'
174

    
175
    def get_success_url(self):
176
        if self.request.POST.has_key('tab'):
177
            return self.success_url + '#tab=' + self.request.POST['tab']
178
        else:
179
            return self.success_url
180

    
181
    def get_context_data(self, **kwargs):
182
        ctx = super(PatientRecordView, self).get_context_data(**kwargs)
183
        ctx['next_rdv'] = get_next_rdv(ctx['object'])
184
        ctx['last_rdv'] = get_last_rdv(ctx['object'])
185
        ctx['initial_state'] = ctx['object'].get_initial_state()
186
        current_state = ctx['object'].get_current_state()
187
        if STATES_MAPPING.has_key(current_state.status.type):
188
            state = STATES_MAPPING[current_state.status.type]
189
        else:
190
            state = current_state.status.name
191
        ctx['current_state'] = current_state
192
        ctx['service_id'] = self.service.id
193
        ctx['states'] = FileState.objects.filter(patient=self.object).filter(status__services=self.service)
194
        ctx['next_rdvs'] = []
195
        for rdv in Occurrence.objects.next_appoinments(ctx['object']):
196
            state = rdv.event.eventact.get_state()
197
            if not state.previous_state:
198
                state = None
199
            ctx['next_rdvs'].append((rdv, state))
200
        ctx['last_rdvs'] = []
201
        for rdv in Occurrence.objects.last_appoinments(ctx['object']):
202
            state = rdv.event.eventact.get_state()
203
            if not state.previous_state:
204
                state = None
205
            ctx['last_rdvs'].append((rdv, state))
206
        ctx['status'] = []
207
        if ctx['object'].service.name == "CMPP":
208
            if ctx['object'].last_state.status.type == "ACCUEIL":
209
                # Insciption automatique au premier acte facturable valide
210
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
211
                        STATES_BTN_MAPPER['DIAGNOSTIC'],
212
                        STATES_BTN_MAPPER['TRAITEMENT']]
213
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
214
                # Passage automatique en diagnostic ou traitement
215
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
216
                        STATES_BTN_MAPPER['DIAGNOSTIC'],
217
                        STATES_BTN_MAPPER['TRAITEMENT']]
218
            elif ctx['object'].last_state.status.type == "DIAGNOSTIC":
219
                # Passage automatique en traitement
220
                ctx['status'] = [STATES_BTN_MAPPER['TRAITEMENT'],
221
                        STATES_BTN_MAPPER['CLOS'],
222
                        STATES_BTN_MAPPER['ACCUEIL']]
223
            elif ctx['object'].last_state.status.type == "TRAITEMENT":
224
                # Passage automatique en diagnostic si on ajoute une prise en charge diagnostic,
225
                # ce qui est faisable dans l'onglet prise en charge par un bouton visible sous conditions
226
                ctx['status'] = [STATES_BTN_MAPPER['DIAGNOSTIC'],
227
                        STATES_BTN_MAPPER['CLOS'],
228
                        STATES_BTN_MAPPER['ACCUEIL']]
229
            elif ctx['object'].last_state.status.type == "CLOS":
230
                # Passage automatique en diagnostic ou traitement
231
                ctx['status'] = [STATES_BTN_MAPPER['DIAGNOSTIC'],
232
                        STATES_BTN_MAPPER['TRAITEMENT'],
233
                        STATES_BTN_MAPPER['ACCUEIL']]
234
        elif ctx['object'].service.name == "CAMSP":
235
            if ctx['object'].last_state.status.type == "ACCUEIL":
236
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
237
                        STATES_BTN_MAPPER['BILAN']]
238
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
239
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
240
                        STATES_BTN_MAPPER['BILAN'],
241
                        STATES_BTN_MAPPER['SURVEILLANCE'],
242
                        STATES_BTN_MAPPER['SUIVI'],
243
                        STATES_BTN_MAPPER['CLOS']]
244
            elif ctx['object'].last_state.status.type == "BILAN":
245
                ctx['status'] = [STATES_BTN_MAPPER['SURVEILLANCE'],
246
                        STATES_BTN_MAPPER['SUIVI'],
247
                        STATES_BTN_MAPPER['CLOS'],
248
                        STATES_BTN_MAPPER['ACCUEIL']]
249
            elif ctx['object'].last_state.status.type == "SURVEILLANCE":
250
                ctx['status'] = [STATES_BTN_MAPPER['SUIVI'],
251
                        STATES_BTN_MAPPER['CLOS'],
252
                        STATES_BTN_MAPPER['ACCUEIL'],
253
                        STATES_BTN_MAPPER['BILAN']]
254
            elif ctx['object'].last_state.status.type == "SUIVI":
255
                ctx['status'] = [STATES_BTN_MAPPER['CLOS'],
256
                        STATES_BTN_MAPPER['ACCUEIL'],
257
                        STATES_BTN_MAPPER['BILAN'],
258
                        STATES_BTN_MAPPER['SURVEILLANCE']]
259
            elif ctx['object'].last_state.status.type == "CLOS":
260
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
261
                        STATES_BTN_MAPPER['BILAN'],
262
                        STATES_BTN_MAPPER['SURVEILLANCE'],
263
                        STATES_BTN_MAPPER['SUIVI']]
264
        elif ctx['object'].service.name == "SESSAD TED" or ctx['object'].service.name == "SESSAD DYS":
265
            if ctx['object'].last_state.status.type == "ACCUEIL":
266
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
267
                        STATES_BTN_MAPPER['TRAITEMENT']]
268
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
269
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
270
                        STATES_BTN_MAPPER['TRAITEMENT'],
271
                        STATES_BTN_MAPPER['CLOS']]
272
            elif ctx['object'].last_state.status.type == "TRAITEMENT":
273
                ctx['status'] = [STATES_BTN_MAPPER['CLOS'],
274
                        STATES_BTN_MAPPER['ACCUEIL']]
275
            elif ctx['object'].last_state.status.type == "CLOS":
276
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
277
                        STATES_BTN_MAPPER['TRAITEMENT']]
278
        ctx['can_rediag'] = self.object.create_diag_healthcare(self.request.user)
279
        ctx['hcs'] = HealthCare.objects.filter(patient=self.object).order_by('-start_date')
280
        return ctx
281

    
282
patient_record = PatientRecordView.as_view()
283

    
284
class PatientRecordsHomepageView(cbv.ListView):
285
    model = PatientRecord
286
    template_name = 'dossiers/index.html'
287

    
288
    def get_queryset(self):
289
        qs = super(PatientRecordsHomepageView, self).get_queryset()
290
        first_name = self.request.GET.get('first_name')
291
        last_name = self.request.GET.get('last_name')
292
        paper_id = self.request.GET.get('paper_id')
293
        id = self.request.GET.get('id')
294
        states = self.request.GET.getlist('states')
295
        social_security_id = self.request.GET.get('social_security_id')
296
        if last_name:
297
            qs = qs.filter(last_name__icontains=last_name)
298
        if first_name:
299
            qs = qs.filter(first_name__icontains=first_name)
300
        if paper_id:
301
            qs = qs.filter(paper_id__contains=paper_id)
302
        if id:
303
            qs = qs.filter(id__contains=id)
304
        if social_security_id:
305
            qs = qs.filter(social_security_id__contains=social_security_id)
306
        if states:
307
            status_types = ['BILAN', 'SURVEILLANCE', 'SUIVI']
308
            for state in states:
309
                status_types.append(STATE_CHOICES_TYPE[state])
310
            qs = qs.filter(last_state__status__type__in=status_types)
311
        else:
312
            qs = qs.filter(last_state__status__type__in="")
313
        qs = qs.filter(service=self.service).order_by('last_name')
314
        return qs
315

    
316
    def get_context_data(self, **kwargs):
317
        ctx = super(PatientRecordsHomepageView, self).get_context_data(**kwargs)
318
        ctx['search_form'] = forms.SearchForm(data=self.request.GET or None)
319
        ctx['patient_records'] = []
320
        ctx['stats'] = {"dossiers": 0,
321
                "En_contact": 0,
322
                "Fin_daccueil": 0,
323
                "Diagnostic": 0,
324
                "Traitement": 0,
325
                "Clos": 0}
326
        if self.request.GET:
327
            for patient_record in ctx['object_list'].filter():
328
                ctx['stats']['dossiers'] += 1
329
                next_rdv = get_next_rdv(patient_record)
330
                last_rdv = get_last_rdv(patient_record)
331
                current_state = patient_record.get_current_state()
332
                if STATES_MAPPING.has_key(current_state.status.type):
333
                    state = STATES_MAPPING[current_state.status.type]
334
                else:
335
                    state = current_state.status.name
336
                state_class = current_state.status.type.lower()
337
                ctx['patient_records'].append(
338
                        {
339
                            'object': patient_record,
340
                            'next_rdv': next_rdv,
341
                            'last_rdv': last_rdv,
342
                            'state': state,
343
                            'state_class': state_class
344
                            }
345
                        )
346
                state = state.replace(' ', '_')
347
                state = state.replace("'", '')
348
                if not ctx['stats'].has_key(state):
349
                    ctx['stats'][state] = 0
350
                ctx['stats'][state] += 1
351

    
352
        return ctx
353

    
354
patientrecord_home = PatientRecordsHomepageView.as_view()
355

    
356
class PatientRecordDeleteView(DeleteView):
357
    model = PatientRecord
358
    success_url = ".."
359
    template_name = 'dossiers/patientrecord_confirm_delete.html'
360

    
361
patientrecord_delete = PatientRecordDeleteView.as_view()
362

    
363

    
364
class PatientRecordPaperIDUpdateView(cbv.UpdateView):
365
    model = PatientRecord
366
    form_class = forms.PaperIDForm
367
    template_name = 'dossiers/generic_form.html'
368
    success_url = '../..'
369

    
370
update_paper_id = PatientRecordPaperIDUpdateView.as_view()
(9-9/9)