Project

General

Profile

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

calebasse / calebasse / agenda / views.py @ ad9330e1

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

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

    
68
class AgendaServiceActivityView(TemplateView):
69

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

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

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