Projet

Général

Profil

Télécharger (32 ko) Statistiques
| Branche: | Tag: | Révision:

calebasse / calebasse / agenda / views.py @ 5e2377b1

1
# -*- coding: utf-8 -*-
2

    
3
import datetime
4
import logging
5
from itertools import chain
6

    
7
from django.contrib import messages
8
from django.db.models import Q
9
from django.shortcuts import redirect, get_object_or_404
10
from django.http import HttpResponseRedirect, HttpResponse, Http404
11
from django.conf import settings
12

    
13
from calebasse.cbv import TemplateView, CreateView, UpdateView
14
from calebasse.agenda.models import Event, EventType, EventWithAct
15
from calebasse.personnes.models import TimeTable, Holiday
16
from calebasse.agenda.appointments import get_daily_appointments, get_daily_usage
17
from calebasse.personnes.models import Worker
18
from calebasse.ressources.models import WorkerType, Ressource
19
from calebasse.actes.validation import (get_acts_of_the_day,
20
        get_days_with_acts_not_locked)
21
from calebasse.actes.validation_states import VALIDATION_STATES
22
from calebasse.actes.models import Act, ValidationMessage
23
from calebasse.actes.validation import (automated_validation, unlock_all_acts_of_the_day)
24
from calebasse import cbv
25

    
26
from calebasse.agenda.forms import (NewAppointmentForm, NewEventForm, UpdatePeriodicAppointmentForm,
27
        DisablePatientAppointmentForm, UpdateAppointmentForm, UpdatePeriodicEventForm,
28
        UpdateEventForm, PeriodicEventsSearchForm)
29

    
30
logger = logging.getLogger(__name__)
31

    
32
def redirect_today(request, service):
33
    '''If not date is given we redirect on the agenda for today'''
34
    return redirect('agenda', date=datetime.date.today().strftime('%Y-%m-%d'),
35
            service=service)
36

    
37

    
38
class AgendaHomepageView(TemplateView):
39
    template_name = 'agenda/index.html'
40
    cookies_to_clear = []
41

    
42
    def post(self, request, *args, **kwargs):
43
        acte_id = request.POST.get('event-id')
44
        try:
45
            event = EventWithAct.objects.get(id=acte_id)
46
            event = event.today_occurrence(self.date)
47
            act = event.act
48
            if not act.validation_locked:
49
                state_name = request.POST.get('act_state')
50
                act.save()
51
                act.set_state(state_name, request.user)
52
        except Act.DoesNotExist:
53
            logger.warning('agenda homepage acte_id %d not found' % acte_id)
54
        return HttpResponseRedirect('#acte-frame-'+acte_id)
55

    
56
    def get_context_data(self, **kwargs):
57
        logger.info('AgendaHomepageView.get_context_data called')
58
        context = super(AgendaHomepageView, self).get_context_data(**kwargs)
59

    
60
        context['workers_types'] = []
61
        workers = Worker.objects.filter(enabled=True).select_related() \
62
                .prefetch_related('services')
63
        worker_by_type = {}
64
        for worker in workers:
65
            workers_for_type = worker_by_type.setdefault(worker.type, [])
66
            workers_for_type.append(worker)
67
        for worker_type, workers_for_type in worker_by_type.iteritems():
68
            context['workers_types'].append(
69
                    {'type': worker_type.name, 'workers': workers_for_type })
70
        context['workers'] = workers
71
        context['disponibility_start_times'] = range(8, 20)
72

    
73
        # ressources
74
        context['ressources_types'] = []
75
        data = {'type': Ressource._meta.verbose_name_plural,
76
                'ressources': Ressource.objects.all()}
77
        context['ressources_types'].append(data)
78

    
79
        return context
80

    
81
class AgendaServiceActivityView(TemplateView, cbv.ServiceViewMixin):
82
    template_name = 'agenda/service-activity.html'
83
    cookies_to_clear = [('agenda-tabs', ), ('active-agenda', ), ('last-ressource', )]
84

    
85
    def get_context_data(self, **kwargs):
86
        context = super(AgendaServiceActivityView, self).get_context_data(**kwargs)
87

    
88
        appointments_times = dict()
89
        events = Event.objects.for_today(self.date) \
