-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdao.py
463 lines (384 loc) · 13.2 KB
/
dao.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
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
"""
An operator for operate database
"""
import sqlite3
import time
class DaoBase():
"""docstring for DaoBase"""
def __init__(self, arg):
super(DaoBase, self).__init__()
self.db_name = arg
self.conn = sqlite3.connect(self.db_name)
self.conn.row_factory = sqlite3.Row
self.cur = self.conn.cursor()
class UserDao(DaoBase):
"""docstring for UserDao"""
def __init__(self, arg):
super(UserDao, self).__init__(arg)
def check_pass(self, username, password):
'''check if user exists
return None or userinfo.
'''
self.cur.execute("SELECT * FROM user WHERE username = ? and password = ?",
(username, password))
res = self.cur.fetchone()
return res
def user_exist(self, username):
'''check if username exists
return userinfo or None
'''
self.cur.execute("SELECT * FROM user WHERE username = ?", (username,))
res = self.cur.fetchone()
return res
def insert(self, *args):
'''insert userinfo into table user
raise exception.
'''
assert not self.user_exist(args[0])
try:
self.cur.execute("INSERT INTO user (username, password, njuid, user_type)"
"VALUES (?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
def fetch(self, username):
self.cur.execute("SELECT * FROM user WHERE username = ?", (username,))
res = self.cur.fetchone()
assert res
return res
def fetch_all(self):
self.cur.execute("SELECT * FROM user")
res = self.cur.fetchall()
return res
def delete(self, username):
# TODO: this delete is by username
try:
self.cur.execute("DELETE FROM user WHERE username = ?", (username,))
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
def update(self, username, info_dict):
try:
for k, v in info_dict.items():
print("UPDATE user SET %s = %s WHERE username = %s" %
(k, v, username))
self.cur.execute("UPDATE user SET {} = ? WHERE username = ?".format(k),
(v, username))
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class UserInfo(DaoBase):
"""docstring for UserInfo"""
def __init__(self, arg):
super(UserInfo, self).__init__(arg)
def delete(self, username):
try:
self.cur.execute("DELETE FROM user_info WHERE username = ?", (username,))
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
def insert(self, *args):
try:
self.cur.execute("INSERT INTO user_info "
"(username, sex, email, truename, mobile, self_intro)"
"VALUES (?, ?, ?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
def fetch(self, username):
self.cur.execute("SELECT * FROM user_info WHERE username = ?", (username,))
res = self.cur.fetchone()
assert res
return res
def fetch_all(self):
self.cur.execute("SELECT * FROM user_info")
res = self.cur.fetchall()
return res
def fetch_member_info(self):
self.cur.execute("SELECT user.username, user.user_id, user.user_type, user.njuid, user_info.email, user_info.mobile "
"FROM user, user_info "
"WHERE user.username = user_info.username")
res = self.cur.fetchall()
return res
def fetch_user_info(self):
self.cur.execute("SELECT user.username, user.njuid, user_info.email, user_info.mobile "
"FROM user, user_info "
"WHERE user.username = user_info.username")
res = self.cur.fetchall()
return res
def update(self, username, info_dict):
try:
for k, v in info_dict.items():
print("UPDATE user_info SET %s = %s WHERE username = %s" %
(k, v, username))
self.cur.execute("UPDATE user_info SET {} = ? WHERE username = ?".format(k),
(v, username))
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class ArticleDao(DaoBase):
"""docstring for ArticleDao"""
def __init__(self, arg):
super(ArticleDao, self).__init__(arg)
def fetch_all(self):
'''fetch all articles.
(use fetchmany())
return the list of articles.
'''
self.cur.execute("SELECT * FROM article")
res = self.cur.fetchall()
return res
def fetch_articles(self, author_name):
'''fetch someone's articles
return the list of articles.
'''
self.cur.execute("SELECT * FROM article WHERE author = ?", (author_name,))
res = self.cur.fetchall()
return res
def insert(self, *args):
'''insert an article
no return.
raise exceptions.
'''
try:
self.cur.execute("INSERT INTO article (title, author, content, date_)"
"VALUES (?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class MessageDao(DaoBase):
"""docstring for MessageDao"""
def __init__(self, arg):
super(MessageDao, self).__init__(arg)
def fetch_message(self, receiver_name):
self.cur.execute(
"SELECT * FROM message WHERE receiver_name = ? or receiver_name = ?", (receiver_name, 'ALL'))
res = self.cur.fetchall()
return res
def insert(self, *args):
try:
self.cur.execute("INSERT INTO message (sender_name, receiver_name, title, content, date_)"
"VALUES (?, ?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class AnnouncementDao(DaoBase):
"""docstring for AnnouncementDao"""
def __init__(self, arg):
super(AnnouncementDao, self).__init__(arg)
def fetch_announcement(self, date_):
'''fetch the announcement of _date
TODO(lxiange): select announcement by date is useless...
'''
self.cur.execute("SELECT * FROM announcement WHERE date_ = ?", (date_,))
res = self.cur.fetchone()
return res
def fetch_all(self):
'''fetch all articles.
return the list of articles.
'''
self.cur.execute("SELECT * FROM announcement")
res = self.cur.fetchall()
return res
def insert(self, *args):
'''insert an Announcement
'''
try:
self.cur.execute("INSERT INTO announcement(title, author, content, date_)"
"VALUES (?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class HomeworkDao(DaoBase):
"""for homework itself!"""
def __init__(self, arg):
super(HomeworkDao, self).__init__(arg)
def fetch_by_id(self, homework_id):
self.cur.execute("SELECT * FROM homework WHERE homework_id=?",
(homework_id,))
res = self.cur.fetchone()
return res
def fetch_all(self):
'''fetch all homework.
return the list of homework.
'''
self.cur.execute("SELECT * FROM homework")
res = self.cur.fetchall()
return res
def insert(self, *args):
try:
self.cur.execute("INSERT INTO homework(title, author, content, date_, deadline)"
"VALUES (?, ?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
def get_last_id(self):
self.cur.execute("SELECT last_insert_rowid()")
res = self.cur.fetchone()
return res[0]
def delete(self, homework_id):
# TODO: this delete is by id
try:
self.cur.execute(
"DELETE FROM homework WHERE homework_id = ?", (homework_id,))
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class SubmissionDao(DaoBase):
"""docstring for SubmissionDao
(username, homework_id) is unique
"""
def __init__(self, arg):
super(SubmissionDao, self).__init__(arg)
def fetch(self, username):
'''fetch someone's submission return a list of dict'''
self.cur.execute("SELECT * FROM submission WHERE author=?", (username,))
res = self.cur.fetchall()
return res
def fetch_by_homework_id(self, homework_id):
self.cur.execute("SELECT * FROM submission WHERE homework_id = ?",
(homework_id,))
res = self.cur.fetchall()
return res
def fetch_one_submission(self, username, homework_id):
self.cur.execute("SELECT * FROM submission WHERE author = ? and homework_id = ?",
(username, homework_id))
res = self.cur.fetchone()
return res
def has_submitted(self, username, homework_id):
'''someone has submitted the homework'''
res = self.fetch_one_submission(username, homework_id)
if res:
return True
else:
return False
def insert(self, *args):
'''add a submit'''
try:
self.cur.execute("INSERT INTO submission"
"(author, title, content, date_, homework_id, file_path, status)"
"VALUES (?, ?, ?, ?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class ResourceDao(DaoBase):
"""docstring for ResourceDao"""
def __init__(self, arg):
super(ResourceDao, self).__init__(arg)
def get_last_id(self):
self.cur.execute("SELECT last_insert_rowid()")
res = self.cur.fetchone()
return res[0]
def fetch(self, username):
'''fetch someone's resource'''
def fetch_all(self):
self.cur.execute("SELECT * FROM resource")
res = self.cur.fetchall()
return res
def fetch_by_id(self, resource_id):
self.cur.execute("SELECT * FROM resource WHERE resource_id =?",
(resource_id,))
res = self.cur.fetchone()
return res
def insert(self, *args):
try:
self.cur.execute("INSERT INTO resource"
"(author, title, content, date_, file_path)"
"VALUES (?, ?, ?, ?, ?)", args)
return True
except Exception as e:
print(e)
assert 0
return False
finally:
self.conn.commit()
class AdminDao(DaoBase):
"""May be we should integrate privileged operations"""
def __init__(self, arg):
super(AdminDao, self).__init__(arg)
def is_admin(self, username):
if not username:
return False
self.cur.execute(
"SELECT user_type FROM user WHERE username = ?", (username,))
res = self.cur.fetchone()
if not res:
return False
if res[0] in ['root', 'admin', 'TA']:
# TODO(lxiange): res['user_type']
return True
else:
return False
def delete_user(self, username):
# TODO: delete anything about this user. such as homework, submission.
ud.delete(username)
ui.delete(username)
def set_TA(self, username):
ud.update(username, {'user_type': 'TA'})
def post_announcement(self, *args, **kw):
'''post_announcement'''
def delete_announcement(self, username):
'''delete_announcement'''
def delete_article(self, article_id):
'''delete_article'''
ud = UserDao('data.db3')
ui = UserInfo('data.db3')
ad = AnnouncementDao('data.db3')
adm = AdminDao('data.db3')
hd = HomeworkDao('data.db3')
sbd = SubmissionDao('data.db3')
rd = ResourceDao('data.db3')
md = MessageDao('data.db3')
if __name__ == '__main__':
print(ui.fetch_all())
ui.update('root', {'email': 'fffff@qq.com'})
print(ui.fetch_all())
print(adm.is_admin('root'))