Project

General

Profile

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

calebasse / calebasse / agenda / views.py @ d86f6cd6

1
import datetime
2

    
3
from django.db.models import Q
4
from django.shortcuts import redirect
5
from django.shortcuts import render_to_response
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
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
17
from calebasse.middleware.request import get_request
18

    
19
from forms import NewAppointmentForm, NewEventForm
20

    
21
def redirect_today(request, service):
22
    '''If not date is given we redirect on the agenda for today'''
23
    return redirect('agenda', date=datetime.date.today().strftime('%Y-%m-%d'),
24
            service=service)
25

    
26
class AgendaHomepageView(TemplateView):
27

    
28
    template_name = 'agenda/index.html'
29

    
30
    def get_context_data(self, **kwargs):
31
        context = super(AgendaHomepageView, self).get_context_data(**kwargs)
32

    
33
        weekday_mapping = {
34
                '0': u'dimanche',
35
                '1': u'lundi',
36
                '2': u'mardi',
37
                '3': u'mercredi',
38
                '4': u'jeudi',
39
                '5': u'vendredi',
40
                '6': u'samedi'
41
                }
42
        weekday = weekday_mapping[context['date'].strftime("%w")]
43
        time_tables = TimeTable.objects.select_related('worker').\
44
                filter(service=self.service).\
45
                filter(weekday=weekday).\
46
                filter(start_date__lte=context['date']).\
47
                filter(Q(end_date=None) |Q(end_date__gte=context['date'])).\
48
                order_by('start_date')
49
        occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
50

    
51
        context['workers_types'] = []
52
        context['workers_agenda'] = []
53
        context['disponnibility'] = {}
54
        workers = []
55
        for worker_type in WorkerType.objects.all():
56
            data = {'type': worker_type.name, 'workers': Worker.objects.for_service(self.service, worker_type) }
57
            context['workers_types'].append(data)
58
            workers.extend(data['workers'])
59

    
60
        occurrences_workers = {}
61
        time_tables_workers = {}
62
        for worker in workers:
63
            time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
64
            occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
65
            occurrences_workers[worker.id] = occurrences_worker
66
            time_tables_workers[worker.id] = time_tables_worker
67
            context['workers_agenda'].append({'worker': worker,
68
                    'appointments': get_daily_appointments(context['date'], worker, self.service,
69
                        time_tables_worker, occurrences_worker)})
70

    
71
        context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'],
72
                occurrences_workers, workers, time_tables_workers)
73
        return context
74

    
75
class AgendaServiceActivityView(TemplateView):
76

    
77
    template_name = 'agenda/service-activity.html'
78

    
79
    def get_context_data(self, **kwargs):
80
        context = super(AgendaServiceActivityView, self).get_context_data(**kwargs)
81

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

    
111

    
112
class NewAppointmentView(CreateView):
113
    model = EventAct
114
    form_class = NewAppointmentForm
115
    template_name = 'agenda/nouveau-rdv.html'
116
    success_url = '..'
117

    
118
    def get_initial(self):
119
        initial = super(NewAppointmentView, self).get_initial()
120
        initial['date'] = self.kwargs.get('date')
121
        initial['participants'] = self.request.GET.getlist('participants')
122
        initial['time'] = self.request.GET.get('time')
123
        return initial
124

    
125
    def get_form_kwargs(self):
126
        kwargs = super(NewAppointmentView, self).get_form_kwargs()
127
        kwargs['service'] = self.service
128
        return kwargs
129

    
130
    def post(self, *args, **kwargs):
131
        return super(NewAppointmentView, self).post(*args, **kwargs)
132

    
133
class NewEventView(CreateView):
134
    model = Event
135
    form_class = NewEventForm
136
    template_name = 'agenda/new-event.html'
137
    success_url = '..'
138

    
139
    def get_initial(self):
140
        initial = super(NewEventView, self).get_initial()
141
        initial['date'] = self.kwargs.get('date')
142
        initial['participants'] = self.request.GET.getlist('participants')
143
        initial['time'] = self.request.GET.get('time')
144
        return initial
145

    
146
    def get_form_kwargs(self):
147
        kwargs = super(NewEventView, self).get_form_kwargs()
148
        #kwargs['service'] = self.service
149
        return kwargs
150

    
151
    def post(self, *args, **kwargs):
152
        return super(NewEventView, self).post(*args, **kwargs)
153

    
154
def new_appointment(request):
155
    pass
156

    
157
class AgendaServiceActValidationView(TemplateView):
158

    
159
    template_name = 'agenda/act-validation.html'
160

    
161
    def post(self, request, *args, **kwargs):
162
        return render_to_response(self.template_name, {},
163
                context_instance=None)
164

    
165
    def get_context_data(self, **kwargs):
166
        context = super(AgendaServiceActValidationView, self).get_context_data(**kwargs)
167
        day_locked = are_all_acts_of_the_day_locked(context['date'])
168
        authorized_lock = True # is_authorized_for_locking(get_request().user)
169
        validation_msg = list()
170
        acts_of_the_day = get_acts_of_the_day(context['date'])
171
        actes = list()
172
        for act in acts_of_the_day:
173
            state = act.get_state()
174
            if not state.previous_state:
175
                state = None
176
            act.date = act.date.strftime("%H:%M")
177
            actes.append((act, state))
178
        context['validation_states'] = VALIDATION_STATES
179
        context['actes'] = actes
180
        context['validation_msg'] = validation_msg
181
        context['day_locked'] = day_locked
182
        context['authorized_lock'] = authorized_lock
183
        return context
184

    
185

    
186
class AutomatedValidationView(CreateView):
187
    pass
188

    
189
class UnlockAllView(CreateView):
190
    pass
(10-10/10)