-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
315 lines (270 loc) · 8.68 KB
/
control.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
import web
import gradebook
import hashlib
import time
#
#Requires a DB with a table called 'States' with columns 'state' and 'page'
#Requires a DB with a tabe called 'Users' with columns 'username' and 'section'
def sha1digest(s):
salt = "boy this is salty frdew34567uhygfrer6uhgfrtyuhijhbgftrdfg"
ho = hashlib.sha1(s+salt)
return ho.hexdigest()
ctrl = web.database(dbn="sqlite",db="control.db")
ctrl.ctx.db.text_factory=str #erm... I have NO CLUE what this means :-/
def isInTable(table,col,entry):
wherestring = '{0}=\"{1}\"'.format(col,entry)
res = ctrl.select(table,where=wherestring)
return bool(res)
def getEntry(table,col,key,ID):
"""
returns value of column from corresponding key/ID. Returns
only one entry.
"""
wherestring = "{0}=\"{1}\"".format(key,ID)
bob = ctrl.select(table,where=wherestring,what=col)
if bool(bob): #Calling bool(bob) depletes the iterator
bob = ctrl.select(table,where=wherestring,what=col)
return bob[0][col]
else:
return None
def isStudent(user):
return isInTable("students","username",user)
def isInstructor(user):
return isInTable("instructors","username",user)
def getPassHash(user):
"""
Returns the hash of the user's password or returns false if
the user doesn't exist.
"""
emp = lambda x: x==None or x=="" or x.isspace()
if isStudent(user):
paswd = getEntry("students","password","username",user)
if emp(paswd):
return None
else:
return paswd
elif isInstructor(user):
paswd = getEntry("instructors","password","username",user)
if emp(paswd):
return None
else:
return paswd
else:
return False
def setPassword(user,paswd):
"""
Stores a hash of the user's password. Returns false if
the user is not found.
"""
passhash = sha1digest(paswd)
sqldic={}
sqldic['where']="username = \"{0}\"".format(user)
sqldic['password']=passhash
if isStudent(user):
ctrl.update("students",**sqldic)
elif isInstructor(user):
ctrl.update("instructors",**sqldic)
else:
return False
def clearPassword(user):
"""
Deletes the password
"""
sqldic={}
sqldic['where']="username = \"{0}\"".format(user)
sqldic['password']=None
if isStudent(user):
ctrl.update("students",**sqldic)
elif isInstructor(user):
ctrl.update("instructors",**sqldic)
else:
return False
def validatePassword(user,pashash):
return pashash == getPassHash(user)
def addInstructor(user):
user=user.strip()
res = isInTable('instructors','username',user)
if not res:
ctrl.insert('instructors', username=user)
# def getUserSection(user):
# wherestring="username=\"{0}\"".format(user)
# bob=ctrl.select('Users', where=wherestring, what='section')
# return bob[0]['section']
def delUser(user):#UPDATE
wherestring="username=\"{0}\"".format(user)
ctrl.delete('Users',where=wherestring)
def assignInstructor(instr,section):
wherestring = 'name=\'{0}\''.format(section)
ctrl.update("sections", where=wherestring, instructor=instr)
def assignSession(qu,section):
wherestring = 'name=\'{0}\''.format(section)
ctrl.update("sections", where=wherestring, session=qu)
def getSections():
"""
Returns list of section names
"""
bob= ctrl.select('sections',what='name')
output = []
for i in bob:
output.append(i['name'])
return output
def getAssignedQuiz(sec):
"""
Returns the quiz currently assigned to a section
"""
return getEntry('sections','session','name',sec)
def getInstrSections(instr):
"""
Returns list of sections assigned to an instructor
"""
wherestring = 'instructor = \"{0}\"'.format(instr)
bob= ctrl.select('sections',what='name',where=wherestring)
output = []
for i in bob:
output.append(i['name'])
return output
def addSection(nam):
nam=nam.strip()
res = isInTable('sections','name', nam)
if not res:#no user there
ctrl.insert('sections', name = nam)
def addStudent(user,sec):#adds a student
user=user.strip()
res = isInTable('students','username', user)
if not res:#no user there
ctrl.insert('students',username = user, section = sec)
sec = sec.strip()#ensures section is added as well, if necessary
res = isInTable('sections','name',sec)
if not res:
addSection(sec)
def populateSections():
"""
This method populates the section list from the student roster.
"""
#ISSUE this is a hack
stus = ctrl.select("students")
for i in stus:
addStudent(i["username"],i["section"])
def getStudentsBySec(section):
"""
Returns the list of student usernames in a given section
"""
wherestring = "section = \"{0}\"".format(section)
students = ctrl.select("students",where=wherestring)
output = []
for i in students:
output.append(i['username'])
return output
def sessionAdd(sesname):
"""
Adds an entry to the sessions table with initialized states
"""
if isInTable("sessions","name",sesname):
return None
ctrl.insert("sessions",name = sesname, page=0, state="init")
def getInstrSession(instr):
return getEntry("instructors","session","username",instr)
def setInstrSession(instr,session):
wherestring = "username = \"{0}\"".format(instr)
sqldic={'where':wherestring,'session':session}
ctrl.update("instructors",**sqldic)
def getSessionSection(instr):
sess = getInstrSession(instr)
return getEntry("sections","name","session",sess)
def getSessionStudents(instr):
sec = getSessionSection(instr)
return getStudentsBySec(sec)
def getSessionPage(session):
return getEntry("sessions","page","name",session)
def getSessionState(session):
return getEntry("sessions","state","name",session)
def setSessionState(session,state):
sqldic={
"where" : "name = \"{0}\"".format(session),
"state": state
}
ctrl.update("sessions",**sqldic)
def getStudentSession(user):
sec = getEntry("students","section","username",user)
return getEntry("sections","session","name",sec)
def getStudentState(student):
sess = getStudentSession(student)
return getEntry("sessions","state","name",sess)
def getStudentPage(user):
return getSessionPage(getStudentSession(user))
def getUserSession(user):
if isStudent(user):
return getStudentSession(user)
if isInstructor(user):
return getInstrSession(user)
def getUserPage(user):
sess = getUserSession(user)
return getSessionPage(sess)
def getUserState(user):
sess = getUserSession(user)
return getSessionState(sess)
def updateEntry(table,col,key,ID,newvalue):
"""
Enters newvalue in the column corresponding to the given key/ID pair
"""
wherestring = "{0} = \"{1}\"".format(key,ID)
sqldict={"where":wherestring, col:newvalue}
ctrl.update(table,**sqldict) #**converts dict to keywords
def getQuizLength(session):
"""
Returns the number of questions in the quiz assigned to
session
"""
quizstr = gradebook.getSessionQuestions(session)
quizli = quizstr.split(',')
return len(quizli)
def advanceSession(session):
"""
Increments the question number. Sets the session to finished if
finished.
"""
length = getQuizLength(session)
curpage = getSessionPage(session)
if curpage >= length-1:
wherestring = "name = \"{0}\"".format(session)
sqldict={"where":wherestring,"state":"finished","page":curpage+1}
ctrl.update("sessions",**sqldict)
return False
else:
sqldict={
"where" : "name = \"{0}\"".format(session),
"page" : curpage+1,
"state" : "init"
}
ctrl.update("sessions",**sqldict)
return True
def setUltimatum(instr,duration):
"""
An ultimatum for timers.
"""
sess = getInstrSession(instr)
now = time.time()
then = now+duration+1
sqldic={
"where":"name = \"{0}\"".format(sess),
"ultimatum":then,
"state":"ultimatum",
}
ctrl.update("sessions",**sqldic)
def giveTimeLeft(user):
"""
Computes the time left in the ultimatum. If negative,
sets session to closed. Otherwise returns the string representation of
the number of seconds remaining.
"""
if isInstructor(user):
sess = getInstrSession(user)
else:
sess = getStudentSession(user)
timeup = getEntry("sessions","ultimatum","name",sess)
now = time.time()
left = int(timeup-now)
if left < -1:
setSessionState(sess,"closed")
return "closed"
return str(left)