Project

General

Profile

« Previous | Next » 

Revision 76974b6f

Added by Benjamin Dauvergne almost 12 years ago

agenda/actes/dossiers: move Occurence fields into Event, add recurring events support

View differences:

calebasse/agenda/views.py
7 7
from django.http import HttpResponseRedirect
8 8

  
9 9
from calebasse.cbv import TemplateView, CreateView, UpdateView
10
from calebasse.agenda.models import Occurrence, Event, EventType
10
from calebasse.agenda.models import Event, EventType, EventWithAct
11 11
from calebasse.personnes.models import TimeTable, Holiday
12
from calebasse.actes.models import EventAct
13 12
from calebasse.agenda.appointments import get_daily_appointments, get_daily_usage
14 13
from calebasse.personnes.models import Worker
15 14
from calebasse.ressources.models import WorkerType, Room
16
from calebasse.actes.validation import get_acts_of_the_day
15
from calebasse.actes.validation import (get_acts_of_the_day,
16
        get_days_with_acts_not_locked)
17 17
from calebasse.actes.validation_states import VALIDATION_STATES
18 18
from calebasse.actes.models import Act, ValidationMessage
19
from calebasse.actes.validation import (automated_validation,
20
    unlock_all_acts_of_the_day, are_all_acts_of_the_day_locked)
19
from calebasse.actes.validation import (automated_validation, unlock_all_acts_of_the_day)
21 20
from calebasse import cbv
22 21

  
23
from forms import (NewAppointmentForm, NewEventForm,
24
        UpdateAppointmentForm, UpdateEventForm)
22
from forms import (NewAppointmentForm, NewEventForm, UpdateAppointmentForm)
25 23

  
26 24
def redirect_today(request, service):
27 25
    '''If not date is given we redirect on the agenda for today'''
......
70 68
        appointments_times = dict()
71 69
        appoinment_type = EventType.objects.get(id=1)
72 70
        meeting_type = EventType.objects.get(id=2)
73
        occurrences = Occurrence.objects.daily_occurrences(context['date'],
74
                services=[self.service],
75
                event_type=[appoinment_type, meeting_type])
76
        for occurrence in occurrences:
77
            start_time = occurrence.start_time.strftime("%H:%M")
78
            if not appointments_times.has_key(start_time):
79
                appointments_times[start_time] = dict()
80
                appointments_times[start_time]['row'] = 0
81
                appointments_times[start_time]['appointments'] = []
71
        plain_events = Event.objects.for_today(self.date) \
72
                .filter(services=self.service,
73
                        event_type__in=[appoinment_type, meeting_type])
74
        events = [ e.today_occurence(self.date) for e in plain_events ]
75
        for event in events:
76
            start_datetime = event.start_datetime.strftime("%H:%M")
77
            if not appointments_times.has_key(start_datetime):
78
                appointments_times[start_datetime] = dict()
79
                appointments_times[start_datetime]['row'] = 0
80
                appointments_times[start_datetime]['appointments'] = []
82 81
            appointment = dict()
83
            length = occurrence.end_time - occurrence.start_time
82
            length = event.end_datetime - event.start_datetime
84 83
            if length.seconds:
85 84
                length = length.seconds / 60
86 85
                appointment['length'] = "%sm" % length
87
            if occurrence.event.event_type == EventType.objects.get(id=1):
86
            if event.event_type == EventType.objects.get(id=1):
88 87
                appointment['type'] = 1
89
                event_act = occurrence.event.eventact
88
                event_act = event.act
90 89
                appointment['label'] = event_act.patient.display_name
91 90
                appointment['act'] = event_act.act_type.name
92
            elif occurrence.event.event_type == EventType.objects.get(id=2):
91
            elif event.event_type == EventType.objects.get(id=2):
93 92
                appointment['type'] = 2
94
                appointment['label'] = '%s - %s' % (occurrence.event.event_type.label,
95
                                                    occurrence.event.title)
93
                appointment['label'] = '%s - %s' % (event.event_type.label,
94
                                                    event.title)
96 95
            else:
97 96
                appointment['type'] = 0
98 97
                appointment['label'] = '???'
99
            appointment['participants'] = occurrence.event.participants.all()
100
            appointments_times[start_time]['row'] += 1
101
            appointments_times[start_time]['appointments'].append(appointment)
98
            appointment['participants'] = event.participants.all()
99
            appointments_times[start_datetime]['row'] += 1
100
            appointments_times[start_datetime]['appointments'].append(appointment)
102 101
        context['appointments_times'] = sorted(appointments_times.items())
103 102
        return context
104 103

  
105 104

  
106 105
class NewAppointmentView(cbv.ReturnToObjectMixin, cbv.ServiceFormMixin, CreateView):
107
    model = EventAct
106
    model = EventWithAct
108 107
    form_class = NewAppointmentForm
109 108
    template_name = 'agenda/nouveau-rdv.html'
110 109
    success_url = '..'
