170
170
return {'code': self.context.text}
173
class ExerciseRESTView(JSONRESTView):
174
'''REST view of an exercise.'''
176
def get_permissions(self, user):
177
# XXX: Do it properly.
178
# XXX: Does any user have the ability to save as themselves?
179
# XXX: Does a user EVER have permission to save as another user?
185
@named_operation('save')
173
class WorksheetExerciseRESTView(JSONRESTView):
174
'''REST view of a worksheet exercise.'''
176
def __init__(self, req, subject, year, semester, worksheet, exercise):
177
self.context = req.store.find(WorksheetExercise,
178
WorksheetExercise.exercise_id == exercise,
179
WorksheetExercise.worksheet_id == Worksheet.id,
180
Worksheet.offering_id == Offering.id,
181
Offering.subject_id == Subject.id,
182
Subject.code == subject,
183
Offering.semester_id == Semester.id,
184
Semester.year == year,
185
Semester.semester == semester).one()
187
if self.context is None:
190
@named_operation('view')
186
191
def save(self, req, text):
187
192
# Find the appropriate WorksheetExercise to save to. If its not found,
188
193
# the user is submitting against a non-existant worksheet/exercise
189
worksheet_exercise = req.store.find(WorksheetExercise,
190
WorksheetExercise.exercise_id == self.exercise,
191
WorksheetExercise.worksheet_id == Worksheet.id,
192
Worksheet.offering_id == Offering.id,
193
Offering.subject_id == Subject.id,
194
Subject.code == self.subject,
195
Offering.semester_id == Semester.id,
196
Semester.year == self.year,
197
Semester.semester == self.semester).one()
199
if worksheet_exercise is None:
202
195
old_save = req.store.find(ExerciseSave,
203
ExerciseSave.ws_ex_id == worksheet_exercise.id,
196
ExerciseSave.ws_ex_id == self.context.id,
204
197
ExerciseSave.user == req.user).one()
206
199
#Overwrite the old, or create a new if there isn't one
224
217
class WorksheetRESTView(JSONRESTView):
225
218
"""View used to update a worksheet."""
227
def get_permissions(self, user):
228
# XXX: Do it properly.
229
# XXX: Lecturers should be allowed to add worksheets Only to subjects
230
# under their control
239
220
def __init__(self, req, **kwargs):
241
222
self.worksheet = kwargs['worksheet']
269
250
class WorksheetsRESTView(JSONRESTView):
270
251
"""View used to update and create Worksheets."""
272
def get_permissions(self, user):
273
# XXX: Do it properly.
274
# XXX: Lecturers should be allowed to add worksheets Only to subjects
275
# under their control
284
253
def __init__(self, req, **kwargs):
286
255
self.subject = kwargs['subject']