90
                .exclude(event_type_id=1) \
91
                .filter(services=self.service) \
92
                .order_by('start_datetime', 'id') \
93
                .select_related() \
94
                .prefetch_related('participants', 'exceptions')
95
        eventswithact = EventWithAct.objects.for_today(self.date) \
96
                .filter(services=self.service) \
97
                .order_by('start_datetime', 'id') \
98
                .select_related() \
99
                .prefetch_related('participants', 'exceptions')
100
        events = [ e.today_occurrence(self.date) for e in events ] \
101
             + [ e.today_occurrence(self.date) for e in eventswithact ]
102
        for event in events:
103
            start_datetime = event.start_datetime.strftime("%H:%M")
104
            if not appointments_times.has_key(start_datetime):
105
                appointments_times[start_datetime] = dict()
106
                appointments_times[start_datetime]['row'] = 0
107
                appointments_times[start_datetime]['appointments'] = []
108
            appointment = dict()
109
            length = event.end_datetime - event.start_datetime
110
            if length.seconds:
111
                length = length.seconds / 60
112
                appointment['length'] = "%sm" % length
113
            if event.event_type_id == 1:
114
                appointment['type'] = 1
115
                appointment['label'] = event.patient.display_name
116
                appointment['paper_id'] = event.patient.paper_id
117
                appointment['act'] = event.act_type.name
118
                appointment['state'] = event.act.get_state()
119
                appointment['absent'] = event.act.is_absent()
120
            else:
121
                appointment['type'] = 2
122
                if event.event_type.label == 'Autre' and event.title:
123
                    appointment['label'] = event.title
124
                else:
125
                    appointment['label'] = '%s - %s' % (event.event_type.label,
126
                                                        event.title)
127
            appointment['participants'] = event.participants.filter(worker__enabled=True)
128
            appointment['len_participants'] = len(appointment['participants'])
129
            appointment['workers_absent'] = event.get_missing_participants()
130
            appointments_times[start_datetime]['row'] += 1
131
            appointments_times[start_datetime]['appointments'].append(appointment)
132
        context['appointments_times'] = sorted(appointments_times.items())
133
        return context
134

    
135

    
136
class NewAppointmentView(cbv.ServiceFormMixin, CreateView):
137
    model = EventWithAct
138
    form_class = NewAppointmentForm
139
    template_name = 'agenda/new-appointment.html'
140
    success_msg = u'Rendez-vous enregistré avec succès.'
141
    cookies_to_clear = []
142

    
143
    def get_initial(self):
144
        initial = super(NewAppointmentView, self).get_initial()
145
        initial['start_datetime'] = self.date
146
        initial['date'] = self.date
147
        initial['participants'] = self.request.GET.getlist('participants')
148
        initial['time'] = self.request.GET.get('time')
149
        initial['ressource'] = self.request.GET.get('ressource')
150
        initial['duration'] = self.request.GET.get('duration')
151
        return initial
152

    
153
    def get_success_url(self):
154
        return self.request.META.get('HTTP_REFERER', '..')
155

    
156
    def form_valid(self, form):
157
        obj = super(NewAppointmentView, self).form_valid(form)
158
        messages.add_message(self.request, messages.INFO, self.success_msg)
159
        return obj
160

    
161

    
162
class TodayOccurrenceMixin(object):
163
    def get_object(self, queryset=None):
164
        o = super(TodayOccurrenceMixin, self).get_object(queryset)
165
        obj = o.today_occurrence(self.date)
166
        if obj:
167
            return obj
168
        raise Http404
169

    
170

    
171
class BaseAppointmentView(UpdateView):
172
    model = EventWithAct
173
    form_class = UpdateAppointmentForm
174
    template_name = 'agenda/update-rdv.html'
175
    success_url = '..'
176
    cookies_to_clear = []
177

    
178
    def get_initial(self):
179
        initial = super(BaseAppointmentView, self).get_initial()
180
        initial['start_datetime'] = self.date
181
        initial['date'] = self.object.start_datetime.date()
182
        initial['time'] = self.object.start_datetime.time()
183
        time = self.object.end_datetime - self.object.start_datetime
184
        if time:
185
            time = time.seconds / 60
186
        else:
187
            time = 0
188
        initial['duration'] = time