......
119 118

  
120 119

  
121 120
class UpdateAppointmentView(UpdateView):
122
    model = EventAct
121
    model = EventWithAct
123 122
    form_class = UpdateAppointmentForm
124 123
    template_name = 'agenda/update-rdv.html'
125 124
    success_url = '..'
126 125

  
127
    def get_object(self, queryset=None):
128
        self.occurrence = Occurrence.objects.get(id=self.kwargs['id'])
129
        if self.occurrence.event.eventact:
130
            return self.occurrence.event.eventact
131

  
132 126
    def get_initial(self):
133 127
        initial = super(UpdateView, self).get_initial()
134
        initial['date'] = self.object.date
135
        initial['time'] = self.occurrence.start_time.strftime("%H:%M")
136
        time = self.occurrence.end_time - self.occurrence.start_time
128
        initial['date'] = self.object.start_datetime.strftime("%Y-%m-%d")
129
        initial['time'] = self.object.start_datetime.strftime("%H:%M")
130
        time = self.object.end_datetime - self.object.start_datetime
137 131
        if time:
138 132
            time = time.seconds / 60
139 133
        else:
......
144 138

  
145 139
    def get_form_kwargs(self):
146 140
        kwargs = super(UpdateAppointmentView, self).get_form_kwargs()
147
        kwargs['occurrence'] = self.occurrence
148 141
        kwargs['service'] = self.service
149 142
        return kwargs
150 143

  
......
165 158
        initial['room'] = self.request.GET.get('room')
166 159
        return initial
167 160

  
161

  
168 162
class UpdateEventView(UpdateView):
169 163
    model = Event
170
    form_class = UpdateEventForm
164
    form_class = NewEventForm
171 165
    template_name = 'agenda/update-event.html'
172 166
    success_url = '..'
173 167

  
174
    def get_object(self, queryset=None):
175
        self.occurrence = Occurrence.objects.get(id=self.kwargs['id'])
176
        return self.occurrence.event
177

  
178 168
    def get_initial(self):
179 169
        initial = super(UpdateEventView, self).get_initial()
180
        initial['date'] = self.occurrence.start_time.date()
181
        initial['time'] = self.occurrence.start_time.strftime("%H:%M")
182
        time = self.occurrence.end_time - self.occurrence.start_time
170
        initial['date'] = self.object.start_datetime.strftime("%Y-%m-%d")
171
        initial['time'] = self.object.start_datetime.strftime("%H:%M")
172
        time = self.object.end_datetime - self.object.start_datetime
183 173
        if time:
184 174
            time = time.seconds / 60
185 175
        else:
......
188 178
        initial['participants'] = self.object.participants.values_list('id', flat=True)
189 179
        return initial
190 180

  
191
    def get_form_kwargs(self):
192
        kwargs = super(UpdateEventView, self).get_form_kwargs()
193
        kwargs['occurrence'] = self.occurrence
194
        return kwargs
195

  
196
def new_appointment(request):
197
    pass
198 181

  
199 182
class AgendaServiceActValidationView(TemplateView):
200

  
201 183
    template_name = 'agenda/act-validation.html'
202 184

  
203 185
    def acts_of_the_day(self):
......
299 281

  
300 282

  
301 283
class AgendasTherapeutesView(AgendaHomepageView):
302

  
303 284
    template_name = 'agenda/agendas-therapeutes.html'
304 285

  
305 286
    def get_context_data(self, **kwargs):
......
312 293
        holidays = Holiday.objects.select_related('worker'). \
313 294
                for_period(self.date, self.date). \
314 295
                order_by('start_date')
315
        occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
296
        plain_events = Event.objects.for_today(self.date) \
297
                .order_by('start_datetime').select_subclasses()
298
        events = [ e.today_occurence(self.date) for e in plain_events ]
316 299

  
317
        occurrences_workers = {}
300
        events_workers = {}
318 301
        time_tables_workers = {}
319 302
        holidays_workers = {}
320 303
        context['workers_agenda'] = []
321 304
        for worker in context['workers']:
322 305
            time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
323
            occurrences_worker = [o for o in occurrences if worker.id in o.event.participants.values_list('id', flat=True)]
306
            events_worker = [o for o in events if worker.id in o.participants.values_list('id', flat=True)]
324 307
            holidays_worker = [h for h in holidays if h.worker_id in (None, worker.id)]
325
            occurrences_workers[worker.id] = occurrences_worker
308
            events_workers[worker.id] = events_worker
326 309
            time_tables_workers[worker.id] = time_tables_worker
327 310
            holidays_workers[worker.id] = holidays_worker
328 311
            context['workers_agenda'].append({'worker': worker,
329 312
                    'appointments': get_daily_appointments(context['date'], worker, self.service,
330
                        time_tables_worker, occurrences_worker, holidays_worker)})
313
                        time_tables_worker, events_worker, holidays_worker)})
331 314

  
332 315
        for worker_agenda in context.get('workers_agenda', []):
