Project

General

Profile

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

calebasse / calebasse / agenda / appointments.py @ 72dbfaca

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

    
3
from datetime import time
4

    
5
from interval import IntervalSet
6

    
7

    
8
class Appointment(object):
9

    
10
    def __init__(self, title=None, begin_time=None, type=None,
11
            length=None, description=None, room=None):
12
        """ """
13
        self.title = title
14
        self.type = type
15
        self.length = length
16
        self.description = description
17
        self.room = room
18
        self.convocation_sent = None
19
        self.other_services_names = []
20
        self.patient_record_id = None
21
        self.event_id = None
22
        self.event_type = None
23
        self.occurrence_id = None
24
        self.workers_initial = None
25
        self.weight = 0
26
        self.act_type = None
27
        self.__set_time(begin_time)
28

    
29
    def __set_time(self, time):
30
        self.begin_time = time
31
        if time:
32
            self.begin_hour = time.strftime("%H:%M")
33
        else:
34
            self.begin_hour = None
35

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

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

    
75

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

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

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

    
115
    return sorted(appointments, key=lambda app: (app.begin_time, app.weight))
116

    
(4-4/10)