189
        initial['participants'] = self.object.participants.values_list('id', flat=True)
190
        return initial
191

    
192
    def get_form_kwargs(self):
193
        kwargs = super(BaseAppointmentView, self).get_form_kwargs()
194
        kwargs['service'] = self.service
195
        return kwargs
196

    
197

    
198
class UpdateAppointmentView(TodayOccurrenceMixin, BaseAppointmentView):
199

    
200
    def get_form_class(self):
201
        if self.object.exception_to and not self.object.exception_to.canceled:
202
            return DisablePatientAppointmentForm
203
        else:
204
            return self.form_class
205

    
206
class UpdatePeriodicAppointmentView(BaseAppointmentView):
207
    form_class = UpdatePeriodicAppointmentForm
208
    template_name = 'agenda/new-appointment.html'
209

    
210

    
211
class NewEventView(CreateView):
212
    model = Event
213
    form_class = NewEventForm
214
    template_name = 'agenda/new-event.html'
215
    cookies_to_clear = []
216

    
217
    def get_initial(self):
218
        initial = super(NewEventView, self).get_initial()
219
        initial['start_datetime'] = self.date
220
        initial['date'] = self.date
221
        initial['participants'] = self.request.GET.getlist('participants')
222
        initial['time'] = self.request.GET.get('time')
223
        initial['event_type'] = 2
224
        initial['ressource'] = self.request.GET.get('ressource')
225
        initial['duration'] = self.request.GET.get('duration')
226
        if not initial.has_key('services'):
227
            initial['services'] = [self.service]
228
        return initial
229

    
230
    def get_form_kwargs(self):
231
        kwargs = super(NewEventView, self).get_form_kwargs()
232
        kwargs['service'] = self.service
233
        return kwargs
234

    
235
    def get_success_url(self):
236
        return self.request.META.get('HTTP_REFERER', '..')
237

    
238
    def form_valid(self, form):
239
        messages.add_message(self.request, messages.INFO, u'Evénement enregistré avec succès.')
240
        return super(NewEventView, self).form_valid(form)
241

    
242
class BaseEventView(UpdateView):
243
    model = Event
244
    form_class = UpdateEventForm
245
    template_name = 'agenda/update-event.html'
246
    success_url = '..'
247
    cookies_to_clear = []
248

    
249
    def get_initial(self):
250
        initial = super(BaseEventView, self).get_initial()
251
        initial['start_datetime'] = self.date
252
        initial['date'] = self.object.start_datetime.date()
253
        initial['time'] = self.object.start_datetime.time()
254
        time = self.object.end_datetime - self.object.start_datetime
255
        if time:
256
            time = time.seconds / 60
257
        else:
258
            time = 0
259
        initial['duration'] = time
260
        initial['participants'] = self.object.participants.values_list('id', flat=True)
261
        return initial
262

    
263
    def get_form_kwargs(self):
264
        kwargs = super(BaseEventView, self).get_form_kwargs()
265
        kwargs['service'] = self.service
266
        return kwargs
267

    
268

    
269
class UpdateEventView(TodayOccurrenceMixin, BaseEventView):
270
    pass
271

    
272

    
273
class UpdatePeriodicEventView(BaseEventView):
274
    form_class = UpdatePeriodicEventForm
275
    template_name = 'agenda/new-event.html'
276

    
277
def delete_eventwithact(event):
278
    assert event.event_type_id == 1
279

    
280
    # in case of "event" is an instance of "Event" model and not "EventWithAct"
281
    # and so doesn't have 'act' attribute
282
    try:
283
        if event.act.id \
284
           and not event.act.is_billed:
285
            event.act.delete()
286

    
287
        if not event.act.id or \
288
           not event.act.is_billed:
289
            event.delete()
290
    except AttributeError:
291
        event.delete()
292

    
293
class DeleteOccurrenceView(TodayOccurrenceMixin, cbv.DeleteView):
294
    model = Event
295
    success_url = '..'
296
    cookies_to_clear = []
297

    
298
    def delete(self, request, *args, **kwargs):
299
        self.object = self.get_object()
300
        if self.object.event_type_id == 1:
301
            delete_eventwithact(self.object)
302
        else:
303
            self.object.delete()
304

    
305
        return HttpResponse(status=204)
