Project

General

Profile

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

calebasse / calebasse / dossiers / views.py @ dfd4bd12

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)
12
from calebasse.dossiers.states import STATES_MAPPING, STATE_CHOICES_TYPE, STATES_BTN_MAPPER
13
from calebasse.ressources.models import Service
14

    
15

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

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

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

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

    
43
    def form_valid(self, form):
44
        create_patient(form.data['first_name'], form.data['last_name'], self.service,
45
                self.user)
46
        return super(NewPatientRecordView, self).form_valid(form)
47

    
48
new_patient_record = NewPatientRecordView.as_view()
49

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

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

    
61
new_patient_contact = NewPatientContactView.as_view()
62

    
63
class DeletePatientContactView(cbv.DeleteView):
64
    model = PatientContact
65
    form_class = forms.PatientContactForm
66
    template_name = 'dossiers/patientcontact_confirm_delete.html'
67
    success_url = '../../view#tab=2'
68

    
69
delete_patient_contact = DeletePatientContactView.as_view()
70

    
71
class NewPatientAddressView(cbv.CreateView):
72
    model = PatientAddress
73
    form_class = forms.PatientAddressForm
74
    template_name = 'dossiers/patientaddress_new.html'
75
    success_url = '../view#tab=2'
76

    
77
    def get_success_url(self):
78
        return self.success_url
79

    
80
    def form_valid(self, form):
81
        patientaddress = form.save()
82
        patientrecord = PatientRecord.objects.get(id=self.kwargs['patientrecord_id'])
83
        patientrecord.addresses.add(patientaddress)
84
        return HttpResponseRedirect(self.get_success_url())
85

    
86
new_patient_address = NewPatientAddressView.as_view()
87

    
88
class DeletePatientAddressView(cbv.DeleteView):
89
    model = PatientAddress
90
    form_class = forms.PatientAddressForm
91
    template_name = 'dossiers/patientaddress_confirm_delete.html'
92
    success_url = '../../view#tab=2'
93

    
94
delete_patient_address = DeletePatientAddressView.as_view()
95

    
96
class StateFormView(cbv.FormView):
97
    template_name = 'dossiers/state.html'
98
    form_class = forms.StateForm
99
    success_url = './view#tab=0'
100

    
101
    def post(self, request, *args, **kwarg):
102
        self.user = request.user
103
        return super(StateFormView, self).post(request, *args, **kwarg)
104

    
105
    def form_valid(self, form):
106
        service = Service.objects.get(id=form.data['service_id'])
107
        status = Status.objects.filter(services=service).filter(type=form.data['state_type'])
108
        patient = PatientRecord.objects.get(id=form.data['patient_id'])
109
        date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
110
        patient.set_state(status[0], self.user, date_selected, form.data['comment'])
111
        return super(StateFormView, self).form_valid(form)
112

    
113
state_form = StateFormView.as_view()
114

    
115

    
116
class PatientRecordView(cbv.ServiceViewMixin, cbv.MultiUpdateView):
117
    model = PatientRecord
118
    forms_classes = {
119
            'general': forms.GeneralForm,
120
            'id': forms.CivilStatusForm,
121
            'physiology': forms.PhysiologyForm,
122
            'inscription': forms.InscriptionForm,
123
            'family': forms.FamilyForm,
124
            'transport': forms.TransportFrom,
125
            'followup': forms.FollowUpForm
126
            }
127
    template_name = 'dossiers/patientrecord_update.html'
128
    success_url = './view'
129

    
130
    def get_success_url(self):
131
        if self.request.POST.has_key('tab'):
132
            return self.success_url + '#tab=' + self.request.POST['tab']
133
        else:
134
            return self.success_url
135

    
136
    def get_context_data(self, **kwargs):
137
        ctx = super(PatientRecordView, self).get_context_data(**kwargs)
138
        ctx['next_rdv'] = get_next_rdv(ctx['object'])
139
        ctx['last_rdv'] = get_last_rdv(ctx['object'])
140
        current_state = ctx['object'].get_current_state()
141
        if STATES_MAPPING.has_key(current_state.status.type):
142
            state = STATES_MAPPING[current_state.status.type]
143
        else:
144
            state = current_state.status.name
145
        ctx['current_state'] = current_state
146
        ctx['service_id'] = self.service.id
147
        ctx['states'] = FileState.objects.filter(patient=self.object).filter(status__services=self.service)
148
        ctx['status'] = []
149
        if ctx['object'].service.name == "CMPP":
150
            if ctx['object'].last_state.status.type == "ACCUEIL":
151
                # Insciption automatique au premier acte facturable valide
152
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
153
                        STATES_BTN_MAPPER['DIAGNOSTIC'],
154
                        STATES_BTN_MAPPER['TRAITEMENT']]
155
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
156
                # Passage automatique en diagnostic ou traitement
157
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
158
                        STATES_BTN_MAPPER['DIAGNOSTIC'],
159
                        STATES_BTN_MAPPER['TRAITEMENT']]
160
            elif ctx['object'].last_state.status.type == "DIAGNOSTIC":
161
                # Passage automatique en traitement
162
                ctx['status'] = [STATES_BTN_MAPPER['TRAITEMENT'],
163
                        STATES_BTN_MAPPER['CLOS'],
164
                        STATES_BTN_MAPPER['ACCUEIL']]
165
            elif ctx['object'].last_state.status.type == "TRAITEMENT":
166
                # Passage automatique en diagnostic si on ajoute une prise en charge diagnostic, 
167
                # ce qui est faisable dans l'onglet prise en charge par un bouton visible sous conditions
168
                ctx['status'] = [STATES_BTN_MAPPER['DIAGNOSTIC'],
169
                        STATES_BTN_MAPPER['CLOS'],
170
                        STATES_BTN_MAPPER['ACCUEIL']]
171
            elif ctx['object'].last_state.status.type == "CLOS":
172
                # Passage automatique en diagnostic ou traitement
173
                ctx['status'] = [STATES_BTN_MAPPER['DIAGNOSTIC'],
174
                        STATES_BTN_MAPPER['TRAITEMENT'],
175
                        STATES_BTN_MAPPER['ACCUEIL']]
176
        elif ctx['object'].service.name == "CAMSP":
177
            if ctx['object'].last_state.status.type == "ACCUEIL":
178
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
179
                        STATES_BTN_MAPPER['BILAN']]
180
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
181
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
182
                        STATES_BTN_MAPPER['BILAN'],
183
                        STATES_BTN_MAPPER['SURVEILLANCE'],
184
                        STATES_BTN_MAPPER['SUIVI'],
185
                        STATES_BTN_MAPPER['CLOS']]
186
            elif ctx['object'].last_state.status.type == "BILAN":
187
                ctx['status'] = [STATES_BTN_MAPPER['SURVEILLANCE'],
188
                        STATES_BTN_MAPPER['SUIVI'],
189
                        STATES_BTN_MAPPER['CLOS'],
190
                        STATES_BTN_MAPPER['ACCUEIL']]
191
            elif ctx['object'].last_state.status.type == "SURVEILLANCE":
192
                ctx['status'] = [STATES_BTN_MAPPER['SUIVI'],
193
                        STATES_BTN_MAPPER['CLOS'],
194
                        STATES_BTN_MAPPER['ACCUEIL'],
195
                        STATES_BTN_MAPPER['BILAN']]
196
            elif ctx['object'].last_state.status.type == "SUIVI":
197
                ctx['status'] = [STATES_BTN_MAPPER['CLOS'],
198
                        STATES_BTN_MAPPER['ACCUEIL'],
199
                        STATES_BTN_MAPPER['BILAN'],
200
                        STATES_BTN_MAPPER['SURVEILLANCE']]
201
            elif ctx['object'].last_state.status.type == "CLOS":
202
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
203
                        STATES_BTN_MAPPER['BILAN'],
204
                        STATES_BTN_MAPPER['SURVEILLANCE'],
205
                        STATES_BTN_MAPPER['SUIVI']]
206
        elif ctx['object'].service.name == "SESSAD TED" or ctx['object'].service.name == "SESSAD DYS":
207
            if ctx['object'].last_state.status.type == "ACCUEIL":
