-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·485 lines (450 loc) · 17.9 KB
/
database.py
File metadata and controls
executable file
·485 lines (450 loc) · 17.9 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 16:34:27 2018
@author: John Carter and Dominic Scola
"""
from datetime import datetime
import re
import mysql.connector
from mysql.connector import errorcode
class se_db:
config = {
'user': 'debian-sys-maint',
'password': 'pR5hJsvCW37Z4Ude',
'host': '127.0.0.1',
'database': 'test',
}
def __init__(self):
self.connection = mysql.connector.connect(**se_db.config)
self.cursor = self.connection.cursor()
'''
Profile Functions
'''
def addProfile(self, email, password, first, last, school = "Rutgers Camden"):
'''
Adds a profile to the database
@param: email, password, first name, last name, school name (defaulted to Rutgers Camden)
@return: True if successful, False otherwise
'''
queryCheck = """SELECT COUNT(*) FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(queryCheck)
for (count) in self.cursor:
if count[0] > 0:
return False
except errorcode.Error as err:
print(err)
return False
queryInsert = """INSERT INTO test.profiles (email, password, first, last, school, upvotes) VALUES ('%s','%s','%s','%s','%s', 0)""" % (email, password, first, last, school)
try:
self.cursor.execute(queryInsert)
self.connection.commit()
except errorcode.Error as err:
print(err)
return False
return True
def checkProfileCredentials(self, email, password):
'''
Checks that the email and password match
@param: email, password
@return: True if credentials correct, False otherwise
'''
queryCheck = """SELECT COUNT(*) FROM test.profiles WHERE email = '%s' AND password = '%s'""" % (email, password)
try:
self.cursor.execute(queryCheck)
except:
return False
for (count) in self.cursor:
if count[0] == 1:
return True
else:
return False
def getProfileName(self, email):
'''
Gets the name of the user
@param: email
@return: the first and last name of the user as a string
'''
querySelect = """SELECT first, last FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(querySelect)
except:
return False
for (first, last) in self.cursor:
return first+" "+last
def getProfileSchool(self, email):
'''
Get the school of the user
@param: email
@return: the school the user belongs to.
'''
querySelect = """SELECT school FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(querySelect)
except:
return False
for (school) in self.cursor:
return school[0]
def getProfileUpvotes(self, email):
'''
Gets the number of upvotes the user has
@param: email
@return: the number of upvotes a user has
'''
querySelect = """SELECT upvotes FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(querySelect)
except:
return False
for (upvotes) in self.cursor:
return upvotes[0]
def getProfileClasses(self, email):
querySelect = """SELECT list_of_classes FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(querySelect)
except:
return False
class_list = []
for(classes) in self.cursor:
length = len(classes)
for i in range(length):
class_list.append(classes[i])
return class_list
def addDateTime(self, email):
'''
Adds the current date and time to a profile
@param: email of profile to edit
@return: the time added to the profile
'''
now = datetime.now()
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
self.cursor.execute("""UPDATE test.profiles SET active_flag = %s WHERE email = %s""",(formatted_date, email))
self.connection.commit()
return formatted_date
def getActiveUsers(self):
'''
Gets all active users
@return: all active users
'''
query = """SELECT first FROM test.profiles WHERE active_flag >= NOW() - INTERVAL 10 MINUTE"""
try:
self.cursor.execute(query)
except:
return False
activeUsers = []
for (name) in self.cursor:
names = len(name)
for (i) in range(names):
activeUsers.append(name[i])
return activeUsers
'''
Message Functions
'''
def addMessage(self, new_thread_ind, reply_message_id, creator, title, post, class_name):
'''
Adds a message into the message table
@param new_thread_id (0 if new thread), the id of the message you are replying to (0 if new thread), creator, title, post, name of class
@return True if successful, False otherwise
'''
if (new_thread_ind == 0):
queryCheck = """SELECT COUNT(*) FROM test.message"""
try:
self.cursor.execute(queryCheck)
for (count) in self.cursor:
if count[0] == 0:
queryInsert = """INSERT INTO test.message (message_id, date_time, creator, title, post, class, upvotes) VALUES (0, SYSDATE(), '%s', '%s', '%s', '%s', 0)""" % (creator, title, post, class_name)
try:
self.cursor.execute(queryInsert)
self.connection.commit()
except:
return False
return True
else:
queryCheckNumber = """SELECT MAX(message_id) FROM test.message"""
try:
self.cursor.execute(queryCheckNumber)
for (message_id_new) in self.cursor:
queryInsertNewThread = """INSERT INTO test.message (message_id, date_time, creator, title, post, class, upvotes) VALUES ('%d', SYSDATE(), '%s', '%s', '%s', '%s', 0)""" % (message_id_new[0] + 1, creator, title, post, class_name)
try:
self.cursor.execute(queryInsertNewThread)
self.connection.commit()
except:
return False
return True
except:
return False
except:
return False
else:
queryInsertNewMessage = """INSERT INTO test.message (message_id, date_time, creator, title, post, class, upvotes) VALUES ('%f', SYSDATE(), '%s', '%s', '%s', '%s', 0)""" % (reply_message_id +.001, creator, title, post, class_name)
try:
self.cursor.execute(queryInsertNewMessage)
self.connection.commit()
except Exception as e:
print(e)
return False
return True
def messagesByUser(self, creator):
'''
Lists all the messages a person has posted
@param: creator
@return: List of all messages
'''
querySelect = """SELECT post FROM test.message WHERE creator = '%s'""" % (creator)
try:
messages_list = []
self.cursor.execute(querySelect)
for (messages) in self.cursor:
messages_list.append(messages[0])
except:
return False
return messages_list
def getRepliesToThread(self, thread_message_id):
'''
Lists all replies to a thread
@param: id of the thread
@return: list of all replies
'''
querySelect = """SELECT post FROM test.message WHERE message_id > '%d' AND message_id < '%d'""" % (thread_message_id, thread_message_id + 1)
try:
replies_list = []
self.cursor.execute(querySelect)
for (replies) in self.cursor:
replies_list.append(replies[0])
except:
return False
return replies_list
def messagesByClass(self, class_name):
'''
Lists all the post titles by class
@param: name of the class
@return: a list of titles
'''
querySelect = """SELECT DISTINCT title FROM test.message WHERE class = '%s'""" % (class_name)
try:
threads_by_class = []
self.cursor.execute(querySelect)
for (threads) in self.cursor:
threads_by_class.append(threads[0])
except:
return False
return threads_by_class
def upvoteMessage(self, email, message_id):
query = """SELECT upvotes FROM test.message WHERE message_id = '%d'""" % (message_id)
query1 = """SELECT upvotes FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(query)
for (upvote_message) in self.cursor:
upvote_message = upvote_message[0]
self.cursor.execute(query1)
for (upvote_profile) in self.cursor:
upvote_profile= upvote_profile[0]
except:
return False
finally:
query2 = """UPDATE test.message SET upvotes = '%d' + 1 WHERE message_id='%d'""" % (upvote_message, message_id)
query3 = """UPDATE test.profiles SET upvotes = '%d' + 1 WHERE email='%s'""" % (upvote_profile, email)
try:
self.cursor.execute(query2)
self.connection.commit()
self.cursor.execute(query3)
self.connection.commit()
except:
return False
finally:
return True
def getThreadTitlesByTime(self):
query = """SELECT title from test.message WHERE (FLOOR(message_id) - message_id = 0) ORDER BY date_time DESC"""
try:
self.cursor.execute(query)
except:
return False
title_list = []
for (titles) in self.cursor:
length = len(titles)
for i in range(length):
title_list.append(titles[i])
return title_list
def getThreadClassByTime(self):
query = """SELECT class from test.message WHERE (FLOOR(message_id) - message_id = 0) OR DER BY date_time DESC"""
try:
self.cursor.execute(query)
except:
return False
class_list = []
for (classes) in self.cursor:
length = len(classes)
for i in range(length):
class_list.append(classes[i])
return class_list
def getThreadCreatorByTime(self):
query = """SELECT profiles.first from test.profiles profiles inner join test.message message on profiles.email = test.message.creator WHERE (FLOOR(message_id) - message_id = 0) ORDER BY date_time DESC"""
try:
self.cursor.execute(query)
except Exception as e:
print(e)
return False
creator_list = []
for(creators) in self.cursor:
length = len(creators)
for i in range(length):
creator_list.append(creators[i])
return creator_list
def getThreadMessageByTime(self):
query = """SELECT post from test.message WHERE (FLOOR(message_id) - message_id = 0) ORDER BY date_time DESC"""
try:
self.cursor.execute(query)
except:
return False
post_list = []
for(post) in self.cursor:
length = len(post)
for i in range(length):
post_list.append(post[i])
return post_list
def getThreadNumbersByTime(self):
query = """SELECT message_id from test.message WHERE (FLOOR(message_id) - message_id = 0) ORDER BY date_time DESC"""
try:
self.cursor.execute(query)
except:
return False
message_id_list = []
for (message_id) in self.cursor:
length = len(message_id)
for i in range(length):
message_id_list.append(str(message_id[i]))
return message_id_list
'''
Class Functions
'''
def listClasses(self):
'''
Lists all classes in the class table
@return: a list of class names
'''
query = """SELECT class_name FROM test.class"""
try:
self.cursor.execute(query)
except:
return False
classList = []
for (class_name) in self.cursor:
names = len(class_name)
for (i) in range(names):
classList.append(class_name[i])
return classList
def addClass(self, name, subject, color, userList, school = "Rutgers Camden"):
'''
Adds a class to the class table
@params: name of class, field of study, color, and list of users (a string)
@return: Error/Success message
'''
query = """SELECT COUNT(*) FROM test.class WHERE class_name = '%s'""" % (name)
try:
self.cursor.execute(query)
for (count) in self.cursor:
if count[0] > 0:
return False
except:
return False
queryInsert = """INSERT INTO test.class (class_name, subject, color, school, list_of_users) VALUES ('%s','%s','%s','%s','%s')""" % (name, subject, color, school, userList)
try:
self.cursor.execute(queryInsert)
self.connection.commit()
except mysql.connector.Error as err:
print(err)
return False
return True
def checkClassColor(self, className):
'''
Finds the class color for the class name passed in
@param 'className': class name to check color
@return: the class' color
'''
querySelect = """SELECT color FROM test.class WHERE class_name = '%s'""" % (className)
try:
self.cursor.execute(querySelect)
except:
return False
for (color) in self.cursor:
return color
def listClassesInSubject(self, subject):
'''
Lists all classes in a specific field of study
@param 'subject': a field of study to list classes
@return: a list of class names
'''
query = """SELECT class_name FROM test.class WHERE subject = '%s'""" % (subject)
try:
self.cursor.execute(query)
except:
return False
classList = []
for (class_name) in self.cursor:
names = len(class_name)
for (i) in range(names):
classList.append(class_name[i])
return classList
def classList(self, className):
'''
Lists all users in a specific class
@param 'className': name of class to list roster
@return: a list of users
'''
query = """SELECT list_of_users FROM test.class WHERE class_name = '%s'""" % (className)
try:
self.cursor.execute(query)
except:
return False
for (list_of_users) in self.cursor:
return list_of_users
def classCount(self, className):
'''
Returns the number of users in a class
@param 'className': name of class to find number of students
@return: number of students in the class
'''
query = """SELECT list_of_users FROM test.class WHERE class_name = '%s'""" % (className)
try:
self.cursor.execute(query)
except:
return False
for (list_of_users) in self.cursor:
userList = str(list_of_users)
userList = userList.strip(',')
userList = userList.split(' ')
return len(userList)
def addUserToClass(self, email, className):
'''
Adds another user to an existing class
@params: email of new user, class to add user to
@return: updated list of users in class
'''
query = """SELECT list_of_users FROM test.class WHERE class_name = '%s'""" % (className)
try:
self.cursor.execute(query)
except:
return False
for (list_of_users) in self.cursor:
list_of_users = str(list_of_users)
list_of_users = re.sub("[()']","", list_of_users)
userList = list_of_users + email
self.cursor.execute("""UPDATE test.class SET list_of_users = %s WHERE class_name = %s""",(userList, className))
self.connection.commit()
query1 = """SELECT list_of_classes FROM test.profiles WHERE email = '%s'""" % (email)
try:
self.cursor.execute(query1)
except errorcode.Error as err:
print(err)
return False
classList = ''
for (list_of_classes) in self.cursor:
list_of_classes = str(list_of_classes)
list_of_classes = re.sub("[()']","", list_of_classes)
classList = list_of_classes+className
self.cursor.execute("""UPDATE test.profiles SET list_of_classes = %s WHERE email = %s""",(classList, email))
self.connection.commit()
return userList, classList
def closeConnection(self):
self.cursor.close()
self.connection.close()