306

    
307
class DeleteEventView(cbv.DeleteView):
308
    model = Event
309
    success_url = '..'
310
    cookies_to_clear = []
311

    
312
    def delete(self, request, *args, **kwargs):
313
        self.object = self.get_object()
314
        # If exceptions remove them only if act is not billed
315
        for exception in self.object.exceptions.all():
316
            exception.recurrence_periodicity = None
317
            exception.exception_to = None
318
            exception.save()
319
            if exception.event_type_id == 1:
320
                delete_eventwithact(exception)
321
            else:
322
                exception.delete()
323

    
324
        if self.object.event_type_id == 1:
325
            delete_eventwithact(self.object)
326
        else:
327
            self.object.delete()
328
        return HttpResponse(status=204)
329

    
330
class AgendaServiceActValidationView(TemplateView):
331
    template_name = 'agenda/act-validation.html'
332
    cookies_to_clear = [('agenda-tabs', ), ('active-agenda', ), ('last-ressource', )]
333

    
334
    def acts_of_the_day(self):
335
        acts = list(Act.objects \
336
                .filter(date=self.date, patient__service=self.service) \
337
                .select_related() \
338
                .prefetch_related('doctors',
339
                        'patient__patientcontact',
340
                        'actvalidationstate_set__previous_state') \
341
                .order_by('time'))
342
        event_ids = [ a.parent_event_id for a in acts if a.parent_event_id ]
343
        events = EventWithAct.objects.for_today(self.date) \
344
                .filter(patient__service=self.service) \
345
                .exclude(id__in=event_ids)
346
        events = [ event.today_occurrence(self.date) for event in events ]
347
        acts += [ event.act for event in events if event ]
348
        return sorted(acts, key=lambda a: (a.time or datetime.time.min, a.id))
349

    
350
    def post(self, request, *args, **kwargs):
351
        if 'unlock-all' in request.POST:
352
            #TODO: check that the user is authorized
353
            unlock_all_acts_of_the_day(self.date, self.service)
354
            ValidationMessage(validation_date=self.date,
355
                who=request.user, what='Déverrouillage',
356
                service=self.service).save()
357
        else:
358
            acte_id = request.POST.get('acte-id')
359
            try:
360
                act = Act.objects.get(id=acte_id)
361
                if 'lock' in request.POST or 'unlock' in request.POST:
362
                    #TODO: check that the user is authorized
363
                    act.validation_locked = 'lock' in request.POST
364
                    act.save()
365
                else:
366
                    state_name = request.POST.get('act_state')
367
                    act.set_state(state_name, request.user)
368
            except Act.DoesNotExist:
369
                pass
370
            return HttpResponseRedirect('#acte-frame-'+acte_id)
371
        return HttpResponseRedirect('')
372

    
373
    def get_context_data(self, **kwargs):
374
        context = super(AgendaServiceActValidationView, self).get_context_data(**kwargs)
375
        authorized_lock = True # is_authorized_for_locking(get_request().user)
376
        validation_msg = ValidationMessage.objects.\
377
            filter(validation_date=self.date, service=self.service).\
378
            order_by('-when')[:3]
379
        acts_of_the_day = self.acts_of_the_day()
380
        actes = list()
381
        for act in acts_of_the_day:
382
            if not act.id:
383
                if act.date < datetime.date(2013, 1, 1):
384
                    continue
385
                else:
386
                    act.save()
387
            state = act.get_state()
388
            display_name = None
389
            if state :
390
                display_name = VALIDATION_STATES[state.state_name]
391
                if not state.previous_state_id and state.state_name == 'NON_VALIDE':
392
                    state = None
393
            actes.append((act, state, display_name))
394
        validation_states = dict(VALIDATION_STATES)
395
        if self.service.name != 'CMPP' and \
396
                'ACT_DOUBLE' in validation_states:
397
            validation_states.pop('ACT_DOUBLE')
398
        vs = [('VALIDE', 'Présent')]
399
        validation_states.pop('VALIDE')
400
        validation_states.pop('ACT_LOST')
401
        validation_states = vs + sorted(validation_states.items(), key=lambda tup: tup[0])
402
        context['validation_states'] = validation_states
403
        context['actes'] = actes
