-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam.py
79 lines (69 loc) · 1.83 KB
/
exam.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
from db import get_db
from user import User
from replit.database import dumps
import json
class Exam():
def __init__(self,
id_,
name,
author,
time_alotted,
is_open = False,
structure = {}):
self.id = str(id_)
self.name = name
self.author = author
self.time_alotted = time_alotted
self.is_open = is_open
self.structure = structure
@staticmethod
def all(as_dicts=False):
db = get_db()
lst = {k: Exam.get(k) for k, v in db['exams'].items() if k != 'next_id'}
if not as_dicts:
return lst
return {k: v.to_dict() for k, v in lst.items()}
@staticmethod
def get(exam_id):
eid = str(exam_id)
db = get_db()
exam = db['exams'].get(eid)
if not exam:
return None
exam = Exam(id_=eid,
name=exam['name'],
author=User.get(exam['author_id']),
time_alotted=exam['time_alotted'],
is_open=exam['is_open'],
structure=exam['structure'])
return exam
@staticmethod
def create(name, author, time_alotted, is_open=False, structure={}):
db = get_db()
exams = db['exams']
eid = str(exams['next_id'])
exams['next_id'] += 1
exams[eid] = {
'name': name,
'author_id': author.id,
'time_alotted': time_alotted,
'is_open': is_open,
'structure': structure
}
return Exam.get(str(eid))
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'author_id': self.author.id,
'time_alotted': self.time_alotted,
'is_open': self.is_open,
'structure': json.loads(dumps(self.structure))
}
def delete(self):
db = get_db()
del db['exams'][self.id]
return self
def commit(self):
db = get_db()
db['exams'][self.id] = self.to_dict()