1
|
import datetime
|
2
|
import collections
|
3
|
|
4
|
from django.db.models import Q
|
5
|
from django.shortcuts import redirect
|
6
|
|
7
|
from calebasse.cbv import TemplateView, CreateView
|
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 Service, WorkerType
|
14
|
|
15
|
from forms import NewAppointmentForm, NewEventForm
|
16
|
|
17
|
def redirect_today(request, service):
|
18
|
'''If not date is given we redirect on the agenda for today'''
|
19
|
return redirect('agenda', date=datetime.date.today().strftime('%Y-%m-%d'),
|
20
|
service=service)
|
21
|
|
22
|
class AgendaHomepageView(TemplateView):
|
23
|
|
24
|
template_name = 'agenda/index.html'
|
25
|
|
26
|
def get_context_data(self, **kwargs):
|
27
|
context = super(AgendaHomepageView, self).get_context_data(**kwargs)
|
28
|
|
29
|
weekday_mapping = {
|
30
|
'0': u'dimanche',
|
31
|
'1': u'lundi',
|
32
|
'2': u'mardi',
|
33
|
'3': u'mercredi',
|
34
|
'4': u'jeudi',
|
35
|
'5': u'vendredi',
|
36
|
'6': u'samedi'
|
37
|
}
|
38
|
weekday = weekday_mapping[context['date'].strftime("%w")]
|
39
|
time_tables = TimeTable.objects.select_related('worker').\
|
40
|
filter(service=self.service).\
|
41
|
filter(weekday=weekday).\
|
42
|
filter(start_date__lte=context['date']).\
|
43
|
filter(Q(end_date=None) |Q(end_date__gte=context['date'])).\
|
44
|
order_by('start_date')
|
45
|
occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
|
46
|
|
47
|
context['workers_types'] = []
|
48
|
context['workers_agenda'] = []
|
49
|
context['disponnibility'] = {}
|
50
|
workers = []
|
51
|
for worker_type in WorkerType.objects.all():
|
52
|
data = {'type': worker_type.name, 'workers': Worker.objects.for_service(self.service, worker_type) }
|
53
|
context['workers_types'].append(data)
|
54
|
workers.extend(data['workers'])
|
55
|
|
56
|
occurrences_workers = {}
|
57
|
time_tables_workers = {}
|
58
|
for worker in workers:
|
59
|
time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
|
60
|
occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
|
61
|
occurrences_workers[worker.id] = occurrences_worker
|
62
|
time_tables_workers[worker.id] = time_tables_worker
|
63
|
context['workers_agenda'].append({'worker': worker,
|
64
|
'appointments': get_daily_appointments(context['date'], worker, self.service,
|
65
|
time_tables_worker, occurrences_worker)})
|
66
|
|
67
|
context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'],
|
68
|
occurrences_workers, workers, time_tables_workers)
|
69
|
return context
|
70
|
|
71
|
class AgendaServiceActivityView(TemplateView):
|
72
|
|
73
|
template_name = 'agenda/service-activity.html'
|
74
|
|
75
|
def get_context_data(self, **kwargs):
|
76
|
context = super(AgendaServiceActivityView, self).get_context_data(**kwargs)
|
77
|
|
78
|
appointments_times = dict()
|
79
|
appoinment_type = EventType.objects.get(id=1)
|
80
|
occurrences = Occurrence.objects.daily_occurrences(context['date'],
|
81
|
services=[self.service],
|
82
|
event_type=appoinment_type)
|
83
|
for occurrence in occurrences:
|
84
|
start_time = occurrence.start_time.strftime("%H:%M")
|
85
|
if not appointments_times.has_key(start_time):
|
86
|
appointments_times[start_time] = dict()
|
87
|
appointments_times[start_time]['row'] = 0
|
88
|
appointments_times[start_time]['appointments'] = []
|
89
|
appointment = dict()
|
90
|
length = occurrence.end_time - occurrence.start_time
|
91
|
if length.seconds:
|
92
|
length = length.seconds / 60
|
93
|
appointment['length'] = "%sm" % length
|
94
|
event_act = occurrence.event.eventact
|
95
|
appointment['patient'] = event_act.patient.display_name
|
96
|
appointment['therapists'] = ""
|
97
|
for participant in occurrence.event.participants.all():
|
98
|
appointment['therapists'] += participant.display_name + "; "
|
99
|
if appointment['therapists']:
|
100
|
appointment['therapists'] = appointment['therapists'][:-2]
|
101
|
appointment['act'] = event_act.act_type.name
|
102
|
appointments_times[start_time]['row'] += 1
|
103
|
appointments_times[start_time]['appointments'].append(appointment)
|
104
|
context['appointments_times'] = collections.OrderedDict(sorted(appointments_times.items()))
|
105
|
return context
|
106
|
|
107
|
|
108
|
class NewAppointmentView(CreateView):
|
109
|
model = EventAct
|
110
|
form_class = NewAppointmentForm
|
111
|
template_name = 'agenda/nouveau-rdv.html'
|
112
|
success_url = '..'
|
113
|
|
114
|
def get_initial(self):
|
115
|
initial = super(NewAppointmentView, self).get_initial()
|
116
|
initial['date'] = self.kwargs.get('date')
|
117
|
initial['participants'] = self.request.GET.getlist('participants')
|
118
|
initial['time'] = self.request.GET.get('time')
|
119
|
return initial
|
120
|
|
121
|
def get_form_kwargs(self):
|
122
|
kwargs = super(NewAppointmentView, self).get_form_kwargs()
|
123
|
kwargs['service'] = self.service
|
124
|
return kwargs
|
125
|
|
126
|
def post(self, *args, **kwargs):
|
127
|
return super(NewAppointmentView, self).post(*args, **kwargs)
|
128
|
|
129
|
class NewEventView(CreateView):
|
130
|
model = Event
|
131
|
form_class = NewEventForm
|
132
|
template_name = 'agenda/new-event.html'
|
133
|
success_url = '..'
|
134
|
|
135
|
def get_initial(self):
|
136
|
initial = super(NewEventView, self).get_initial()
|
137
|
initial['date'] = self.kwargs.get('date')
|
138
|
initial['participants'] = self.request.GET.getlist('participants')
|
139
|
initial['time'] = self.request.GET.get('time')
|
140
|
return initial
|
141
|
|
142
|
def get_form_kwargs(self):
|
143
|
kwargs = super(NewEventView, self).get_form_kwargs()
|
144
|
#kwargs['service'] = self.service
|
145
|
return kwargs
|
146
|
|
147
|
def post(self, *args, **kwargs):
|
148
|
return super(NewEventView, self).post(*args, **kwargs)
|
149
|
|
150
|
def new_appointment(request):
|
151
|
pass
|
152
|
|