Project

General

Profile

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

calebasse / calebasse / dossiers / views.py @ 428081e0

1

    
2
from datetime import datetime
3

    
4
from django.views.generic.edit import DeleteView
5
from django.core.urlresolvers import reverse_lazy
6

    
7
from calebasse import cbv
8
from calebasse.agenda.models import Occurrence
9
from calebasse.dossiers.models import PatientRecord, Status, FileState, create_patient
10
from calebasse.dossiers.forms import (SearchForm, CivilStatusForm, StateForm,
11
        PhysiologyForm, FamilyForm, InscriptionForm, GeneralForm,
12
        NewPatientRecordForm, TransportFrom, FollowUpForm)
13
from calebasse.dossiers.states import STATES_MAPPING, STATE_CHOICES_TYPE
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 = 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
        create_patient(form.data['first_name'], form.data['last_name'], self.service,
46
                self.user)
47
        return super(NewPatientRecordView, self).form_valid(form)
48

    
49
new_patient_record = NewPatientRecordView.as_view()
50

    
51
class StateFormView(cbv.FormView):
52
    template_name = 'dossiers/state.html'
53
    form_class = StateForm
54
    success_url = '..'
55

    
56
    def post(self, request, *args, **kwarg):
57
        self.user = request.user
58
        return super(StateFormView, self).post(request, *args, **kwarg)
59

    
60
    def form_valid(self, form):
61
        service = Service.objects.get(id=form.data['service_id'])
62
        status = Status.objects.filter(services=service).filter(type=form.data['state_type'])
63
        patient = PatientRecord.objects.get(id=form.data['patient_id'])
64
        date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
65
        patient.set_state(status[0], self.user, date_selected, form.data['comment'])
66
        return super(StateFormView, self).form_valid(form)
67

    
68
state_form = StateFormView.as_view()
69

    
70

    
71
class PatientRecordView(cbv.ServiceViewMixin, cbv.MultiUpdateView):
72
    model = PatientRecord
73
    forms_classes = {
74
            'general': GeneralForm,
75
            'id': CivilStatusForm,
76
            'physiology': PhysiologyForm,
77
            'inscription': InscriptionForm,
78
            'family': FamilyForm,
79
            'transport': TransportFrom,
80
            'followup': FollowUpForm
81
            }
82
    template_name = 'dossiers/patientrecord_update.html'
83
    success_url = './view'
84

    
85
    def get_success_url(self):
86
        if self.request.POST.has_key('tab'):
87
            return self.success_url + '#tab=' + self.request.POST['tab']
88
        else:
89
            return self.success_url
90

    
91
    def get_context_data(self, **kwargs):
92
        ctx = super(PatientRecordView, self).get_context_data(**kwargs)
93
        ctx['next_rdv'] = get_next_rdv(ctx['object'])
94
        ctx['last_rdv'] = get_last_rdv(ctx['object'])
95
        current_state = ctx['object'].get_current_state()
96
        if STATES_MAPPING.has_key(current_state.status.type):
97
            state = STATES_MAPPING[current_state.status.type]
98
        else:
99
            state = current_state.status.name
100
        ctx['current_state'] = current_state
101
        ctx['service_id'] = self.service.id
102
        ctx['states'] = FileState.objects.filter(patient=self.object).filter(status__services=self.service)
103
        return ctx
104

    
105
patient_record = PatientRecordView.as_view()
106

    
107
class PatientRecordsHomepageView(cbv.ListView):
108
    model = PatientRecord
109
    template_name = 'dossiers/index.html'
110

    
111
    def get_queryset(self):
112
        qs = super(PatientRecordsHomepageView, self).get_queryset()
113
        first_name = self.request.GET.get('first_name')
114
        last_name = self.request.GET.get('last_name')
115
        paper_id = self.request.GET.get('paper_id')
116
        states = self.request.GET.getlist('states')
117
        social_security_id = self.request.GET.get('social_security_id')
118
        if last_name:
119
            qs = qs.filter(last_name__icontains=last_name)
120
        if first_name:
121
            qs = qs.filter(first_name__icontains=first_name)
122
        if paper_id:
123
            qs = qs.filter(paper_id__contains=paper_id)
124
        if social_security_id:
125
            qs = qs.filter(social_security_id__contains=social_security_id)
126
        if states:
127
            status_types = ['BILAN', 'SURVEILLANCE', 'SUIVI']
128
            for state in states:
129
                status_types.append(STATE_CHOICES_TYPE[state])
130
            qs = qs.filter(last_state__status__type__in=status_types)
131
        else:
132
            qs = qs.filter(last_state__status__type__in="")
133
        qs = qs.filter(service=self.service)
134
        return qs
135

    
136
    def get_context_data(self, **kwargs):
137
        ctx = super(PatientRecordsHomepageView, self).get_context_data(**kwargs)
138
        ctx['search_form'] = SearchForm(data=self.request.GET or None)
139
        ctx['patient_records'] = []
140
        ctx['stats'] = {"dossiers": 0,
141
                "En_contact": 0,
142
                "Fin_daccueil": 0,
143
                "Diagnostic": 0,
144
                "Traitement": 0,
145
                "Clos": 0}
146
        if self.request.GET:
147
            for patient_record in ctx['object_list'].filter():
148
                ctx['stats']['dossiers'] += 1
149
                next_rdv = get_next_rdv(patient_record)
150
                last_rdv = get_last_rdv(patient_record)
151
                current_state = patient_record.get_current_state()
152
                if STATES_MAPPING.has_key(current_state.status.type):
153
                    state = STATES_MAPPING[current_state.status.type]
154
                else:
155
                    state = current_state.status.name
156
                state_class = patient_record.last_state.status.type.lower()
157
                ctx['patient_records'].append(
158
                        {
159
                            'object': patient_record,
160
                            'next_rdv': next_rdv,
161
                            'last_rdv': last_rdv,
162
                            'state': state,
163
                            'state_class': state_class
164
                            }
165
                        )
166
                state = state.replace(' ', '_')
167
                state = state.replace("'", '')
168
                if not ctx['stats'].has_key(state):
169
                    ctx['stats'][state] = 0
170
                ctx['stats'][state] += 1
171

    
172
        return ctx
173

    
174
patientrecord_home = PatientRecordsHomepageView.as_view()
175

    
176
class PatientRecordDeleteView(DeleteView):
177
    model = PatientRecord
178
    success_url = ".."
179
    template_name = 'dossiers/patientrecord_confirm_delete.html'
180

    
181
patientrecord_delete = PatientRecordDeleteView.as_view()
182

    
(9-9/9)