-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrecords.py
397 lines (313 loc) · 12.1 KB
/
records.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
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
import sqlite3
import os
import json
_DATABASE_FILE = 'records.db'
"""
Registrant Scheme {
EMAIL: TEXT*,
DATA: TEXT (JSON parsed){
FIRST_NAME: TEXT,
LAST_NAME: TEXT,
UNIVERSITY: TEXT,
MAJOR: TEXT,
GRAD_YEAR: INTEGER,
... (other fields)
header_name: contents
}
ROLES: TEXT (Array parsed)
DISCORD_ID: TEXT (Used during email verification, ties email to user_id before adding to verification table)
}
Verified Scheme {
DISCORD_ID: INTEGER* PRIMARY KEY,
EMAIL: TEXT*,
TEAM_ID: INTEGER REFERENCES {_TEAM_TABLE_NAME}(id)
USERNAME: TEXT*
}
- Channels is a JSON object that can have a variable number of channels,
Channel Keys are (TEXT, VOICE, CATEGORY*)
Team Scheme {
ID: INTEGER PRIMARY KEY AUTOINCREMENT,
NAME: TEXT UNIQUE,
CHANNELS: TEXT* (JSON parsed) {
CATEGORY: INTEGER*,
TEXT: INTEGER,
VOICE: INTEGER
}
}
Code Scheme {
CODE: TEXT* PRIMARY KEY
USER_ID: INTEGER*
}
"""
_REG_RESPONSES_TABLE_NAME = 'registration'
_VERIFIED_TABLE_NAME = 'verified'
_TEAM_TABLE_NAME = 'teams'
_CODE_TABLE_NAME = 'codes'
def _initialize_db(cursor: sqlite3.Cursor):
# Registration form responses
cursor.execute(
f"""CREATE TABLE {_REG_RESPONSES_TABLE_NAME} (
email TEXT NOT NULL,
roles TEXT NOT NULL,
data TEXT,
discord_id INTEGER
)
""")
# Verified users
cursor.execute(
f"""CREATE TABLE {_VERIFIED_TABLE_NAME} (
discord_id INTEGER UNIQUE PRIMARY KEY,
team_id REFERENCES {_TEAM_TABLE_NAME}(id),
email TEXT UNIQUE NOT NULL,
username TEXT UNIQUE NOT NULL
)
""")
# Teams
cursor.execute(
f"""CREATE TABLE {_TEAM_TABLE_NAME} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
channels TEXT
)
""")
# Verification Codes
cursor.execute(
f"""CREATE TABLE {_CODE_TABLE_NAME} (
code TEXT PRIMARY KEY NOT NULL,
value INTEGER NOT NULL
)
""")
# ---------- Register Data into Database ------------------------------------------------------------ DONE
def add_registered_user(email: str, roles: list, data: dict):
#Check if user is not verified
if verified_email_exists(email):
print(f"The email ({email}) is already verified. Registered user is not added")
return
#Check if user with that email already registerd and remove it
if registered_user_exists(email):
remove_registered_user(email)
roles = json.dumps(roles)
data = json.dumps(data)
#Add Registered user back with newest form submission information
cursor.execute(
f'INSERT INTO {_REG_RESPONSES_TABLE_NAME} (email, roles, data) VALUES (?, ?, ?)',
(email, roles, data)
)
def add_verified_user(discord_id: int, email: str, username: str):
cursor.execute(
f'INSERT INTO {_VERIFIED_TABLE_NAME} (discord_id, email, username) VALUES (?, ?, ?)',
(discord_id, email, username)
)
def create_team(team_name: str, channels: dict) -> int:
channels_text = json.dumps(channels)
cursor.execute(
f'INSERT INTO {_TEAM_TABLE_NAME} (name, channels) VALUES (?,?)',
(team_name,channels_text)
)
return cursor.lastrowid
# ------------ Remove Entries from Tables -------------------------------------------------------------------------- DONE
def remove_registered_user(email: str):
cursor.execute(
f'DELETE FROM {_REG_RESPONSES_TABLE_NAME} WHERE email = ?',
(email,)
)
def remove_verified_user(discord_id: int):
cursor.execute(
f'DELETE FROM {_VERIFIED_TABLE_NAME} WHERE discord_id = ?',
(discord_id,)
)
def remove_team(team_id: int):
cursor.execute(
f'DELETE FROM {_TEAM_TABLE_NAME} WHERE id = ?',
(team_id,)
)
# ------------ Retrieve Individual Entry ------------------------------------------------------------------------ DONE
def get_registered_user(email: str) -> dict:
cursor.execute(
f'SELECT * FROM {_REG_RESPONSES_TABLE_NAME} WHERE email = ?',
(email,)
)
data_tuple = cursor.fetchone()
if data_tuple is None:
return None
# Convert data string to dictionary
data = {
'email': data_tuple[0],
'roles': json.loads(data_tuple[1]),
'data': json.loads(data_tuple[2]),
'discord_id': data_tuple[3]
}
return data
def get_verified_user(discord_id: int) -> dict:
cursor.execute(
f'SELECT * FROM {_VERIFIED_TABLE_NAME} WHERE discord_id = ?',
(discord_id,)
)
data_tuple = cursor.fetchone()
if data_tuple is None:
return None
# Convert data string to dictionary
data = {
'discord_id': data_tuple[0],
'team_id': data_tuple[1],
'email': data_tuple[2],
'username': data_tuple[3],
}
return data
def get_team(team_id: int) -> dict:
cursor.execute(
f'SELECT * FROM {_TEAM_TABLE_NAME} WHERE id = ?',
(team_id,)
)
data_tuple = cursor.fetchone()
if data_tuple is None:
return None
# Convert data string to dictionary
data = {
'id': data_tuple[0],
'name': data_tuple[1],
'channels': json.loads(data_tuple[2])
}
return data
# -------------- Check if entry exists ----------------------------------------------------------------------- DONE
def registered_user_exists(email: str) -> bool:
cursor.execute(
f'SELECT * FROM {_REG_RESPONSES_TABLE_NAME} WHERE email = ?',
(email,)
)
return cursor.fetchone() is not None
def verified_user_exists(discord_id: int) -> bool:
cursor.execute(
f'SELECT * FROM {_VERIFIED_TABLE_NAME} WHERE discord_id = ?',
(discord_id,)
)
return cursor.fetchone() is not None
def team_exists(team_id: int) -> bool:
cursor.execute(
f'SELECT * FROM {_TEAM_TABLE_NAME} WHERE id = ?',
(team_id,)
)
return cursor.fetchone() is not None
# ------- Registered User Methods ----------------------------------------------------------------------------
def get_roles(email: str) -> list:
cursor.execute(
f'SELECT roles FROM {_REG_RESPONSES_TABLE_NAME} WHERE email = ?',
(email,)
)
return json.loads(cursor.fetchone()[0])
def reassign_roles(email: str, roles: list):
roles = json.dumps(roles)
cursor.execute(
f'UPDATE {_REG_RESPONSES_TABLE_NAME} SET roles=? WHERE email=?',
(roles, email)
)
def get_first_name(email: str) -> str:
if not has_first_name(email):
return "Hackathon Registrant"
else:
return get_registered_user(email)['data']['first_name']
def has_first_name(email:str) -> bool:
userData = get_registered_user(email)
return 'first_name' in userData['data']
def update_reg_discord_id(email:str, discord_id:int):
cursor.execute(f"UPDATE {_REG_RESPONSES_TABLE_NAME} SET discord_id=:discord_id where email=:email", {
'discord_id':discord_id,
'email': email
})
def get_email_from_reg(discord_id: int) -> str:
return cursor.execute(f"SELECT email FROM {_REG_RESPONSES_TABLE_NAME} WHERE discord_id=:discord_id", {
'discord_id': discord_id
}).fetchone()[0]
# ------- Verified User Methods ----------------------------------------------------------------------------
def get_verified_discord_id(email: str) -> int:
return cursor.execute(
f"SELECT discord_id FROM {_VERIFIED_TABLE_NAME} WHERE email=:email", {'email': email}).fetchone()[0]
def get_verified_email(discord_id: int) -> str:
return cursor.execute(
f"SELECT email FROM {_VERIFIED_TABLE_NAME} WHERE discord_id=:discord_id", {'discord_id': discord_id}).fetchone()[0]
def verified_email_exists(email: str) -> bool:
return cursor.execute(f"SELECT * from {_VERIFIED_TABLE_NAME} WHERE email=:email", {
'email': email
}).fetchone() is not None
def user_is_participant(user_id: int) -> bool:
roles = get_registered_user(get_verified_email(user_id))['roles']
return 'participant' in roles
# ----------- Team Methods ----------------------------------------------------------------------------
def add_channel(team_id, channel):
channels = get_channels(team_id)
channels.append(channel)
cursor.execute(f"UPDATE {_TEAM_TABLE_NAME} SET channels=:channels WHERE id=:team_id", {
'team_id': team_id,
'channels': channels
})
def get_channels(team_id):
channels_text = cursor.execute(
f"SELECT channels FROM {_TEAM_TABLE_NAME} WHERE id=:team_id", {
'team_id': team_id})
return json.loads(channels_text)
def drop_team(discord_id: int):
cursor.execute(
f'UPDATE {_VERIFIED_TABLE_NAME} SET team_id=NULL WHERE discord_id=:discord_id', {
'discord_id': discord_id})
def join_team(team_id: int, discord_id: int):
cursor.execute(
f'UPDATE {_VERIFIED_TABLE_NAME} SET team_id=:team_id WHERE discord_id=:discord_id', {
'discord_id': discord_id, 'team_id': team_id})
def get_team_size(team_id: int) -> int:
return cursor.execute(
f'SELECT COUNT(*) FROM {_VERIFIED_TABLE_NAME} WHERE team_id=:team_id', {
'team_id': team_id}).fetchone()[0]
def get_number_of_teams() -> int:
return cursor.execute(f"SELECT COUNT(*) FROM {_TEAM_TABLE_NAME}").fetchone()[0]
def get_max_team_id() -> int:
max_id = cursor.execute(f'SELECT MAX(id) FROM {_TEAM_TABLE_NAME}').fetchone()[0]
return max_id if max_id is not None else 0
def is_member_on_team(discord_id: int) -> bool:
cursor.execute(f"SELECT team_id FROM {_VERIFIED_TABLE_NAME} WHERE discord_id=:discord_id", {'discord_id': discord_id})
user_team_id = cursor.fetchone()[0]
return user_team_id is not None
def get_user_team_id(discord_id: int) -> int:
return cursor.execute(f"SELECT team_id FROM {_VERIFIED_TABLE_NAME} WHERE discord_id=:discord_id",
{'discord_id': discord_id}).fetchone()[0]
def get_team_id(team_name: str) -> int:
return cursor.execute(f"SELECT id FROM {_TEAM_TABLE_NAME} WHERE name=:team_name", {'team_name': team_name}).fetchone()[0]
def get_team_members(team_id: int) -> list:
cursor.execute(f'SELECT discord_id FROM {_VERIFIED_TABLE_NAME} WHERE team_id=:team_id', {'team_id': team_id})
members = cursor.fetchall()
return members
def team_name_exists(team_name: int) -> bool:
cursor.execute(f"SELECT * FROM {_TEAM_TABLE_NAME} WHERE name=:team_name", {
'team_name': team_name
})
return cursor.fetchone() is not None
def update_channels(team_id: int, channels: list):
channels_text = json.dumps(channels)
cursor.execute(f"UPDATE {_TEAM_TABLE_NAME} SET channels=:channels WHERE id=:team_id", {
'channels': channels_text,
'team_id': team_id
})
# ----------- Verification Code Methods ----------------------------------------------------------------------
def add_code(code: str, value: int):
cursor.execute(f"INSERT INTO {_CODE_TABLE_NAME} (code, value) VALUES (:code, :value)", {
'code': code,
'value': value
})
def code_exists(code: str) -> bool:
return cursor.execute(f"SELECT * FROM {_CODE_TABLE_NAME} WHERE code=:code", {'code': code}).fetchone() is not None
def get_value_from_code(code: str) -> int:
return cursor.execute(f"SELECT value FROM {_CODE_TABLE_NAME} WHERE code=:code", {'code': code}).fetchone()[0]
def remove_code(code: str):
cursor.execute(f"DELETE FROM {_CODE_TABLE_NAME} WHERE code=:code", {'code': code})
def remove_user_codes(discord_id: int):
cursor.execute(f"DELETE FROM {_CODE_TABLE_NAME} WHERE value=:discord_id",{
'discord_id': discord_id
})
# ----------- Connect to Database ----------------------------------------------------------------------------
# Check if db file exists
_db_file_exists = os.path.isfile(_DATABASE_FILE)
# Connecting creates the file if not there
_connection = sqlite3.connect(_DATABASE_FILE, isolation_level=None)
cursor = _connection.cursor()
# Initialize tables if database is new
if not _db_file_exists:
_initialize_db(cursor)