404
        context['validation_msg'] = validation_msg
405
        context['authorized_lock'] = authorized_lock
406
        return context
407

    
408

    
409
class AutomatedValidationView(TemplateView):
410
    template_name = 'agenda/automated-validation.html'
411

    
412
    def post(self, request, *args, **kwargs):
413
        automated_validation(self.date, self.service,
414
            request.user)
415
        ValidationMessage(validation_date=self.date,
416
            who=request.user, what='Validation automatique',
417
            service=self.service).save()
418
        return HttpResponseRedirect('..')
419

    
420
    def get_context_data(self, **kwargs):
421
        context = super(AutomatedValidationView, self).get_context_data(**kwargs)
422
        request = self.request
423
        (nb_acts_total, nb_acts_validated, nb_acts_double,
424
        nb_acts_abs_non_exc, nb_acts_abs_exc, nb_acts_abs_inter, nb_acts_annul_nous,
425
        nb_acts_annul_famille, nb_acts_reporte, nb_acts_abs_ess_pps,
426
        nb_acts_enf_hosp, nb_acts_losts) = \
427
            automated_validation(self.date, self.service,
428
                request.user, commit = False)
429

    
430
        nb_acts_not_validated = nb_acts_double + \
431
            nb_acts_abs_non_exc + \
432
            nb_acts_abs_exc + \
433
            nb_acts_abs_inter + \
434
            nb_acts_annul_nous + \
435
            nb_acts_annul_famille + \
436
            nb_acts_reporte + \
437
            nb_acts_abs_ess_pps + \
438
            nb_acts_enf_hosp + \
439
            nb_acts_losts
440
        context.update({
441
            'nb_acts_total': nb_acts_total,
442
            'nb_acts_validated': nb_acts_validated,
443
            'nb_acts_not_validated': nb_acts_not_validated,
444
            'nb_acts_double': nb_acts_double,
445
            'nb_acts_abs_non_exc': nb_acts_abs_non_exc,
446
            'nb_acts_abs_exc': nb_acts_abs_exc,
447
            'nb_acts_abs_inter': nb_acts_abs_inter,
448
            'nb_acts_annul_nous': nb_acts_annul_nous,
449
            'nb_acts_annul_famille': nb_acts_annul_famille,
450
            'nb_acts_reporte': nb_acts_reporte,
451
            'nb_acts_abs_ess_pps': nb_acts_abs_ess_pps,
452
            'nb_acts_enf_hosp': nb_acts_enf_hosp,
453
            'nb_acts_losts': nb_acts_losts})
454
        return context
455

    
456
class UnlockAllView(CreateView):
457
    pass
458

    
459

    
460
class AgendasTherapeutesView(AgendaHomepageView):
461
    template_name = 'agenda/agendas-therapeutes.html'
462
    cookies_to_clear = [('agenda-tabs', ), ('active-agenda', ), ('last-ressource', )]
463

    
464
    def get_context_data(self, **kwargs):
465
        context = super(AgendasTherapeutesView, self).get_context_data(**kwargs)
466

    
467
        time_tables = TimeTable.objects.select_related('worker'). \
468
                filter(services=self.service). \
469
                for_today(self.date). \
470
                order_by('start_date')
471
        holidays = Holiday.objects.select_related('worker') \
472
                .for_period(self.date, self.date) \
473
                .order_by('start_date') \
474
                .select_related()
475
        events = Event.objects.for_today(self.date) \
476
                .exclude(event_type_id=1) \
477
                .filter(services=self.service) \
478
                .order_by('start_datetime') \
479
                .select_related() \
480
                .prefetch_related('services',
481
                        'exceptions',
482
                        'participants')
483
        eventswithact = EventWithAct.objects.for_today(self.date) \
484
                .filter(services=self.service) \
485
                .order_by('start_datetime') \
486
                .select_related() \
487
                .prefetch_related(
488
                        'services',
489
                        'patient__service',
490
                        'act_set__actvalidationstate_set',
491
                        'exceptions', 'participants')
492

    
493
        context['CURRENT_SERVICE_EVENTS_ONLY'] = settings.CURRENT_SERVICE_EVENTS_ONLY
494

    
495
        events = [ e.today_occurrence(self.date) for e in events ] \
