Project

General

Profile

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

calebasse / calebasse / agenda / views.py @ 3465089e

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
        for worker in workers:
58
            time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
59
            occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
60
            occurrences_workers[worker.id] = occurrences_worker
61
            context['workers_agenda'].append({'worker': worker,
62
                    'appointments': get_daily_appointments(context['date'], worker, self.service,
63
                        time_tables_worker, occurrences_worker)})
64

    
65
        context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'],
66
                occurrences_workers, workers)
67
        return context
68

    
69
class AgendaServiceActivityView(TemplateView):
70

    
71
    template_name = 'agenda/service-activity.html'
72

    
73
    def get_context_data(self, **kwargs):
74
        context = super(AgendaServiceActivityView, self).get_context_data(**kwargs)
75

    
76
        appointments_times = dict()
77
        appoinment_type = EventType.objects.get(id=1)
78
        occurrences = Occurrence.objects.daily_occurrences(context['date'],
79
                services=[self.service],
80
                event_type=appoinment_type)
81
        for occurrence in occurrences:
82
            start_time = occurrence.start_time.strftime("%H:%M")
83
            if not appointments_times.has_key(start_time):
84
                appointments_times[start_time] = dict()
85
                appointments_times[start_time]['row'] = 0
86
                appointments_times[start_time]['appointments'] = []
87
            appointment = dict()
88
            length = occurrence.end_time - occurrence.start_time
89
            if length.seconds:
90
                length = length.seconds / 60
91
                appointment['length'] = "%sm" % length
92
            event_act = occurrence.event.eventact
93
            appointment['patient'] = event_act.patient.display_name
94
            appointment['therapists'] = ""
95
            for participant in occurrence.event.participants.all():
96
                appointment['therapists'] += participant.display_name + "; "
97
            if appointment['therapists']:
98
                appointment['therapists'] = appointment['therapists'][:-2]
99
            appointment['act'] = event_act.act_type.name
100
            appointments_times[start_time]['row'] += 1
101
            appointments_times[start_time]['appointments'].append(appointment)
102
        context['appointments_times'] = collections.OrderedDict(sorted(appointments_times.items()))
103
        return context
104

    
105

    
106
class NewAppointmentView(CreateView):
107
    model = EventAct
108
    form_class = NewAppointmentForm
109
    template_name = 'agenda/nouveau-rdv.html'
110
    success_url = '..'
111

    
112
    def get_initial(self):
113
        initial = super(NewAppointmentView, self).get_initial()
114
        initial['date'] = self.kwargs.get('date')
115
        initial['participants'] = self.request.GET.getlist('participants')
116
        initial['time'] = self.request.GET.get('time')
117
        return initial
118

    
119
    def get_form_kwargs(self):
120
        kwargs = super(NewAppointmentView, self).get_form_kwargs()
121
        kwargs['service'] = self.service
122
        return kwargs
123

    
124
    def post(self, *args, **kwargs):
125
        return super(NewAppointmentView, self).post(*args, **kwargs)
126

    
127
class NewEventView(CreateView):
128
    model = Event
129
    form_class = NewEventForm
130
    template_name = 'agenda/new-event.html'
131
    success_url = '..'
132

    
133
    def get_initial(self):
134
        initial = super(NewEventView, self).get_initial()
135
        initial['date'] = self.kwargs.get('date')
136
        initial['participants'] = self.request.GET.getlist('participants')
137
        initial['time'] = self.request.GET.get('time')
138
        return initial
139

    
140
    def get_form_kwargs(self):
141
        kwargs = super(NewEventView, self).get_form_kwargs()
142
        #kwargs['service'] = self.service
143
        return kwargs
144

    
145
    def post(self, *args, **kwargs):
146
        return super(NewEventView, self).post(*args, **kwargs)
147

    
148
def new_appointment(request):
149
    pass
150

    
(10-10/10)