208
                ctx['status'] = [STATES_BTN_MAPPER['FIN_ACCUEIL'],
209
                        STATES_BTN_MAPPER['TRAITEMENT']]
210
            elif ctx['object'].last_state.status.type == "FIN_ACCUEIL":
211
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
212
                        STATES_BTN_MAPPER['TRAITEMENT'],
213
                        STATES_BTN_MAPPER['CLOS']]
214
            elif ctx['object'].last_state.status.type == "TRAITEMENT":
215
                ctx['status'] = [STATES_BTN_MAPPER['CLOS'],
216
                        STATES_BTN_MAPPER['ACCUEIL']]
217
            elif ctx['object'].last_state.status.type == "CLOS":
218
                ctx['status'] = [STATES_BTN_MAPPER['ACCUEIL'],
219
                        STATES_BTN_MAPPER['TRAITEMENT']]
220
        return ctx
221

    
222
patient_record = PatientRecordView.as_view()
223

    
224
class PatientRecordsHomepageView(cbv.ListView):
225
    model = PatientRecord
226
    template_name = 'dossiers/index.html'
227

    
228
    def get_queryset(self):
229
        qs = super(PatientRecordsHomepageView, self).get_queryset()
230
        first_name = self.request.GET.get('first_name')
231
        last_name = self.request.GET.get('last_name')
232
        paper_id = self.request.GET.get('paper_id')
233
        states = self.request.GET.getlist('states')
234
        social_security_id = self.request.GET.get('social_security_id')
235
        if last_name:
236
            qs = qs.filter(last_name__icontains=last_name)
237
        if first_name:
238
            qs = qs.filter(first_name__icontains=first_name)
239
        if paper_id:
240
            qs = qs.filter(paper_id__contains=paper_id)
241
        if social_security_id:
242
            qs = qs.filter(social_security_id__contains=social_security_id)
243
        if states:
244
            status_types = ['BILAN', 'SURVEILLANCE', 'SUIVI']
245
            for state in states:
246
                status_types.append(STATE_CHOICES_TYPE[state])
247
            qs = qs.filter(last_state__status__type__in=status_types)
248
        else:
249
            qs = qs.filter(last_state__status__type__in="")
250
        qs = qs.filter(service=self.service)
251
        return qs
252

    
253
    def get_context_data(self, **kwargs):
254
        ctx = super(PatientRecordsHomepageView, self).get_context_data(**kwargs)
255
        ctx['search_form'] = forms.SearchForm(data=self.request.GET or None)
256
        ctx['patient_records'] = []
257
        ctx['stats'] = {"dossiers": 0,
258
                "En_contact": 0,
259
                "Fin_daccueil": 0,
260
                "Diagnostic": 0,
261
                "Traitement": 0,
262
                "Clos": 0}
263
        if self.request.GET:
264
            for patient_record in ctx['object_list'].filter():
265
                ctx['stats']['dossiers'] += 1
266
                next_rdv = get_next_rdv(patient_record)
267
                last_rdv = get_last_rdv(patient_record)
268
                current_state = patient_record.get_current_state()
269
                if STATES_MAPPING.has_key(current_state.status.type):
270
                    state = STATES_MAPPING[current_state.status.type]
271
                else:
272
                    state = current_state.status.name
273
                state_class = patient_record.last_state.status.type.lower()
274
                ctx['patient_records'].append(
275
                        {
276
                            'object': patient_record,
277
                            'next_rdv': next_rdv,
278
                            'last_rdv': last_rdv,
279
                            'state': state,
280
                            'state_class': state_class
281
                            }
282
                        )
283
                state = state.replace(' ', '_')
284
                state = state.replace("'", '')
285
                if not ctx['stats'].has_key(state):
286
                    ctx['stats'][state] = 0
287
                ctx['stats'][state] += 1
288

    
289
        return ctx
290

    
291
patientrecord_home = PatientRecordsHomepageView.as_view()
292

    
293
class PatientRecordDeleteView(DeleteView):
294
    model = PatientRecord
295
    success_url = ".."
296
    template_name = 'dossiers/patientrecord_confirm_delete.html'
297

    
298
patientrecord_delete = PatientRecordDeleteView.as_view()
299

    
300

    
(9-9/9)