191
191
if worksheet_exercise is None:
194
new_save = ExerciseSave()
194
old_save = req.store.find(ExerciseSave,
195
ExerciseSave.ws_ex_id == worksheet_exercise.id,
196
ExerciseSave.user == req.user).one()
198
#Overwrite the old, or create a new if there isn't one
200
new_save = ExerciseSave()
201
req.store.add(new_save)
195
205
new_save.worksheet_exercise = worksheet_exercise
196
206
new_save.user = req.user
197
207
new_save.text = unicode(text)
198
208
new_save.date = datetime.datetime.now()
199
req.store.add(new_save)
200
210
return {"result": "ok"}
213
def generate_exerciselist(worksheet, req, worksheetdata):
214
"""Runs through the worksheetstream, generating the appropriate
215
WorksheetExercises, and de-activating the old ones."""
217
# Turns the worksheet into an xml stream, and then finds all the
218
# exercise nodes in the stream.
219
worksheetdata = genshi.XML(worksheetdata)
220
for kind, data, pos in worksheetdata:
221
if kind is genshi.core.START:
222
# Data is a tuple of tag name and a list of name->value tuples
223
if data[0] == 'exercise':
229
if attr[0] == 'optional':
230
optional = attr[1] == 'true'
232
exercises.append((src, optional))
234
# Set all current worksheet_exercises to be inactive
235
db_worksheet_exercises = req.store.find(WorksheetExercise,
236
WorksheetExercise.worksheet_id == worksheet.id)
237
for worksheet_exercise in db_worksheet_exercises:
238
worksheet_exercise.active = False
240
for exerciseid, optional in exercises:
241
worksheet_exercise = req.store.find(WorksheetExercise,
242
WorksheetExercise.worksheet_id == worksheet.id,
243
Exercise.id == WorksheetExercise.exercise_id,
244
Exercise.id == exerciseid).one()
245
if worksheet_exercise is None:
246
exercise = req.store.find(Exercise,
247
Exercise.id == exerciseid
251
worksheet_exercise = WorksheetExercise()
252
worksheet_exercise.worksheet_id = worksheet.id
253
worksheet_exercise.exercise_id = exercise.id
254
req.store.add(worksheet_exercise)
255
worksheet_exercise.active = True
256
worksheet_exercise.seq_no = ex_num
257
worksheet_exercise.optional = optional
260
# Note that this is the view of an existing worksheet. Creation is handled
261
# by OfferingRESTView (as offerings have worksheets)
203
262
class WorksheetRESTView(JSONRESTView):
204
263
"""View used to update a worksheet."""
206
def generate_exerciselist(self, req, worksheet):
207
"""Runs through the worksheetstream, generating the appropriate
208
WorksheetExercises, and de-activating the old ones."""
210
# Turns the worksheet into an xml stream, and then finds all the
211
# exercise nodes in the stream.
212
worksheet = genshi.XML(worksheet)
213
for kind, data, pos in worksheet:
214
if kind is genshi.core.START:
215
# Data is a tuple of tag name and a list of name->value tuples
216
if data[0] == 'exercise':
222
if attr[0] == 'optional':
223
optional = attr[1] == 'true'
225
exercises.append((src, optional))
227
# Set all current worksheet_exercises to be inactive
228
db_worksheet_exercises = req.store.find(WorksheetExercise,
229
WorksheetExercise.worksheet_id == self.context.id)
230
for worksheet_exercise in db_worksheet_exercises:
231
worksheet_exercise.active = False
233
for exerciseid, optional in exercises:
234
worksheet_exercise = req.store.find(WorksheetExercise,
235
WorksheetExercise.worksheet_id == self.context.id,
236
Exercise.id == WorksheetExercise.exercise_id,
237
Exercise.id == exerciseid).one()
238
if worksheet_exercise is None:
239
exercise = req.store.find(Exercise,
240
Exercise.id == exerciseid
244
worksheet_exercise = WorksheetExercise()
245
worksheet_exercise.worksheet_id = self.context.id
246
worksheet_exercise.exercise_id = exercise.id
247
req.store.add(worksheet_exercise)
248
worksheet_exercise.active = True
249
worksheet_exercise.seq_no = ex_num
250
worksheet_exercise.optional = optional
252
265
def get_permissions(self, user):
253
266
# XXX: Do it properly.
254
267
# XXX: Lecturers should be allowed to add worksheets Only to subjects
283
296
@named_operation('save')
284
def save(self, req, name, assessable, data):
297
def save(self, req, name, assessable, data, format):
285
298
"""Takes worksheet data and saves it."""
286
self.generate_exerciselist(req, data)
299
self.generate_exerciselist(self.context, req, data)
288
301
self.context.name = unicode(name)
302
self.context.assessable = self.convert_bool(assessable)
289
303
self.context.data = unicode(data)
290
self.context.assessable = self.convert_bool(assessable)
292
return {"result": "ok"}
305
return {"result": "ok"}
307
class OfferingRESTView(JSONRESTView):
308
"""View used to update Offering and create Worksheets."""
310
def get_permissions(self, user):
311
# XXX: Do it properly.
312
# XXX: Lecturers should be allowed to add worksheets Only to subjects
313
# under their control
315
if user.rolenm == 'admin':
316
return set(['add_worksheet'])
322
def __init__(self, req, **kwargs):
324
self.subject = kwargs['subject']
325
self.year = kwargs['year']
326
self.semester = kwargs['semester']
328
self.context = req.store.find(Offering,
329
Offering.subject_id == Subject.id,
330
Subject.code == self.subject,
331
Offering.semester_id == Semester.id,
332
Semester.year == self.year,
333
Semester.semester == self.semester).one()
335
if self.context is None:
338
@named_operation('add_worksheet')
339
def add_worksheet(self,req, identifier, name, assessable, data, format):
340
"""Takes worksheet data and adds it."""
342
new_worksheet = Worksheet()
344
new_worksheet.offering = self.context
345
new_worksheet.identifier = unicode(identifier)
346
new_worksheet.name = unicode(name)
347
new_worksheet.assessable = self.convert_bool(assessable)
348
new_worksheet.data = unicode(data)
349
new_worksheet.format = unicode(format)
350
new_worksheet.seq_no = self.context.worksheets.count()
351
req.store.add(new_worksheet)
353
generate_exerciselist(new_worksheet, req, data)
355
return {"result": "ok"}