-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathout_of_office.py
125 lines (106 loc) · 3.13 KB
/
out_of_office.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from datetime import datetime
from sqlalchemy.orm import Session
from typing import List
from app.database.models import User, OutOfOffice
DATETIME_FORMAT = "%Y-%m-%d %H:%M"
START_DATE = "start_date"
END_DATE = "end_date"
START_TIME = "start_time"
END_TIME = "end_time"
def get_who_is_out_of_office(
session: Session,
event_start_date: datetime,
invited_emails: List[str],
):
"""
Get who is out of office
Args:
session: db session
event_start_date: event start date
invited_emails: invited emails
Returns:
Users who are out of office at the event date
"""
out_of_office_users = (
session.query(User.username, User.email)
.join(OutOfOffice)
.filter(User.email.in_(invited_emails))
.filter(
OutOfOffice.start_date <= event_start_date,
OutOfOffice.end_date >= event_start_date,
)
.filter(OutOfOffice.status == "On")
.all()
)
return out_of_office_users
def insert_new_out_of_office(out_of_office_data, user: User, session: Session):
out = get_out_of_office_template(
user_id=user.id,
start_date=get_date_formatted(
out_of_office_data,
START_DATE,
START_TIME,
DATETIME_FORMAT,
),
end_date=get_date_formatted(
out_of_office_data,
END_DATE,
END_TIME,
DATETIME_FORMAT,
),
status="On",
)
session.add(out)
def get_out_of_office_template(
user_id,
start_date=None,
end_date=None,
status="Off",
):
return OutOfOffice(
user_id=user_id,
start_date=start_date,
end_date=end_date,
status=status,
)
def get_date_formatted(out_of_office_data, date, time, date_format):
return datetime.strptime(
out_of_office_data[date] + " " + out_of_office_data[time],
date_format,
)
def update_out_of_office(
out_of_office_data_from_req,
out_of_office_data_from_db,
):
activate_out_of_office = "1"
if out_of_office_data_from_req["outOfOffice"] == activate_out_of_office:
out_of_office_data_from_db.start_date = get_date_formatted(
out_of_office_data_from_req,
START_DATE,
START_TIME,
DATETIME_FORMAT,
)
out_of_office_data_from_db.end_date = get_date_formatted(
out_of_office_data_from_req,
END_DATE,
END_TIME,
DATETIME_FORMAT,
)
out_of_office_data_from_db.status = "On"
else:
out_of_office_data_from_db.status = "Off"
def update_out_of_office_status_to_off(out_of_office_data, session: Session):
"""
Update out of office status to off if out of office date passed
Args:
out_of_office_data: Out of office data from db
session: db session
Returns:
out_of_office_data object
"""
if out_of_office_data:
if out_of_office_data.status == "On":
if out_of_office_data.end_date < datetime.now():
out_of_office_data.status = "Off"
session.commit()
return out_of_office_data