forked from humblEgo/42mate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_manage.py
168 lines (144 loc) · 4.72 KB
/
db_manage.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from models import User, Evaluation
from app import db
from datetime import datetime, timedelta
from pytz import timezone, utc
def create_user(form):
"""
:param form: payload from slack slash command
:return User: created user in database
"""
slack_id = form.getlist('user_id')[0]
intra_id = form.getlist('user_name')[0]
try:
user=User(
slack_id=slack_id,
intra_id=intra_id,
)
db.session.add(user)
db.session.commit()
return user
except Exception as e:
print(str(e))
def register_user(slack_id):
"""
update register field to True and joined field to False from user record
:param slack_id: string
"""
try:
user = User.query.filter_by(slack_id=slack_id).first()
user.register = True
user.joined = False
db.session.commit()
except Exception as e:
print(str(e))
def unregister_user(slack_id):
"""
update register field to False and joined field to False from user record
:param slack_id: string
"""
try:
user = User.query.filter_by(slack_id=slack_id).first()
user.register = False
user.joined = False
db.session.commit()
except Exception as e:
print(str(e))
def join_user(slack_id):
"""
update register field to True and joined field to True from user record
:param slack_id: string
"""
try:
user = User.query.filter_by(slack_id=slack_id).first()
user.register = True
user.joined = True
db.session.commit()
except Exception as e:
print(str(e))
def unjoin_user(slack_id):
"""
update joined field to False from user record
:param slack_id: string
"""
try:
user = User.query.filter_by(slack_id=slack_id).first()
user.joined = False
db.session.commit()
except Exception as e:
print(str(e))
def get_user_state(user):
"""
:param user: User
:return string: user state based on register and joined field from user record
"""
if user.register:
if user.joined:
return "joined"
else:
return "unjoined"
else:
return "unregistered"
def get_user_record(form):
"""
:param form: payload from slack slash command
:return User: record of the user who entered command from database
"""
slack_id = form.getlist('user_id')[0]
user_record = User.query.filter_by(slack_id=slack_id).first()
return user_record
def get_today_start_dt():
"""
get today's 00:00:00 KST datetime and convert it to UTC datetime
:return datetime: today's 00:00:00 datetime(UTC)
"""
now_dt_kst = datetime.now(timezone('Asia/Seoul'))
today_start_dt_kst = now_dt_kst.replace(hour=00, minute=00, second=00)
today_start_dt_utc = today_start_dt_kst.astimezone(utc)
return today_start_dt_utc
def get_user_current_mate(user):
"""
get today's mate by getting evaluation record based on current day
:param user: User
:return string or none: today's mate if exists
"""
today_utc = get_today_start_dt()
yesterday_utc = today_utc - timedelta(days=1)
current_evaluation = Evaluation.query.filter(Evaluation.user == user).order_by(Evaluation.index.desc()).first()
if current_evaluation is None:
return None
current_match_day = current_evaluation.match.match_day
if current_match_day >= yesterday_utc and current_match_day < today_utc:
return current_evaluation.mate.intra_id
def get_user_info(user):
"""
:param user: User
:return dictionary: user information
"""
user_info = {}
user_info['slack_id'] = user.slack_id
user_info['intra_id'] = user.intra_id
user_info['state'] = get_user_state(user)
user_info['current_mate'] = get_user_current_mate(user)
return user_info
def is_overlap_evaluation(block_id):
"""
:param block_id: string
:return boolean: True if react_time exists
"""
evaluation_index = block_id.replace('evaluation_blocks_', '')
react_time = Evaluation.query.filter_by(index=evaluation_index).first().react_time
return True if react_time else False
def update_evaluation(data):
"""
update react_time and satisfaction field from evaluation record
:param data: payload from evaluation callback
"""
try:
evaluation_index = data['message']['blocks'][1]['block_id'].replace('evaluation_blocks_', '')
evaluation = Evaluation.query.filter_by(index=evaluation_index).first()
if evaluation.react_time is None:
evaluation.react_time = datetime.utcnow()
evaluation.satisfaction = int(data['actions'][0]['value'])
db.session.commit()
except Exception as e:
print(str(e))