Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 36d6d03e

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

    
3
from django.db.models import Q
4
from datetime import datetime, time
5

    
6
from interval import IntervalSet
7

    
8
from calebasse.actes.models import EventAct
9
from calebasse.agenda.models import Occurrence
10
from calebasse.personnes.models import TimeTable
11

    
12
class Appointment(object):
13

    
14
    def __init__(self, title=None, begin_time=None, type=None,
15
            length=None, description=None, room=None):
16
        """ """
17
        self.title = title
18
        self.type = type
19
        self.length = length
20
        self.description = description
21
        self.room = room
22
        self.convocation_sent = None
23
        self.service_name = None
24
        self.patient_record_id = None
25
        self.event_id = None
26
        self.occurrence_id = None
27
        self.service = None
28
        self.workers_initial = None
29
        self.weight = 0
30
        self.act_type = None
31
        self.__set_time(begin_time)
32

    
33
    def __set_time(self, time):
34
        self.begin_time = time
35
        if time:
36
            self.begin_hour = time.strftime("%H:%M")
37
        else:
38
            self.begin_hour = None
39

    
40
    def init_from_occurrence(self, occurrence, service):
41
        """ """
42
        delta = occurrence.end_time - occurrence.start_time
43
        self.occurrence_id = occurrence.id
44
        self.length = delta.seconds / 60
45
        self.title = occurrence.title
46
        services = occurrence.event.services.all()
47
        if services:
48
            self.service = services[0].name.lower()
49
        self.__set_time(time(occurrence.start_time.hour, occurrence.start_time.minute))
50
        if service in occurrence.event.services.all():
51
            self.type = "busy-here"
52
        else:
53
            self.type = "busy-elsewhere"
54
            self.service_name = service.name
55
        self.event_id = occurrence.event.id
56
        if occurrence.event.room:
57
            self.room = occurrence.event.room.name
58
        self.description = occurrence.event.description
59
        if occurrence.event.event_type.id == 1:
60
            event_act = occurrence.event.eventact
61
            workers = event_act.participants.all()
62
            self.convocation_sent = event_act.convocation_sent
63
            self.patient_record_id = event_act.patient.id
64
            self.workers_initial = ""
65
            for worker in workers:
66
                self.workers_initial += " " + worker.first_name.upper()[0]
67
                self.workers_initial += worker.last_name.upper()[0]
68
            self.act_type = event_act.act_type.name
69

    
70
    def init_free_time(self, length, begin_time):
71
        """ """
72
        self.type = "free"
73
        self.length = length
74
        self.__set_time(begin_time)
75

    
76

    
77
    def init_start_stop(self, title, time):
78
        """
79
        title: Arrivee ou Depart
80
        """
81
        self.type = "info"
82
        self.title = title
83
        self.__set_time(time)
84

    
85
def get_daily_appointments(date, worker, service, time_tables, occurrences):
86
    """
87
    """
88
    appointments = []
89

    
90
    timetables_set = IntervalSet((t.to_interval(date) for t in time_tables))
91
    occurrences_set = IntervalSet((o.to_interval() for o in occurrences))
92
    for free_time in timetables_set - occurrences_set:
93
        if free_time:
94
            delta = free_time.upper_bound - free_time.lower_bound
95
            delta_minutes = delta.seconds / 60
96
            appointment = Appointment()
97
            appointment.init_free_time(delta_minutes,
98
                    time(free_time.lower_bound.hour, free_time.lower_bound.minute))
99
            appointments.append(appointment)
100
    for occurrence in occurrences:
101
        appointment = Appointment()
102
        appointment.init_from_occurrence(occurrence, service)
103
        appointments.append(appointment)
104
    for time_table in time_tables:
105
        appointment = Appointment()
106
        appointment.init_start_stop(u"Arrivée",
107
            time(time_table.start_time.hour, time_table.start_time.minute))
108
        appointment.weight = -1
109
        appointments.append(appointment)
110
        appointment = Appointment()
111
        appointment.init_start_stop(u"Départ",
112
            time(time_table.end_time.hour, time_table.end_time.minute))
113
        appointment.weight = 1
114
        appointments.append(appointment)
115

    
116
    s = sorted(appointments, key=lambda app: app.weight)
117
    return sorted(s, key=lambda app: app.begin_time)
118

    
(4-4/10)