1
|
|
2
|
from datetime import datetime
|
3
|
|
4
|
from calebasse.cbv import ListView, MultiUpdateView, FormView, ServiceViewMixin
|
5
|
from calebasse.agenda.models import Occurrence
|
6
|
from calebasse.dossiers.models import PatientRecord, Status, FileState
|
7
|
from calebasse.dossiers.forms import (SearchForm, CivilStatusForm, StateForm,
|
8
|
PhysiologyForm, FamillyForm, InscriptionForm, GeneralForm)
|
9
|
from calebasse.dossiers.states import STATES_MAPPING, STATE_CHOICES_TYPE
|
10
|
from calebasse.ressources.models import Service
|
11
|
|
12
|
|
13
|
def get_next_rdv(patient_record):
|
14
|
next_rdv = {}
|
15
|
occurrence = Occurrence.objects.next_appoinment(patient_record)
|
16
|
if occurrence:
|
17
|
next_rdv['start_datetime'] = occurrence.start_time
|
18
|
next_rdv['participants'] = occurrence.event.participants.all()
|
19
|
next_rdv['act_type'] = occurrence.event.eventact.act_type
|
20
|
return next_rdv
|
21
|
|
22
|
def get_last_rdv(patient_record):
|
23
|
last_rdv = {}
|
24
|
occurrence = Occurrence.objects.last_appoinment(patient_record)
|
25
|
if occurrence:
|
26
|
last_rdv['start_datetime'] = occurrence.start_time
|
27
|
last_rdv['participants'] = occurrence.event.participants.all()
|
28
|
last_rdv['act_type'] = occurrence.event.eventact.act_type
|
29
|
return last_rdv
|
30
|
|
31
|
class StateFormView(FormView):
|
32
|
template_name = 'dossiers/state.html'
|
33
|
form_class = StateForm
|
34
|
success_url = '..'
|
35
|
|
36
|
def post(self, request, *args, **kwarg):
|
37
|
self.user = request.user
|
38
|
return super(StateFormView, self).post(request, *args, **kwarg)
|
39
|
|
40
|
def form_valid(self, form):
|
41
|
service = Service.objects.get(id=form.data['service_id'])
|
42
|
status = Status.objects.filter(services=service).filter(type=form.data['state_type'])
|
43
|
patient = PatientRecord.objects.get(id=form.data['patient_id'])
|
44
|
date_selected = datetime.strptime(form.data['date'], "%d/%m/%Y")
|
45
|
patient.set_state(status[0], self.user, date_selected, form.data['comment'])
|
46
|
return super(StateFormView, self).form_valid(form)
|
47
|
|
48
|
state_form = StateFormView.as_view()
|
49
|
|
50
|
|
51
|
class PatientRecordView(ServiceViewMixin, MultiUpdateView):
|
52
|
model = PatientRecord
|
53
|
forms_classes = {
|
54
|
'general': GeneralForm,
|
55
|
'id': CivilStatusForm,
|
56
|
'physiology': PhysiologyForm,
|
57
|
'inscription': InscriptionForm,
|
58
|
'familly': FamillyForm,
|
59
|
}
|
60
|
template_name = 'dossiers/patientrecord_update.html'
|
61
|
success_url = './view'
|
62
|
|
63
|
def get_success_url(self):
|
64
|
if self.request.POST.has_key('tab'):
|
65
|
return self.success_url + '#tab=' + self.request.POST['tab']
|
66
|
else:
|
67
|
return self.success_url
|
68
|
|
69
|
def get_context_data(self, **kwargs):
|
70
|
ctx = super(PatientRecordView, self).get_context_data(**kwargs)
|
71
|
ctx['next_rdv'] = get_next_rdv(ctx['object'])
|
72
|
ctx['last_rdv'] = get_last_rdv(ctx['object'])
|
73
|
ctx['service_id'] = self.service.id
|
74
|
ctx['states'] = FileState.objects.filter(patient=self.object).filter(status__services=self.service)
|
75
|
return ctx
|
76
|
|
77
|
patient_record = PatientRecordView.as_view()
|
78
|
|
79
|
class PatientRecordsHomepageView(ListView):
|
80
|
model = PatientRecord
|
81
|
template_name = 'dossiers/index.html'
|
82
|
|
83
|
def get_queryset(self):
|
84
|
qs = super(PatientRecordsHomepageView, self).get_queryset()
|
85
|
first_name = self.request.GET.get('first_name')
|
86
|
last_name = self.request.GET.get('last_name')
|
87
|
paper_id = self.request.GET.get('paper_id')
|
88
|
states = self.request.GET.getlist('states')
|
89
|
social_security_id = self.request.GET.get('social_security_id')
|
90
|
if last_name:
|
91
|
qs = qs.filter(last_name__icontains=last_name)
|
92
|
if first_name:
|
93
|
qs = qs.filter(first_name__icontains=first_name)
|
94
|
if paper_id:
|
95
|
qs = qs.filter(paper_id__contains=paper_id)
|
96
|
if social_security_id:
|
97
|
qs = qs.filter(social_security_id__contains=social_security_id)
|
98
|
if states:
|
99
|
status_types = []
|
100
|
for state in states:
|
101
|
status_types.append(STATE_CHOICES_TYPE[state])
|
102
|
qs = qs.filter(last_state__status__type__in=status_types)
|
103
|
return qs
|
104
|
|
105
|
def get_context_data(self, **kwargs):
|
106
|
ctx = super(PatientRecordsHomepageView, self).get_context_data(**kwargs)
|
107
|
ctx['search_form'] = SearchForm(data=self.request.GET or None)
|
108
|
ctx['patient_records'] = []
|
109
|
ctx['stats'] = {"dossiers": 0,
|
110
|
"En_contact": 0,
|
111
|
"Fin_daccueil": 0,
|
112
|
"Diagnostic": 0,
|
113
|
"Traitement": 0,
|
114
|
"Clos": 0}
|
115
|
if self.request.GET:
|
116
|
for patient_record in ctx['object_list'].filter():
|
117
|
ctx['stats']['dossiers'] += 1
|
118
|
next_rdv = get_next_rdv(patient_record)
|
119
|
last_rdv = get_last_rdv(patient_record)
|
120
|
if STATES_MAPPING.has_key(patient_record.last_state.status.type):
|
121
|
state = STATES_MAPPING[patient_record.last_state.status.type]
|
122
|
else:
|
123
|
state = patient_record.last_state.status.name
|
124
|
state_class = patient_record.last_state.status.type.lower()
|
125
|
ctx['patient_records'].append(
|
126
|
{
|
127
|
'object': patient_record,
|
128
|
'next_rdv': next_rdv,
|
129
|
'last_rdv': last_rdv,
|
130
|
'state': state,
|
131
|
'state_class': state_class
|
132
|
}
|
133
|
)
|
134
|
state = state.replace(' ', '_')
|
135
|
state = state.replace("'", '')
|
136
|
if not ctx['stats'].has_key(state):
|
137
|
ctx['stats'][state] = 0
|
138
|
ctx['stats'][state] += 1
|
139
|
|
140
|
return ctx
|
141
|
|
142
|
patientrecord_home = PatientRecordsHomepageView.as_view()
|
143
|
|