134
135
except ValueError:
137
exercise = ivle.database.Exercise.get_by_name(req.store, exercise)
138
worksheet = req.store.find(Worksheet,
139
Worksheet.name == self.worksheet,
138
worksheet_exercise = req.store.find(WorksheetExercise,
139
WorksheetExercise.exercise_id == exercise,
140
WorksheetExercise.worksheet_id == Worksheet.id,
141
Worksheet.identifier == worksheet,
140
142
Worksheet.offering_id == Offering.id,
141
143
Offering.subject_id == Subject.id,
142
Subject.code == self.subject,
144
Subject.code == subject,
143
145
Offering.semester_id == Semester.id,
144
Semester.year == self.year,
145
Semester.semester == self.semester).one()
147
attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
148
exercise, worksheet, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
146
Semester.year == year,
147
Semester.semester == semester).one()
149
attempt = req.store.find(ExerciseAttempt,
150
ExerciseAttempt.login_id == user.id,
151
ExerciseAttempt.ws_ex_id == worksheet_exercise.id,
152
ExerciseAttempt.date == date
150
155
if attempt is None:
170
177
@named_operation('save')
171
178
def save(self, req, text):
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())
179
# Find the appropriate WorksheetExercise to save to. If its not found,
180
# the user is submitting against a non-existant worksheet/exercise
181
worksheet_exercise = req.store.find(WorksheetExercise,
182
WorksheetExercise.exericise_id == self.exercise,
183
WorksheetExercise.worksheet_id == Worksheet.id,
184
Worksheet.offering_id == Offering.id,
185
Offering.subject_id == Subject.id,
186
Subject.code == self.subject,
187
Offering.semester_id == Semester.id,
188
Semester.year == self.year,
189
Semester.semester == self.semester).one()
191
if worksheet_exercise is None:
194
new_save = ExerciseSave()
195
new_save.worksheet_exercise = worksheet_exercise
196
new_save.user = req.user
197
new_save.text = unicode(text)
198
new_save.date = datetime.datetime.now()
199
req.store.add(new_save)
200
return {"result": "ok"}
203
class WorksheetRESTView(JSONRESTView):
204
"""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
def get_permissions(self, user):
253
# XXX: Do it properly.
254
# XXX: Lecturers should be allowed to add worksheets Only to subjects
255
# under their control
257
if user.rolenm == 'admin':
264
def __init__(self, req, **kwargs):
266
self.worksheet = kwargs['worksheet']
267
self.subject = kwargs['subject']
268
self.year = kwargs['year']
269
self.semester = kwargs['semester']
271
self.context = req.store.find(Worksheet,
272
Worksheet.identifier == self.worksheet,
273
Worksheet.offering_id == Offering.id,
274
Offering.subject_id == Subject.id,
275
Subject.code == self.subject,
276
Offering.semester_id == Semester.id,
277
Semester.year == self.year,
278
Semester.semester == self.semester).one()
280
if self.context is None:
283
@named_operation('save')
284
def save(self, req, name, assessable, data):
285
"""Takes worksheet data and saves it."""
286
self.generate_exerciselist(req, data)
288
self.context.name = unicode(name)
289
self.context.data = unicode(data)
290
self.context.assessable = self.convert_bool(assessable)
186
292
return {"result": "ok"}