Revision 2ff5e689
Added by Jérôme Schneider over 13 years ago
| calebasse/agenda/appointments.py | ||
|---|---|---|
|
from django.db.models import Q
|
||
|
from datetime import datetime, time
|
||
|
|
||
|
from interval import IntervalSet
|
||
|
|
||
|
from calebasse.actes.models import EventAct
|
||
|
from calebasse.agenda.models import Occurrence
|
||
|
from calebasse.personnes.models import TimeTable
|
||
| ... | ... | |
|
self.title = title
|
||
|
self.__set_time(time)
|
||
|
|
||
|
def _add_free_time(delta, appointments, begin_time):
|
||
|
if delta > 0:
|
||
|
delta_minutes = delta / 60
|
||
|
appointment = Appointment()
|
||
|
appointment.init_free_time(delta_minutes, begin_time)
|
||
|
appointments.append(appointment)
|
||
|
return appointments
|
||
|
|
||
|
def get_daily_appointments(date, worker, service):
|
||
|
def get_daily_appointments(date, worker, service, time_tables, occurrences):
|
||
|
"""
|
||
|
"""
|
||
|
appointments = []
|
||
|
weekday_mapping = {
|
||
|
'0': u'dimanche',
|
||
|
'1': u'lundi',
|
||
|
'2': u'mardi',
|
||
|
'3': u'mercredi',
|
||
|
'4': u'jeudi',
|
||
|
'5': u'vendredi',
|
||
|
'6': u'samedi'
|
||
|
}
|
||
|
weekday = weekday_mapping[date.strftime("%w")]
|
||
|
time_tables = TimeTable.objects.filter(worker=worker).\
|
||
|
filter(service=service).\
|
||
|
filter(weekday=weekday).\
|
||
|
filter(start_date__lte=date).\
|
||
|
filter(Q(end_date=None) |Q(end_date__gte=date)).\
|
||
|
order_by('start_date')
|
||
|
|
||
|
appointments = []
|
||
|
occurrences = Occurrence.objects.daily_occurrences(date, [worker]).order_by('start_time')
|
||
|
timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
|
||
|
occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
|
||
|
for free_time in timetables_set - occurrences_set:
|
||
|
if free_time:
|
||
|
delta = free_time.upper_bound - free_time.lower_bound
|
||
|
delta_minutes = delta.seconds / 60
|
||
|
appointment = Appointment()
|
||
|
appointment.init_free_time(delta_minutes,
|
||
|
time(free_time.lower_bound.hour, free_time.upper_bound.minute))
|
||
|
appointments.append(appointment)
|
||
|
for occurrence in occurrences:
|
||
|
appointment = Appointment()
|
||
|
appointment.init_from_occurrence(occurrence, service)
|
||
|
appointments.append(appointment)
|
||
|
# Find free times between occurrences in time_tables
|
||
|
next_occurrences = Occurrence.objects.filter(start_time__gte=occurrence.end_time).order_by('start_time')
|
||
|
if next_occurrences:
|
||
|
next_occurrence = next_occurrences[0]
|
||
|
if not Occurrence.objects.filter(end_time__gt=occurrence.end_time).filter(end_time__lt=next_occurrence.start_time):
|
||
|
for time_table in time_tables:
|
||
|
start_time_table = datetime(date.year, date.month, date.day,
|
||
|
time_table.start_time.hour, time_table.start_time.minute)
|
||
|
end_time_table = datetime(date.year, date.month, date.day,
|
||
|
time_table.end_time.hour, time_table.end_time.minute)
|
||
|
if (occurrence.end_time >= start_time_table) and (next_occurrence.start_time < end_time_table):
|
||
|
delta = next_occurrence.start_time - occurrence.end_time
|
||
|
appointments = _add_free_time(delta.seconds, appointments,
|
||
|
time(occurrence.end_time.hour, occurrence.end_time.minute))
|
||
|
|
||
|
for time_table in time_tables:
|
||
|
appointment = Appointment()
|
||
|
appointment.init_start_stop(u"Arrivée",
|
||
| ... | ... | |
|
appointment.init_start_stop(u"Départ",
|
||
|
time(time_table.end_time.hour, time_table.end_time.minute))
|
||
|
appointments.append(appointment)
|
||
|
start_datetime = datetime(date.year, date.month, date.day,
|
||
|
time_table.start_time.hour, time_table.start_time.minute)
|
||
|
end_datetime = datetime(date.year, date.month, date.day,
|
||
|
time_table.end_time.hour, time_table.end_time.minute)
|
||
|
smallest = Occurrence.objects.smallest_start_in_range(start_datetime, end_datetime, [worker])
|
||
|
biggest = Occurrence.objects.biggest_end_in_range(start_datetime, end_datetime, [worker])
|
||
|
if not smallest and not biggest:
|
||
|
delta = end_datetime - start_datetime
|
||
|
appointments = _add_free_time(delta.seconds, appointments,
|
||
|
time(start_datetime.hour, start_datetime.minute))
|
||
|
if smallest:
|
||
|
delta = smallest.start_time - start_datetime
|
||
|
appointments = _add_free_time(delta.seconds, appointments,
|
||
|
time(start_datetime.hour, start_datetime.minute))
|
||
|
if biggest:
|
||
|
delta = end_datetime - biggest.end_time
|
||
|
appointments = _add_free_time(delta.seconds, appointments,
|
||
|
time(biggest.end_time.hour, biggest.end_time.minute))
|
||
|
|
||
|
appointments = sorted(appointments, key=lambda app: app.begin_time)
|
||
|
return appointments
|
||
| calebasse/agenda/models.py | ||
|---|---|---|
|
|
||
|
def to_interval(self):
|
||
|
return Interval(self.start_time, self.end_time)
|
||
|
|
||
| calebasse/agenda/utils.py | ||
|---|---|---|
|
from interval import IntervalSet
|
||
|
|
||
|
def free_time(date, timetables, events):
|
||
|
timetables = IntervalSet((t.to_interval(date) for t in timetables))
|
||
|
events = IntervalSet((e.to_interval() for e in events))
|
||
|
for free_time in timetables - events:
|
||
|
yield free_time
|
||
| calebasse/agenda/views.py | ||
|---|---|---|
|
import datetime
|
||
|
|
||
|
from django.db.models import Q
|
||
|
from django.shortcuts import redirect
|
||
|
|
||
|
from calebasse.cbv import TemplateView, CreateView
|
||
|
from calebasse.agenda.models import Occurrence
|
||
|
from calebasse.personnes.models import TimeTable
|
||
|
from calebasse.actes.models import EventAct
|
||
|
from calebasse.agenda.appointments import get_daily_appointments
|
||
|
from calebasse.personnes.models import Worker
|
||
| ... | ... | |
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super(AgendaHomepageView, self).get_context_data(**kwargs)
|
||
|
|
||
|
weekday_mapping = {
|
||
|
'0': u'dimanche',
|
||
|
'1': u'lundi',
|
||
|
'2': u'mardi',
|
||
|
'3': u'mercredi',
|
||
|
'4': u'jeudi',
|
||
|
'5': u'vendredi',
|
||
|
'6': u'samedi'
|
||
|
}
|
||
|
weekday = weekday_mapping[context['date'].strftime("%w")]
|
||
|
time_tables = TimeTable.objects.select_related().\
|
||
|
filter(service=self.service).\
|
||
|
filter(weekday=weekday).\
|
||
|
filter(start_date__lte=context['date']).\
|
||
|
filter(Q(end_date=None) |Q(end_date__gte=context['date'])).\
|
||
|
order_by('start_date')
|
||
|
occurrences = Occurrence.objects.daily_occurrences(context['date']).select_related().order_by('start_time')
|
||
|
|
||
|
context['workers_types'] = []
|
||
|
context['workers_agenda'] = []
|
||
|
context['disponnibility'] = {}
|
||
|
workers = []
|
||
|
service = Service.objects.get(name=context['service_name'])
|
||
|
for worker_type in WorkerType.objects.all():
|
||
|
data = {'type': worker_type.name, 'workers': Worker.objects.for_service(self.service, worker_type) }
|
||
|
context['workers_types'].append(data)
|
||
|
workers.extend(data['workers'])
|
||
|
|
||
|
for worker in workers:
|
||
|
time_tables_worker = [tt for tt in time_tables if tt.worker.id == worker.id]
|
||
|
occurrences_worker = [o for o in occurrences for id in o.event.participants.values_list('id') if id[0] == worker.id]
|
||
|
context['workers_agenda'].append({'worker': worker,
|
||
|
'appointments': get_daily_appointments(context['date'], worker, service)})
|
||
|
'appointments': get_daily_appointments(context['date'], worker, self.service,
|
||
|
time_tables_worker, occurrences_worker)})
|
||
|
|
||
|
context['disponibility'] = Occurrence.objects.daily_disponiblity(context['date'], workers)
|
||
|
return context
|
||
| ... | ... | |
|
|
||
|
def new_appointment(request):
|
||
|
pass
|
||
|
|
||
| calebasse/fixtures/personnes.json | ||
|---|---|---|
|
"pk": 7,
|
||
|
"model": "personnes.people",
|
||
|
"fields": {
|
||
|
"first_name": " Lepoulpe",
|
||
|
"last_name": "Pol",
|
||
|
"display_name": " Lepoulpe Pol"
|
||
|
"first_name": "Pol",
|
||
|
"last_name": "Lepoulpe",
|
||
|
"display_name": "Pol Lepoulpe"
|
||
|
}
|
||
|
},
|
||
|
{
|
||
| scripts/import_db.py | ||
|---|---|---|
|
#!/usr/bin/env python
|
||
|
|
||
|
import ipdb
|
||
|
import os
|
||
|
import csv
|
||
|
|
||
Also available in: Unified diff
agenda: optimize appoinments rendering and use interval lib
sql request
appointments
Lepouple