496
             + [ e.today_occurrence(self.date) for e in eventswithact ]
497
        for e in events:
498
            e.workers_ids = set(p.id for p in e.participants.all())
499

    
500
        events_workers = {}
501
        time_tables_workers = {}
502
        holidays_workers = {}
503
        context['workers_agenda'] = []
504
        context['workers'] = context['workers'].filter(services=self.service)
505
        for worker in context['workers']:
506
            time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
507
            events_worker = [o for o in events if worker.id in o.workers_ids ]
508
            holidays_worker = [h for h in holidays if h.worker_id in (None, worker.id)]
509
            events_workers[worker.id] = events_worker
510
            time_tables_workers[worker.id] = time_tables_worker
511
            holidays_workers[worker.id] = holidays_worker
512
            daily_appointments = get_daily_appointments(context['date'], worker, self.service,
513
                        time_tables_worker, events_worker, holidays_worker)
514
            if all(map(lambda x: x.holiday, daily_appointments)):
515
                continue
516
            context['workers_agenda'].append({'worker': worker,
517
                    'appointments': daily_appointments,
518
                    'has_events': True if events_worker else False})
519

    
520
        for worker_agenda in context.get('workers_agenda', []):
521
            patient_appointments = [x for x in worker_agenda['appointments'] if x.patient_record_id]
522
            worker_agenda['summary'] = {
523
              'rdv': len(patient_appointments),
524
              'presence': len([x for x in patient_appointments if x.act_absence is None]),
525
              'absence': len([x for x in patient_appointments if x.act_absence is not None]),
526
              'doubles': len([x for x in patient_appointments if x.act_type == 'ACT_DOUBLE']),
527
              'valides': len([x for x in patient_appointments if x.act_type == 'ACT_VALIDE']),
528
            }
529

    
530
        return context
531

    
532
class JoursNonVerrouillesView(TemplateView):
533
    template_name = 'agenda/days-not-locked.html'
534

    
535
    def get_context_data(self, **kwargs):
536
        context = super(JoursNonVerrouillesView, self).get_context_data(**kwargs)
537
        acts = Act.objects.filter(is_billed=False,
538
            patient__service=self.service).order_by('date')
539
        days = set(acts.values_list('date', flat=True))
540
        if days:
541
            max_day, min_day = max(days), min(days)
542
            today = datetime.datetime.today().date()
543
            if max_day > today:
544
                max_day = today
545
            days &= set(get_days_with_acts_not_locked(min_day, max_day, self.service))
546
        context['days_not_locked'] = sorted(days)
547
        return context
548

    
549
class AjaxWorkerTabView(TemplateView):
550

    
551
    template_name = 'agenda/ajax-worker-tab.html'
552
    cookies_to_clear = []
553

    
554
    def get_context_data(self, worker_id, **kwargs):
555
        context = super(AjaxWorkerTabView, self).get_context_data(**kwargs)
556
        worker = Worker.objects.get(id=worker_id)
557

    
558
        time_tables_worker = TimeTable.objects.select_related('worker'). \
559
                filter(services=self.service, worker=worker) \
560
                .for_today(self.date) \
561
                .order_by('start_date') \
562
                .select_related()
563
        holidays_worker = Holiday.objects.for_worker(worker) \
564
                .for_period(self.date, self.date) \
565
                .order_by('start_date') \
566
                .select_related()
567
        events = Event.objects.for_today(self.date) \
568
                .exclude(event_type_id=1) \
569
                .filter(participants=worker) \
570
                .order_by('start_datetime') \
571
                .select_related() \
572
                .prefetch_related('services',
573
                        'exceptions',
574
                        'participants')
575
        eventswithact = EventWithAct.objects.for_today(self.date) \
576
                .filter(participants=worker) \
577
                .order_by('start_datetime') \
578
                .select_related() \
579
                .prefetch_related('patient__addresses',
580
                        'patient__addresses__patientcontact_set',
581
                        'services',
582
                        'patient__service',
583
                        'act_set__actvalidationstate_set',
584
                        'exceptions', 'participants')
585
        events = [ e.today_occurrence(self.date) for e in events ] \
586
             + [ e.today_occurrence(self.date) for e in eventswithact ]
