-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathout_of_office.py
93 lines (82 loc) · 3.6 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
from datetime import datetime
from sqlalchemy.orm import Session
from app.database.models import User, OutOfOffice
def get_who_is_out_of_office(session: Session,
event_start_date,
invited_emails):
"""
This func check who is out of office
:param session: db session
:param event_start_date: event start date
:param invited_emails: invited users
:return: all users that cant be at the meeting
"""
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,
session):
out = get_out_of_office_template(1,
user_id=user.id,
start_date=datetime.
strptime(
out_of_office_data['start_date']
+ ' ' +
out_of_office_data['start_time'],
'%Y-%m-%d %H:%M'),
end_date=datetime.strptime(
out_of_office_data['end_date']
+ ' ' +
out_of_office_data['end_time'],
'%Y-%m-%d %H:%M'),
status='On')
session.add(out)
def get_out_of_office_template(out_of_office_id,
user_id,
start_date=None,
end_date=None,
status='Off'):
return OutOfOffice(
id=out_of_office_id,
user_id=user_id,
start_date=start_date,
end_date=end_date,
status=status
)
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 = datetime.strptime(
out_of_office_data_from_req['start_date']
+ ' ' +
out_of_office_data_from_req['start_time'],
'%Y-%m-%d %H:%M')
out_of_office_data_from_db.end_date = datetime.strptime(
out_of_office_data_from_req['end_date']
+ ' ' +
out_of_office_data_from_req['end_time'],
'%Y-%m-%d %H:%M')
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):
"""
This func check if out of office date passed and changed the status to off
:param out_of_office_data: Out of office data from db
:param session:
:return: 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():
# update status to off
out_of_office_data.status = 'Off'
session.commit()
return out_of_office_data