176
170
@named_operation('save')
177
171
def save(self, req, text):
178
# Find the appropriate WorksheetExercise to save to. If its not found,
179
# the user is submitting against a non-existant worksheet/exercise
180
worksheet_exercise = req.store.find(WorksheetExercise,
181
WorksheetExercise.exercise_id == self.exercise,
182
WorksheetExercise.worksheet_id == Worksheet.id,
183
Worksheet.offering_id == Offering.id,
184
Offering.subject_id == Subject.id,
185
Subject.code == self.subject,
186
Offering.semester_id == Semester.id,
187
Semester.year == self.year,
188
Semester.semester == self.semester).one()
190
if worksheet_exercise is None:
193
old_save = req.store.find(ExerciseSave,
194
ExerciseSave.ws_ex_id == worksheet_exercise.id,
195
ExerciseSave.user == req.user).one()
197
#Overwrite the old, or create a new if there isn't one
199
new_save = ExerciseSave()
200
req.store.add(new_save)
204
new_save.worksheet_exercise = worksheet_exercise
205
new_save.user = req.user
206
new_save.text = unicode(text)
207
new_save.date = datetime.datetime.now()
209
return {"result": "ok"}
212
def generate_exerciselist(worksheet, req, worksheetdata):
213
"""Runs through the worksheetstream, generating the appropriate
214
WorksheetExercises, and de-activating the old ones."""
216
# Turns the worksheet into an xml stream, and then finds all the
217
# exercise nodes in the stream.
218
worksheetdata = genshi.XML(worksheetdata)
219
for kind, data, pos in worksheetdata:
220
if kind is genshi.core.START:
221
# Data is a tuple of tag name and a list of name->value tuples
222
if data[0] == 'exercise':
228
if attr[0] == 'optional':
229
optional = attr[1] == 'true'
231
exercises.append((src, optional))
233
# Set all current worksheet_exercises to be inactive
234
db_worksheet_exercises = req.store.find(WorksheetExercise,
235
WorksheetExercise.worksheet_id == worksheet.id)
236
for worksheet_exercise in db_worksheet_exercises:
237
worksheet_exercise.active = False
239
for exerciseid, optional in exercises:
240
worksheet_exercise = req.store.find(WorksheetExercise,
241
WorksheetExercise.worksheet_id == worksheet.id,
242
Exercise.id == WorksheetExercise.exercise_id,
243
Exercise.id == exerciseid).one()
244
if worksheet_exercise is None:
245
exercise = req.store.find(Exercise,
246
Exercise.id == exerciseid
250
worksheet_exercise = WorksheetExercise()
251
worksheet_exercise.worksheet_id = worksheet.id
252
worksheet_exercise.exercise_id = exercise.id
253
req.store.add(worksheet_exercise)
254
worksheet_exercise.active = True
255
worksheet_exercise.seq_no = ex_num
256
worksheet_exercise.optional = optional
259
# Note that this is the view of an existing worksheet. Creation is handled
260
# by OfferingRESTView (as offerings have worksheets)
261
class WorksheetRESTView(JSONRESTView):
262
"""View used to update a worksheet."""
264
def get_permissions(self, user):
265
# XXX: Do it properly.
266
# XXX: Lecturers should be allowed to add worksheets Only to subjects
267
# under their control
269
if user.rolenm == 'admin':
276
def __init__(self, req, **kwargs):
278
self.worksheet = kwargs['worksheet']
279
self.subject = kwargs['subject']
280
self.year = kwargs['year']
281
self.semester = kwargs['semester']
283
self.context = req.store.find(Worksheet,
284
Worksheet.identifier == self.worksheet,
285
Worksheet.offering_id == Offering.id,
286
Offering.subject_id == Subject.id,
287
Subject.code == self.subject,
288
Offering.semester_id == Semester.id,
289
Semester.year == self.year,
290
Semester.semester == self.semester).one()
292
if self.context is None:
295
@named_operation('save')
296
def save(self, req, name, assessable, data, format):
297
"""Takes worksheet data and saves it."""
298
generate_exerciselist(self.context, req, data)
300
self.context.name = unicode(name)
301
self.context.assessable = self.convert_bool(assessable)
302
self.context.data = unicode(data)
303
self.context.format = unicode(format)
305
return {"result": "ok"}
307
class WorksheetsRESTView(JSONRESTView):
308
"""View used to update 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':
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('edit')
339
def add_worksheet(self, req, identifier, name, assessable, data, format):
340
"""Takes worksheet data and adds it."""
342
new_worksheet = Worksheet()
343
new_worksheet.seq_no = self.context.worksheets.count()
344
# Setting new_worksheet.offering implicitly adds new_worksheet,
345
# hence worksheets.count MUST be called above it
346
new_worksheet.offering = self.context
347
new_worksheet.identifier = unicode(identifier)
348
new_worksheet.name = unicode(name)
349
new_worksheet.assessable = self.convert_bool(assessable)
350
new_worksheet.data = unicode(data)
351
new_worksheet.format = unicode(format)
353
# This call is added for clarity, as the worksheet is implicitly added.
354
req.store.add(new_worksheet)
356
generate_exerciselist(new_worksheet, req, data)
358
return {"result": "ok"}
360
@named_operation('edit')
361
def move_up(self, req, worksheetid):
362
"""Takes a list of worksheet-seq_no pairs and updates their
363
corresponding Worksheet objects to match."""
365
worksheet_below = req.store.find(Worksheet,
366
Worksheet.offering_id == self.context.id,
367
Worksheet.identifier == unicode(worksheetid)).one()
368
if worksheet_below is None:
369
raise NotFound('worksheet_below')
370
worksheet_above = req.store.find(Worksheet,
371
Worksheet.offering_id == self.context.id,
372
Worksheet.seq_no == (worksheet_below.seq_no - 1)).one()
373
if worksheet_above is None:
374
raise NotFound('worksheet_above')
376
worksheet_below.seq_no = worksheet_below.seq_no - 1
377
worksheet_above.seq_no = worksheet_above.seq_no + 1
379
return {'result': 'ok'}
381
@named_operation('edit')
382
def move_down(self, req, worksheetid):
383
"""Takes a list of worksheet-seq_no pairs and updates their
384
corresponding Worksheet objects to match."""
386
worksheet_above = req.store.find(Worksheet,
387
Worksheet.offering_id == self.context.id,
388
Worksheet.identifier == unicode(worksheetid)).one()
389
if worksheet_above is None:
390
raise NotFound('worksheet_below')
391
worksheet_below = req.store.find(Worksheet,
392
Worksheet.offering_id == self.context.id,
393
Worksheet.seq_no == (worksheet_above.seq_no + 1)).one()
394
if worksheet_below is None:
395
raise NotFound('worksheet_above')
397
worksheet_below.seq_no = worksheet_below.seq_no - 1
398
worksheet_above.seq_no = worksheet_above.seq_no + 1
400
return {'result': 'ok'}
172
# Need to open JUST so we know this is a real exercise.
173
# (This avoids users submitting code for bogus exercises).
174
exercise = req.store.find(Exercise,
175
Exercise.id == self.exercise).one()
176
worksheet = req.store.find(Worksheet,
177
Worksheet.name == self.worksheet,
178
Worksheet.offering_id == Offering.id,
179
Offering.subject_id == Subject.id,
180
Subject.code == self.subject,
181
Offering.semester_id == Semester.id,
182
Semester.year == self.year,
183
Semester.semester == self.semester).one()
184
ivle.worksheet.save_exercise(req.store, req.user, exercise, worksheet,
185
unicode(text), datetime.datetime.now())
186
return {"result": "ok"}