587

    
588
        context['worker_agenda'] = {'worker': worker,
589
                    'appointments': get_daily_appointments(context['date'], worker, self.service,
590
                        time_tables_worker, events, holidays_worker)}
591

    
592
        if settings.RTF_TEMPLATES_DIRECTORY:
593
            context['mail'] = True
594
        return context
595

    
596
class AjaxRessourceTabView(TemplateView):
597
    template_name = 'agenda/ajax-ressource-tab.html'
598
    cookies_to_clear = []
599

    
600
    def get_context_data(self, ressource_id, **kwargs):
601
        context = super(AjaxRessourceTabView, self).get_context_data(**kwargs)
602
        ressource = Ressource.objects.get(pk=ressource_id)
603
        plain_events = Event.objects.for_today(self.date) \
604
                                    .order_by('start_datetime').select_subclasses()
605
        events = [ e.today_occurrence(self.date) for e in plain_events ]
606
        events_ressource = [e for e in events if ressource == e.ressource]
607
        context['ressource_agenda'] = {'appointments': get_daily_usage(context['date'],
608
                                                                       ressource,
609
                                                                       self.service,
610
                                                                       events_ressource)
611
        }
612
        return context
613

    
614
class AjaxDisponibilityColumnView(TemplateView):
615

    
616
    template_name = 'agenda/ajax-disponibility-column.html'
617
    cookies_to_clear = []
618

    
619
    def get_ressource_context_data(self, ressource_id, context):
620
        ressource = Ressource.objects.get(pk = ressource_id)
621
        context['initials'] = ressource.name[:3]
622
        disponibility = dict()
623
        start_datetime = datetime.datetime(self.date.year,
624
                                           self.date.month,
625
                                           self.date.day, 8, 0)
626
        end_datetime = datetime.datetime(self.date.year, self.date.month,
627
                                         self.date.day, 8, 15)
628
        events = Event.objects.filter(ressource__id=ressource_id).today_occurrences(self.date)
629

    
630
        while (start_datetime.hour <= 19):
631
            if start_datetime.hour not in disponibility:
632
                disponibility[start_datetime.hour] = [[], [], [], []]
633
                quarter = 0
634
            dispo = 'free'
635
            mins = quarter * 15
636

    
637
            if events:
638
                for event in events:
639
                    overlap_events = Event.objects.overlap_occurences(start_datetime, events)
640
                    if len(overlap_events) > 1:
641
                        dispo = 'overlap'
642
                    elif event.start_datetime <= start_datetime and event.end_datetime >= end_datetime:
643
                        dispo = 'busy'
644
            disponibility[start_datetime.hour][quarter].append((mins, {'id': ressource_id, 'dispo': dispo}))
645
            quarter += 1
646
            start_datetime += datetime.timedelta(minutes=15)
647
            end_datetime += datetime.timedelta(minutes=15)
648

    
649
        context['disponibility'] = disponibility
650
        return context
651

    
652

    
653
    def get_worker_context_data(self, worker_id, context):
654
        worker = Worker.objects.get(pk=worker_id)
655

    
656
        time_tables = TimeTable.objects.select_related('worker'). \
657
                filter(services=self.service, worker=worker). \
658
                for_today(self.date). \
659
                order_by('start_date')
660
        holidays = Holiday.objects.for_worker(worker). \
661
                for_period(self.date, self.date). \
662
                order_by('start_date')
663
        events = Event.objects.for_today(self.date) \
664
                .exclude(event_type_id=1) \
665
                .filter(participants=worker) \
666
                .order_by('start_datetime') \
667
                .select_related() \
668
                .prefetch_related('participants', 'exceptions')
669
        eventswithact = EventWithAct.objects.for_today(self.date) \
670
                .filter(participants=worker) \
671
                .order_by('start_datetime') \
672
                .select_related() \
673
                .prefetch_related('participants', 'exceptions',
674
                        'act_set__actvalidationstate_set')
675

    
676
        events = list(events) + list(eventswithact)
677
        events = [ e.today_occurrence(self.date) for e in events ]
678
        time_tables_workers = {worker.id: time_tables}
679
        events_workers = {worker.id: events}
680
        holidays_workers = {worker.id: holidays}
681

    
682
        context['initials'] = worker.initials