333 316
            patient_appointments = [x for x in worker_agenda['appointments'] if x.patient_record_id]
......
342 325
        return context
343 326

  
344 327
class JoursNonVerrouillesView(TemplateView):
345

  
346 328
    template_name = 'agenda/days-not-locked.html'
347 329

  
348 330
    def get_context_data(self, **kwargs):
349 331
        context = super(JoursNonVerrouillesView, self).get_context_data(**kwargs)
350
        acts = EventAct.objects.filter(is_billed=False,
332
        acts = Act.objects.filter(is_billed=False,
351 333
            patient__service=self.service).order_by('date')
352
        days_not_locked = []
353
        for act in acts:
354
            current_day = datetime.datetime(act.date.year, act.date.month, act.date.day)
355
            if not current_day in days_not_locked:
356
                locked = are_all_acts_of_the_day_locked(current_day, self.service)
357
                if not locked:
358
                    days_not_locked.append(current_day)
359
        context['days_not_locked'] = days_not_locked
334
        days = set(acts.values_list('date', flat=True))
335
        max_day, min_day = max(days), min(days)
336
        days &= set(get_days_with_acts_not_locked(min_day, max_day, self.service))
337
        context['days_not_locked'] = days
360 338
        return context
361 339

  
362 340
class RessourcesView(TemplateView):
......
366 344
    def get_context_data(self, **kwargs):
367 345
        context = super(RessourcesView, self).get_context_data(**kwargs)
368 346

  
369
        occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
347
        plain_events = Event.objects.for_today(self.date) \
348
                .order_by('start_datetime').select_subclasses()
349
        events = [ e.today_occurence(self.date) for e in plain_events ]
370 350

  
371 351
        context['ressources_types'] = []
372 352
        context['ressources_agenda'] = []
......
376 356
        context['ressources_types'].append(data)
377 357
        ressources.extend(data['ressources'])
378 358

  
379
        occurrences_ressources = {}
359
        events_ressources = {}
380 360
        for ressource in ressources:
381
            occurrences_ressource = [o for o in occurrences if ressource == o.event.room]
382
            occurrences_ressources[ressource.id] = occurrences_ressource
361
            events_ressource = [e for e in events if ressource == e.room]
362
            events_ressources[ressource.id] = events_ressource
383 363
            context['ressources_agenda'].append({'ressource': ressource,
384 364
                    'appointments': get_daily_usage(context['date'], ressource,
385
                        self.service, occurrences_ressource)})
365
                        self.service, events_ressource)})
386 366

  
387 367
        return context
388 368

  
......
401 381
        holidays_worker = Holiday.objects.for_worker_id(worker_id). \
402 382
                for_period(self.date, self.date). \
403 383
                order_by('start_date')
404
        occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
405
        occurrences_worker = [o for o in occurrences if worker_id in o.event.participants.values_list('id', flat=True)]
384
        plain_events = Event.objects.for_today(self.date) \
385
                .order_by('start_datetime').select_subclasses()
386
        events = [ e.today_occurence(self.date) for e in plain_events ]
387
        events_worker = [e for e in events if worker_id in e.participants.values_list('id', flat=True)]
406 388

  
407 389
        worker = Worker.objects.get(pk=worker_id)
408 390
        context['worker_agenda'] = {'worker': worker,
409 391
                    'appointments': get_daily_appointments(context['date'], worker, self.service,
410
                        time_tables_worker, occurrences_worker, holidays_worker)}
392
                        time_tables_worker, events_worker, holidays_worker)}
411 393
        return context
412 394

  
413 395
class AjaxWorkerDisponibilityColumnView(TemplateView):
......
425 407
        holidays_worker = Holiday.objects.for_worker_id(worker_id). \
426 408
                for_period(self.date, self.date). \
427 409
                order_by('start_date')
428
        occurrences = Occurrence.objects.daily_occurrences(context['date']).order_by('start_time')
429
        occurrences_worker = [o for o in occurrences if worker_id in o.event.participants.values_list('id', flat=True)]
410
        events = Event.objects.today_occurences(self.date)
411
        events_worker = [e for e in events if worker_id in e.participants.values_list('id', flat=True)]
430 412

  
431 413
        worker = Worker.objects.get(pk=worker_id)
432 414
        time_tables_workers = {worker.id: time_tables_worker}
433
        occurrences_workers = {worker.id: occurrences_worker}
415
        events_workers = {worker.id: events_worker}
434 416
        holidays_workers = {worker.id: holidays_worker}
435 417

  
436 418
        context['initials'] = worker.get_initials()
437 419
        context['worker_id'] = worker.id
438
        context['disponibility'] = Occurrence.objects.daily_disponibility(context['date'],
439
                occurrences_workers, [worker], time_tables_workers, holidays_workers)
420
        context['disponibility'] = Event.objects.daily_disponibilities(self.date,
421
                events_workers, [worker], time_tables_workers, holidays_workers)
440 422
        return context

Also available in: Unified diff