1
|
import datetime
|
2
|
|
3
|
from django.db.models import Q
|
4
|
from django.shortcuts import redirect
|
5
|
|
6
|
from calebasse.cbv import TemplateView, CreateView
|
7
|
from calebasse.agenda.models import Occurrence
|
8
|
from calebasse.personnes.models import TimeTable
|
9
|
from calebasse.actes.models import EventAct
|
10
|
from calebasse.agenda.appointments import get_daily_appointments
|
11
|
from calebasse.personnes.models import Worker
|
12
|
from calebasse.ressources.models import Service, WorkerType
|
13
|
|
14
|
from forms import NewAppointmentForm
|
15
|
|
16
|
def redirect_today(request, service):
|
17
|
'''If not date is given we redirect on the agenda for today'''
|
18
|
return redirect('agenda', date=datetime.date.today().strftime('%Y-%m-%d'),
|
19
|
service=service)
|
20
|
|
21
|
class AgendaHomepageView(TemplateView):
|
22
|
|
23
|
template_name = 'agenda/index.html'
|
24
|
|
25
|
def get_context_data(self, **kwargs):
|
26
|
context = super(AgendaHomepageView, self).get_context_data(**kwargs)
|
27
|
|
28
|
weekday_mapping = {
|
29
|
'0': u'dimanche',
|
30
|
'1': u'lundi',
|
31
|
'2': u'mardi',
|
32
|
'3': u'mercredi',
|
33
|
'4': u'jeudi',
|
34
|
'5': u'vendredi',
|
35
|
'6': u'samedi'
|
36
|
}
|
37
|
weekday = weekday_mapping[context['date'].strftime("%w")]
|
38
|
time_tables = TimeTable.objects.select_related('worker').\
|
39
|
filter(service=self.service).\
|
40
|
filter(weekday=weekday).\
|
41
|
filter(start_date__lte=context['date']).\
|
42
|
filter(Q(end_date=None) |Q(end_date__gte=context['date'])).\
|
43
|
order_by('start_date')
|
44
|
occurrences = Occurrence.objects.daily_occurrences(context['date']).select_related().order_by('start_time')
|
45
|
|
46
|
context['workers_types'] = []
|
47
|
context['workers_agenda'] = []
|
48
|
context['disponnibility'] = {}
|
49
|
workers = []
|
50
|
for worker_type in WorkerType.objects.all():
|
51
|
data = {'type': worker_type.name, 'workers': Worker.objects.for_service(self.service, worker_type) }
|
52
|
context['workers_types'].append(data)
|
53
|
workers.extend(data['workers'])
|
54
|
|
55
|
for worker in workers:
|
56
|
time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
|
57
|
occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
|
58
|
context['workers_agenda'].append({'worker': worker,
|
59
|
'appointments': get_daily_appointments(context['date'], worker, self.service,
|
60
|
time_tables_worker, occurrences_worker)})
|
61
|
|
62
|
context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'], workers)
|
63
|
return context
|
64
|
|
65
|
class NewAppointmentView(CreateView):
|
66
|
model = EventAct
|
67
|
form_class = NewAppointmentForm
|
68
|
template_name = 'agenda/nouveau-rdv.html'
|
69
|
success_url = '..'
|
70
|
|
71
|
def get_initial(self):
|
72
|
initial = super(NewAppointmentView, self).get_initial()
|
73
|
initial['date'] = self.kwargs.get('date')
|
74
|
initial['participants'] = self.request.GET.getlist('participants')
|
75
|
initial['time'] = self.request.GET.get('time')
|
76
|
return initial
|
77
|
|
78
|
def get_form_kwargs(self):
|
79
|
kwargs = super(NewAppointmentView, self).get_form_kwargs()
|
80
|
kwargs['service'] = self.service
|
81
|
return kwargs
|
82
|
|
83
|
def post(self, *args, **kwargs):
|
84
|
return super(NewAppointmentView, self).post(*args, **kwargs)
|
85
|
|
86
|
def new_appointment(request):
|
87
|
pass
|
88
|
|