1
|
import datetime
|
2
|
|
3
|
from django.db.models import Q
|
4
|
from django.shortcuts import redirect
|
5
|
from django.http import HttpResponseRedirect
|
6
|
|
7
|
from calebasse.cbv import TemplateView, CreateView, UpdateView
|
8
|
from calebasse.agenda.models import Occurrence, Event, EventType
|
9
|
from calebasse.personnes.models import TimeTable
|
10
|
from calebasse.actes.models import EventAct
|
11
|
from calebasse.agenda.appointments import get_daily_appointments
|
12
|
from calebasse.personnes.models import Worker
|
13
|
from calebasse.ressources.models import WorkerType
|
14
|
from calebasse.actes.validation import (are_all_acts_of_the_day_locked,
|
15
|
get_acts_of_the_day)
|
16
|
from calebasse.actes.validation_states import VALIDATION_STATES, VALIDE
|
17
|
from calebasse.actes.models import Act
|
18
|
from calebasse.actes.validation import (automated_validation,
|
19
|
unlock_all_acts_of_the_day)
|
20
|
|
21
|
from forms import NewAppointmentForm, NewEventForm
|
22
|
|
23
|
def redirect_today(request, service):
|
24
|
'''If not date is given we redirect on the agenda for today'''
|
25
|
return redirect('agenda', date=datetime.date.today().strftime('%Y-%m-%d'),
|
26
|
service=service)
|
27
|
|
28
|
class AgendaHomepageView(TemplateView):
|
29
|
|
30
|
template_name = 'agenda/index.html'
|
31
|
|
32
|
def get_context_data(self, **kwargs):
|
33
|
context = super(AgendaHomepageView, self).get_context_data(**kwargs)
|
34
|
|
35
|
weekday_mapping = {
|
36
|
'0': u'dimanche',
|
37
|
'1': u'lundi',
|
38
|
'2': u'mardi',
|
39
|
'3': u'mercredi',
|
40
|
'4': u'jeudi',
|
41
|
'5': u'vendredi',
|
42
|
'6': u'samedi'
|
43
|
}
|
44
|
weekday = weekday_mapping[context['date'].strftime("%w")]
|
45
|
time_tables = TimeTable.objects.select_related('worker').\
|
46
|
filter(service=self.service).\
|
47
|
filter(weekday=weekday).\
|
48
|
filter(start_date__lte=context['date']).\
|
49
|
filter(Q(end_date=None) |Q(end_date__gte=context['date'])).\
|
50
|
order_by('start_date')
|
51
|
occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
|
52
|
|
53
|
context['workers_types'] = []
|
54
|
context['workers_agenda'] = []
|
55
|
context['disponnibility'] = {}
|
56
|
workers = []
|
57
|
for worker_type in WorkerType.objects.all():
|
58
|
workers_type = Worker.objects.for_service(self.service, worker_type)
|
59
|
if workers_type:
|
60
|
data = {'type': worker_type.name, 'workers': workers_type }
|
61
|
context['workers_types'].append(data)
|
62
|
workers.extend(data['workers'])
|
63
|
|
64
|
occurrences_workers = {}
|
65
|
time_tables_workers = {}
|
66
|
for worker in workers:
|
67
|
time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
|
68
|
occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
|
69
|
occurrences_workers[worker.id] = occurrences_worker
|
70
|
time_tables_workers[worker.id] = time_tables_worker
|
71
|
context['workers_agenda'].append({'worker': worker,
|
72
|
'appointments': get_daily_appointments(context['date'], worker, self.service,
|
73
|
time_tables_worker, occurrences_worker)})
|
74
|
|
75
|
context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'],
|
76
|
occurrences_workers, workers, time_tables_workers)
|
77
|
return context
|
78
|
|
79
|
class AgendaServiceActivityView(TemplateView):
|
80
|
|
81
|
template_name = 'agenda/service-activity.html'
|
82
|
|
83
|
def get_context_data(self, **kwargs):
|
84
|
context = super(AgendaServiceActivityView, self).get_context_data(**kwargs)
|
85
|
|
86
|
appointments_times = dict()
|
87
|
appoinment_type = EventType.objects.get(id=1)
|
88
|
occurrences = Occurrence.objects.daily_occurrences(context['date'],
|
89
|
services=[self.service],
|
90
|
event_type=appoinment_type)
|
91
|
for occurrence in occurrences:
|
92
|
start_time = occurrence.start_time.strftime("%H:%M")
|
93
|
if not appointments_times.has_key(start_time):
|
94
|
appointments_times[start_time] = dict()
|
95
|
appointments_times[start_time]['row'] = 0
|
96
|
appointments_times[start_time]['appointments'] = []
|
97
|
appointment = dict()
|
98
|
length = occurrence.end_time - occurrence.start_time
|
99
|
if length.seconds:
|
100
|
length = length.seconds / 60
|
101
|
appointment['length'] = "%sm" % length
|
102
|
event_act = occurrence.event.eventact
|
103
|
appointment['patient'] = event_act.patient.display_name
|
104
|
appointment['therapists'] = ""
|
105
|
for participant in occurrence.event.participants.all():
|
106
|
appointment['therapists'] += participant.display_name + "; "
|
107
|
if appointment['therapists']:
|
108
|
appointment['therapists'] = appointment['therapists'][:-2]
|
109
|
appointment['act'] = event_act.act_type.name
|
110
|
appointments_times[start_time]['row'] += 1
|
111
|
appointments_times[start_time]['appointments'].append(appointment)
|
112
|
context['appointments_times'] = sorted(appointments_times.items())
|
113
|
return context
|
114
|
|
115
|
|
116
|
class NewAppointmentView(CreateView):
|
117
|
model = EventAct
|
118
|
form_class = NewAppointmentForm
|
119
|
template_name = 'agenda/nouveau-rdv.html'
|
120
|
success_url = '..'
|
121
|
|
122
|
def get_initial(self):
|
123
|
initial = super(NewAppointmentView, self).get_initial()
|
124
|
initial['date'] = self.kwargs.get('date')
|
125
|
initial['participants'] = self.request.GET.getlist('participants')
|
126
|
initial['time'] = self.request.GET.get('time')
|
127
|
return initial
|
128
|
|
129
|
def get_form_kwargs(self):
|
130
|
kwargs = super(NewAppointmentView, self).get_form_kwargs()
|
131
|
kwargs['service'] = self.service
|
132
|
return kwargs
|
133
|
|
134
|
def post(self, *args, **kwargs):
|
135
|
return super(NewAppointmentView, self).post(*args, **kwargs)
|
136
|
|
137
|
class UpdateAppointmentView(UpdateView):
|
138
|
model = EventAct
|
139
|
form_class = NewAppointmentForm
|
140
|
template_name = 'agenda/nouveau-rdv.html'
|
141
|
success_url = '..'
|
142
|
|
143
|
def get_object(self, queryset=None):
|
144
|
self.occurrence = Occurrence.objects.get(id=self.kwargs['id'])
|
145
|
if self.occurrence.event.eventact:
|
146
|
return self.occurrence.event.eventact
|
147
|
|
148
|
def get_initial(self):
|
149
|
initial = super(UpdateView, self).get_initial()
|
150
|
initial['date'] = self.object.date.strftime("%Y-%m-%d")
|
151
|
initial['time'] = self.occurrence.start_time.strftime("%H:%M")
|
152
|
time = self.occurrence.end_time - self.occurrence.start_time
|
153
|
if time:
|
154
|
time = time.seconds / 60
|
155
|
else:
|
156
|
time = 0
|
157
|
initial['duration'] = time
|
158
|
initial['participants'] = self.object.participants.values_list('id', flat=True)
|
159
|
return initial
|
160
|
|
161
|
def post(self, *args, **kwargs):
|
162
|
return super(UpdateAppointmentView, self).post(*args, **kwargs)
|
163
|
|
164
|
|
165
|
class NewEventView(CreateView):
|
166
|
model = Event
|
167
|
form_class = NewEventForm
|
168
|
template_name = 'agenda/new-event.html'
|
169
|
success_url = '..'
|
170
|
|
171
|
def get_initial(self):
|
172
|
initial = super(NewEventView, self).get_initial()
|
173
|
initial['date'] = self.kwargs.get('date')
|
174
|
initial['participants'] = self.request.GET.getlist('participants')
|
175
|
initial['time'] = self.request.GET.get('time')
|
176
|
initial['services'] = [self.service]
|
177
|
return initial
|
178
|
|
179
|
def get_form_kwargs(self):
|
180
|
kwargs = super(NewEventView, self).get_form_kwargs()
|
181
|
return kwargs
|
182
|
|
183
|
def post(self, *args, **kwargs):
|
184
|
return super(NewEventView, self).post(*args, **kwargs)
|
185
|
|
186
|
def new_appointment(request):
|
187
|
pass
|
188
|
|
189
|
class AgendaServiceActValidationView(TemplateView):
|
190
|
|
191
|
template_name = 'agenda/act-validation.html'
|
192
|
|
193
|
def acts_of_the_day(self):
|
194
|
return get_acts_of_the_day(self.date)
|
195
|
|
196
|
def post(self, request, *args, **kwargs):
|
197
|
if 'validate-all' in request.POST:
|
198
|
#TODO: check that the user is authorized
|
199
|
(nb_acts_total, nb_acts_validated, nb_acts_double,
|
200
|
nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_annul_nous,
|
201
|
nb_acts_annul_famille, nb_acts_abs_ess_pps,
|
202
|
nb_acts_enf_hosp) = \
|
203
|
automated_validation(self.date, self.service,
|
204
|
request.user)
|
205
|
elif 'unlock-all' in request.POST:
|
206
|
#TODO: check that the user is authorized
|
207
|
unlock_all_acts_of_the_day(self.date)
|
208
|
else:
|
209
|
acte_id = request.POST.get('acte-id')
|
210
|
try:
|
211
|
act = Act.objects.get(id=acte_id)
|
212
|
if 'lock' in request.POST or 'unlock' in request.POST:
|
213
|
#TODO: check that the user is authorized
|
214
|
act.validation_locked = 'lock' in request.POST
|
215
|
act.save()
|
216
|
else:
|
217
|
state_name = request.POST.get('act_state')
|
218
|
act.set_state(state_name, request.user)
|
219
|
except Act.DoesNotExist:
|
220
|
pass
|
221
|
return HttpResponseRedirect('#acte-frame-'+acte_id)
|
222
|
return HttpResponseRedirect('')
|
223
|
|
224
|
def get_context_data(self, **kwargs):
|
225
|
context = super(AgendaServiceActValidationView, self).get_context_data(**kwargs)
|
226
|
authorized_lock = True # is_authorized_for_locking(get_request().user)
|
227
|
validation_msg = list()
|
228
|
acts_of_the_day = self.acts_of_the_day()
|
229
|
actes = list()
|
230
|
for act in acts_of_the_day:
|
231
|
state = act.get_state()
|
232
|
display_name = VALIDATION_STATES[state.state_name]
|
233
|
if not state.previous_state:
|
234
|
state = None
|
235
|
act.date = act.date.strftime("%H:%M")
|
236
|
actes.append((act, state, display_name))
|
237
|
context['validation_states'] = VALIDATION_STATES
|
238
|
context['actes'] = actes
|
239
|
context['validation_msg'] = validation_msg
|
240
|
context['authorized_lock'] = authorized_lock
|
241
|
return context
|
242
|
|
243
|
|
244
|
class AutomatedValidationView(CreateView):
|
245
|
pass
|
246
|
|
247
|
class UnlockAllView(CreateView):
|
248
|
pass
|