683
        context['disponibility'] = Event.objects.daily_disponibilities(self.date,
684
                events, worker, time_tables, holidays)
685
        return context
686

    
687
    def get_context_data(self, ressource_type, ressource_id, **kwargs):
688
        context = super(AjaxDisponibilityColumnView, self).get_context_data(**kwargs)
689
        if ressource_type in ('worker', 'ressource',):
690
            context['ressource_type'] = ressource_type
691
            context['ressource_id'] = ressource_id
692
            getattr(self, 'get_%s_context_data' % ressource_type)(ressource_id, context)
693
        return context
694

    
695
class PeriodicEventsView(cbv.ListView):
696
    model = EventWithAct
697
    template_name = 'agenda/periodic-events.html'
698
    cookies_to_clear = [('agenda-tabs', ), ('active-agenda', ), ('last-ressource', )]
699

    
700
    def dispatch(self, request, *args, **kwargs):
701
        if 'worker_id' in kwargs:
702
            self.worker = get_object_or_404(Worker, id=kwargs['worker_id'])
703
        else:
704
            self.worker = None
705
        return super(PeriodicEventsView, self).dispatch(request, *args, **kwargs)
706

    
707
    def get_form(self):
708
        kwargs = {
709
                'initial': {
710
                    'start_date': self.date,
711
                }
712
        }
713
        if self.request.GET:
714
            kwargs['data'] = self.request.GET
715
        self.form = PeriodicEventsSearchForm(prefix='periodic-events-search-form', **kwargs)
716
        return self.form
717

    
718
    def get_queryset(self):
719
        qs1 = Event.objects.exclude(event_type_id=1)
720
        qs2 = EventWithAct.objects.all()
721
        form = self.get_form()
722
        if not self.request.GET:
723
            return ()
724
        qs1 = self.filter_queryset(form, qs1)
725
        qs2 = self.filter_queryset(form, qs2)
726
        if form.is_valid():
727
            patient = form.cleaned_data.get('patient')
728
            if patient is not None:
729
                qs1 = qs1.none()
730
                qs2 = qs2.filter(patient=patient)
731
            worker = form.cleaned_data.get('worker')
732
            if worker is not None:
733
                qs1 = qs1.filter(participants=worker)
734
                qs2 = qs2.filter(participants=worker)
735
        return sorted(chain(qs1, qs2),
736
                key=lambda x: (x.start_datetime, x.recurrence_end_date or datetime.date(9999,12,31)))
737

    
738
    def filter_queryset(self, form, qs):
739
        if self.worker is not None:
740
            qs = qs.filter(participants=self.worker)
741
        start_date = datetime.date.today()
742
        end_date = start_date+datetime.timedelta(days=90)
743
        if form.is_valid():
744
            if form.cleaned_data.get('start_date'):
745
                start_date = form.cleaned_data['start_date']
746
            if form.cleaned_data.get('end_date'):
747
                end_date = form.cleaned_data['end_date']
748
            else:
749
                end_date = start_date+datetime.timedelta(days=90)
750
            if len(form.cleaned_data.get('event_type')) != 2:
751
                if '0' in form.cleaned_data.get('event_type'):
752
                    qs = qs.filter(event_type_id=1)
753
                else:
754
                    qs = qs.exclude(event_type_id=1)
755
            if form.cleaned_data.get('no_end_date'):
756
                qs = qs.filter(recurrence_end_date__isnull=True)
757
        qs = qs.filter(canceled=False)
758
        qs = qs.filter(services=self.service)
759
        qs = qs.filter(recurrence_periodicity__isnull=False)
760
        qs = qs.filter(start_datetime__lt=end_date)
761
        qs = qs.filter(Q(recurrence_end_date__isnull=True)
762
                | Q(recurrence_end_date__gte=start_date))
763
        qs = qs.order_by('start_datetime', 'recurrence_end_date')
764
        qs = qs.select_related()
765
        qs = qs.prefetch_related('participants', 'services')
766
        return qs
767

    
768
    def get_context_data(self, **kwargs):
769
        ctx = super(PeriodicEventsView, self).get_context_data(**kwargs)
770
        ctx['search_form'] = self.form
771
        ctx['worker'] = self.worker
772
        return ctx
(10-10/10)