-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_func.py
315 lines (275 loc) · 13.1 KB
/
database_func.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import mysql.connector
from dotenv import load_dotenv
import os
from pytz import timezone
import datetime
class database_func:
__instance = None
@staticmethod
def getInstance():
# singleton pattern to make sure only
# one instance of database connection is running
if database_func.__instance is None:
database_func()
return database_func.__instance
def __init__(self):
# establish database conection
if database_func.__instance is None:
load_dotenv()
connection_config_dict = {
'user': os.getenv('ROOT'),
'password': os.getenv('PASSWORD'),
'host': os.getenv('HOST'),
'database': os.getenv('DATABASE'),
'raise_on_warnings': True,
}
self.connection = mysql.connector.connect(**connection_config_dict)
if self.connection.is_connected():
db_Info = self.connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
self.cursor = self.connection.cursor()
database_func.__instance = self
else:
print("This is singleton, you cannot have multiple objects")
def add_user(self, userid, username):
# insert a user into the user table given
# userid username
insert_stmt = "insert ignore into Users (user_id, username)""Values (%s,%s)"
data = (userid, username)
self.cursor.execute(insert_stmt, data)
self.connection.commit()
print("Inserted successfully")
def add_class(self, class_name:str, channel_id:int, role_id:int, user_id:int):
# insert a class
insert_stmt = "insert into Classes (channel_id, class_name, role_id, user_id)""Values (%s,%s,%s,%s)"
data = (channel_id,class_name,role_id,user_id)
self.cursor.execute(insert_stmt, data)
self.connection.commit()
def add_enrollment(self, user_id, class_id):
# add user enrollment to a class
insert_stmt = "insert into Enrollments (user_id, class_id, date_enrolled)""Values (%s, %s, NOW())"
data = (user_id, class_id)
self.cursor.execute(insert_stmt, data)
self.connection.commit()
print("Inserted successfully")
# add_task will add a task to the table Tasks. Tasks has these columns: task_id(The discord message id), user_id(the discord author id), channel_id(the discord channel id), task_name(the task name), task_description(the details of the task), difficulty(int 1-10),deadline(the deadline of the task)
def add_task(self, task_id, user_id, channel_id, task_name, task_description, difficulty, deadline):
insert_stmt = "insert into Tasks (task_id, user_id, channel_id, task_name, task_description, difficulty, deadline)""Values (%s, %s, %s, %s, %s, %s, %s)"
data = (task_id, user_id, channel_id, task_name, task_description, difficulty, deadline)
self.cursor.execute(insert_stmt, data)
self.connection.commit()
print("Inserted successfully")
#get_tasks_channel_specific will return all the tasks in a specific channel and specific user, it takes in a channel id, user id, and the number of days to look forward
def get_tasks_channel_specific(self, channel_id, user_id, days):
select_stmt = "SELECT * from Tasks t where t.channel_id = %s and t.user_id = %s and DATEDIFF(t.deadline,NOW()) < %s"
data = (channel_id, user_id, days)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
def get_tasks_user_all(self, user_id,days):
select_stmt = "SELECT * from Tasks t where t.user_id = %s and DATEDIFF(t.deadline,NOW()) < %s"
data = (user_id,days)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
def update_task_deadline(self, user_id, taskname, new_deadline):
# change task dealdine
update_stmt = "update Tasks set deadline = %s where task_name = %s and user_id =%s;"
data = (new_deadline, taskname, user_id)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update %s successfully" % taskname)
def delete_user(self, userid):
# delete a user
delete_stmt = "delete from Users where user_id = %s"
data = (userid,)
self.cursor.execute(delete_stmt, data)
self.connection.commit()
print("delete successfully")
def delete_class(self, class_name):
# delete a class
delete_stmt = "delete from Classes where class_name = %s"
data = (class_name,)
self.cursor.execute(delete_stmt, data)
self.connection.commit()
print("delete %s successfully" % class_name)
def delete_task(self,task_name):
# delete a task
delete_stmt = "delete from Tasks where task_name = %s"
data = (task_name,)
self.cursor.execute(delete_stmt, data)
self.connection.commit()
print("delete %s successfully" % task_name)
def delete_expired_tasks(self):
# delete all expired tasks
delete_stmt = "delete from Tasks where deadline < NOW()"
self.cursor.execute(delete_stmt)
self.connection.commit()
print("delete expired tasks successfully")
def users_in_class(self, class_id):
select_stmt = "select Users.username from Users join Enrollments on Users.user_id = Enrollments.user_id where Enrollments.class_id =%s"
data = (class_id,)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
def print_all_users(self):
select_stmt = "select username from Users"
self.cursor.execute(select_stmt)
results = self.cursor.fetchall()
return results
def print_all_class(self):
select_stmt = "select* from Classes"
self.cursor.execute(select_stmt)
results = self.cursor.fetchall()
for x in results:
print(x)
def update_user(self,name,server,time_zone):
# update user info
update_stmt = "update Users set username = %s , servers = %s, time_zone = %s where username = %s;"
data = (name,server,time_zone)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update %s successfully" % name)
def update_class(self,classname,server):
# update class info
update_stmt = "update Classes set class_name = %s, servers = %s where class_name =%s"
data = (classname,server)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update %s successfully" % classname)
def clear_activity(self):
delete_stmt = "truncate table Activities"
self.cursor.execute(delete_stmt)
self.connection.commit()
print("clear activity successfully")
def add_activity(self, userid, activity):
# add user activity
insert_stmt = "insert into Activities (user_id,activity)""Values (%s,%s)"
data = [userid, activity]
self.cursor.execute(insert_stmt, data)
self.connection.commit()
print("Inserted " + activity + " successfully")
def update_activity(self,userid,activity):
# update user activity
update_stmt = "update Activities set time = time+10 where user_id =%s and activity =%s"
data = (userid,activity)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update %s successfully" % activity)
def activity_exist(self, userid, activity):
select_stmt = "SELECT * from Activities where user_id = %s and activity = %s"
data = (userid, activity)
self.cursor.execute(select_stmt,data)
results = self.cursor.fetchall()
if len(results) == 0:
return False
else:
return True
def get_user_activities(self,userid):
# return all users activity
select_stmt = "SELECT activity, time from Activities where user_id = %s"
data = (userid, )
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
# here we return list of tuples
def sum_user_activities(self,userid):
# sum their activity
select_stmt = "select sum(time) from Activities where user_id = %s and activity not like %s"
data = (userid,"%custom%" )
self.cursor.execute(select_stmt, data)
result = self.cursor.fetchone()[0]
if result is None:
result = 0
return result
def get_playtime_limit_and_warning(self,userid):
# prepare to warn user
select_stmt = "SELECT playtime_limit, is_warned from Users where user_id =%s"
data = (userid, )
self.cursor.execute(select_stmt, data)
result = self.cursor.fetchone()
return result
# return a tuple where [0] is limit and [1] is warned or not
def is_warned(self,userid):
# make them warned to prevent spamming
update_stmt = "update Users set is_warned =1 where user_id =%s"
data = (userid,)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update successfully")
# the user is warned
def change_warned(self,userid):
# change warning status daily
update_stmt = "update Users set is_warned =0 where user_id =%s"
data = (userid,)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update successfully")
# change warning status
def change_play_time_limit(self, userid, time):
# update playtime limit
update_stmt = "update Users set playtime_limit =%s where user_id =%s"
data = (time, userid)
self.cursor.execute(update_stmt, data)
self.connection.commit()
print("update successfully")
def add_reminder(self,reminder_id, channel_id, user_id, reminder_time, title, description):
# create a reminder
insert_stmt = "insert into Reminders (reminder_id, channel_id, user_id, reminder_time, reminder_title,reminder_description)""Values (%s,%s,%s,%s,%s,%s)"
data = [reminder_id , channel_id , user_id , reminder_time , title, description]
self.cursor.execute(insert_stmt,data)
self.connection.commit()
print("Inserted reminder successfully")
def get_reminders(self):
# get pending reminders
select_stmt = "select channel_id, reminder_title, reminder_description from Reminders where reminder_time BETWEEN %s AND %s"
now_time = datetime.datetime.now(timezone('America/Chicago'))
past_time = now_time - datetime.timedelta(minutes=1)
now = now_time.strftime('%Y-%m-%d-%H:%M:%S')
past = past_time.strftime('%Y-%m-%d-%H:%M:%S')
data = (past, now)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
# return lists of approached reminders
def add_meeting(self, meeting_id, channel_id, title, begin, end, location, description, creator):
# create a meeting
insert_stmt = "insert into Meetings ""Values (%s,%s,%s,%s,%s,%s,%s,%s)"
data = [meeting_id, channel_id, title, begin, end, location, description, creator]
self.cursor.execute(insert_stmt,data)
self.connection.commit()
print("Inserted meeting successfully")
def get_meetings(self):
# get meetings arrived
select_stmt = "select meeting_id, channel_id, meeting_title, begin_time, end_time,location, description from Meetings where begin_time BETWEEN %s AND %s"
now_time = datetime.datetime.now(timezone('America/Chicago'))
past_time = now_time - datetime.timedelta(minutes=1)
now = now_time.strftime('%Y-%m-%d-%H:%M:%S')
past = past_time.strftime('%Y-%m-%d-%H:%M:%S')
data = (past, now)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
def get_meetings_within_10minutes(self):
# get meetings within 10 minutes
select_stmt = "select meeting_id, channel_id, meeting_title, begin_time, end_time, location from Meetings where begin_time BETWEEN %s AND %s"
now_time = datetime.datetime.now(timezone('America/Chicago'))
time_change = datetime.timedelta(minutes=10)
future_time = now_time + time_change
now = now_time.strftime('%Y-%m-%d-%H:%M:%S')
future = future_time.strftime('%Y-%m-%d-%H:%M:%S')
data = (now, future)
self.cursor.execute(select_stmt, data)
results = self.cursor.fetchall()
return results
def add_participant_to_meetings(self, user_id,meeting_id):
# add users to participation table
insert_stmt = "insert into Participation (user_id, meeting_id) ""Values (%s,%s)"
data = [user_id,meeting_id]
self.cursor.execute(insert_stmt,data)
self.connection.commit()
print("Inserted participants successfully")
def disconnect(self):
# make sure to disconnect database after finish everything
self.cursor.close()
self.connection.close()