136
135
except ValueError:
139
worksheet_exercise = req.store.find(WorksheetExercise,
140
WorksheetExercise.exercise_id == exercise,
141
WorksheetExercise.worksheet_id == Worksheet.id,
142
Worksheet.identifier == worksheet,
138
exercise = ivle.database.Exercise.get_by_name(req.store, exercise)
139
worksheet = req.store.find(Worksheet,
140
Worksheet.name == self.worksheet,
143
141
Worksheet.offering_id == Offering.id,
144
142
Offering.subject_id == Subject.id,
145
Subject.code == subject,
143
Subject.code == self.subject,
146
144
Offering.semester_id == Semester.id,
147
Semester.year == year,
148
Semester.semester == semester).one()
145
Semester.year == self.year,
146
Semester.semester == self.semester).one()
150
148
attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
151
worksheet_exercise, as_of=date,
152
allow_inactive=HISTORY_ALLOW_INACTIVE)
149
exercise, worksheet, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
154
151
if attempt is None:
176
171
@named_operation('save')
177
172
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,
173
# Need to open JUST so we know this is a real exercise.
174
# (This avoids users submitting code for bogus exercises).
175
exercise = req.store.find(Exercise,
176
Exercise.id == self.exercise).one()
177
worksheet = req.store.find(Worksheet,
178
Worksheet.name == self.worksheet,
183
179
Worksheet.offering_id == Offering.id,
184
180
Offering.subject_id == Subject.id,
185
181
Subject.code == self.subject,
186
182
Offering.semester_id == Semester.id,
187
183
Semester.year == self.year,
188
184
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()
185
ivle.worksheet.save_exercise(req.store, req.user, exercise, worksheet,
186
unicode(text), datetime.datetime.now())
209
187
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
189
class WorksheetRESTView(JSONRESTView):
262
190
"""View used to update a worksheet."""
192
def generate_exerciselist(self, req, worksheet):
193
"""Runs through the worksheetstream, generating the appropriate
194
WorksheetExercises, and de-activating the old ones."""
196
# Turns the worksheet into an xml stream, and then finds all the
197
# exercise nodes in the stream.
198
worksheet = genshi.XML(worksheet)
199
for kind, data, pos in worksheet:
200
if kind is genshi.core.START:
201
# Data is a tuple of tag name and a list of name->value tuples
202
if data[0] == 'exercise':
208
if attr[0] == 'optional':
209
optional = attr[1] == 'true'
211
exercises.append((src, optional))
213
# Set all current worksheet_exercises to be inactive
214
db_worksheet_exercises = req.store.find(WorksheetExercise,
215
WorksheetExercise.worksheet_id == self.context.id)
216
for worksheet_exercise in db_worksheet_exercises:
217
worksheet_exercise.active = False
219
for exerciseid, optional in exercises:
220
worksheet_exercise = req.store.find(WorksheetExercise,
221
WorksheetExercise.worksheet_id == self.context.id,
222
Exercise.id == WorksheetExercise.exercise_id,
223
Exercise.id == exerciseid).one()
224
if worksheet_exercise is None:
225
exercise = req.store.find(Exercise,
226
Exercise.id == exerciseid
230
worksheet_exercise = WorksheetExercise()
231
worksheet_exercise.worksheet_id = self.context.id
232
worksheet_exercise.exercise_id = exercise.id
233
req.store.add(worksheet_exercise)
234
worksheet_exercise.active = True
235
worksheet_exercise.seq_no = ex_num
236
worksheet_exercise.optional = optional
264
238
def get_permissions(self, user):
265
239
# XXX: Do it properly.
266
# XXX: Lecturers should be allowed to add worksheets Only to subjects
267
# under their control
268
240
if user is not None:
269
241
if user.rolenm == 'admin':
270
242
return set(['save'])
295
267
@named_operation('save')
296
def save(self, req, name, assessable, data, format):
268
def save(self, req, name, assessable, data):
297
269
"""Takes worksheet data and saves it."""
298
generate_exerciselist(self.context, req, data)
270
self.generate_exerciselist(req, data)
300
272
self.context.name = unicode(name)
273
self.context.data = unicode(data)
301
274
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'}
276
return {"result": "ok"}