1
# IVLE - Informatics Virtual Learning Environment
2
# Copyright (C) 2007-2009 The University of Melbourne
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# Author: Nick Chadwick
22
from ivle.database import Exercise, TestSuite, TestCase, \
23
TestSuiteVar, TestCasePart
24
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
26
from ivle.webapp.errors import NotFound, BadRequest
29
class ExercisesRESTView(JSONRESTView):
30
"""Rest view for adding an exercise."""
32
#Only lecturers and admins can add exercises
33
def get_permissions(self, user):
37
elif 'lecturer' in set((e.role for e in user.active_enrolments)):
44
@named_operation('save')
45
def add_exercise(self, req, identifier, name, description, partial, solution, include, num_rows):
47
new_exercise = Exercise()
48
new_exercise.id = unicode(identifier)
49
new_exercise.name = unicode(name)
50
new_exercise.description = unicode(description)
51
new_exercise.partial = unicode(partial)
52
new_exercise.solution = unicode(solution)
53
new_exercise.include = unicode(include)
54
new_exercise.num_rows = int(num_rows)
56
req.store.add(new_exercise)
58
return {'result': 'ok'}
62
class ExerciseRESTView(JSONRESTView):
63
"""View for updating Exercises"""
65
def __init__(self, req, exercise):
67
self.context = req.store.find(Exercise,
68
Exercise.id == exercise).one()
70
if self.context is None:
73
@named_operation(u'edit')
74
def edit_exercise(self, req, name, description, partial,
75
solution, include, num_rows):
77
self.context.name = unicode(name)
78
self.context.description = unicode(description)
79
self.context.partial = unicode(partial)
80
self.context.solution = unicode(solution)
81
self.context.include = unicode(include)
82
self.context.num_rows = int(num_rows)
83
return {'result': 'ok'}
85
@named_operation(u'edit')
86
def delete_exercise(self, req, id):
89
return {'result': 'ok'}
91
@named_operation(u'edit')
92
def add_suite(self, req, description, function, stdin):
94
new_suite = TestSuite()
95
new_suite.description = unicode(description)
96
new_suite.seq_no = self.context.test_suites.count()
97
new_suite.function = unicode(function)
98
new_suite.stdin = unicode(stdin)
99
new_suite.exercise = self.context
101
req.store.add(new_suite)
103
return {'result': 'ok'}
105
@named_operation(u'edit')
106
def edit_suite(self, req, suiteid, description, function, stdin):
108
suite = req.store.find(TestSuite,
109
TestSuite.suiteid == int(suiteid),
110
TestSuite.exercise_id == self.context.id).one()
115
suite.description = unicode(description)
116
suite.function = unicode(function)
117
suite.stdin = unicode(stdin)
119
return {'result': 'ok'}
121
@named_operation(u'edit')
122
def delete_suite(self, req, suiteid):
124
suite = req.store.find(TestSuite,
125
TestSuite.suiteid == int(suiteid),
126
TestSuite.exercise_id == self.context.id).one()
132
return {'result': 'ok'}
134
@named_operation(u'edit')
135
def add_var(self, req, suiteid, var_type, var_name, var_val, argno):
137
suite = req.store.find(TestSuite,
138
TestSuite.suiteid == int(suiteid),
139
TestSuite.exercise_id == self.context.id).one()
144
new_var = TestSuiteVar()
145
new_var.var_type = unicode(var_type)
146
new_var.var_name = unicode(var_name)
147
new_var.var_val = unicode(var_val)
148
new_var.argno = argno
149
new_var.suite = suite
151
req.store.add(new_var)
153
return {'result': 'ok'}
155
@named_operation(u'edit')
156
def edit_var(self, req, suiteid, varid, var_type, var_name, var_val, argno):
157
var = req.store.find(TestSuiteVar,
158
TestSuiteVar.varid == int(varid),
159
TestSuiteVar.suiteid == int(suiteid)
163
raise NotFound("Var not found.")
165
var.var_type = unicode(var_type)
166
var.var_name = unicode(var_name)
167
var.var_val = unicode(var_val)
170
return {'result': 'ok'}
172
@named_operation(u'edit')
173
def delete_var(self, req, suiteid, varid):
174
var = req.store.find(TestSuiteVar,
175
TestSuiteVar.varid == int(varid),
176
TestSuiteVar.suiteid == int(suiteid)).one()
182
return {'result': 'ok'}
184
@named_operation(u'edit')
185
def add_testcase(self, req, suiteid, passmsg, failmsg, default):
187
suite = req.store.find(TestSuite,
188
TestSuite.suiteid == int(suiteid),
189
TestSuite.exercise_id == self.context.id).one()
194
new_case = TestCase()
195
new_case.passmsg = unicode(passmsg)
196
new_case.failmsg = unicode(failmsg)
197
new_case.default = unicode(default)
198
new_case.seq_no = suite.test_cases.count()
199
suite.test_cases.add(new_case)
201
req.store.add(new_case)
203
return {'result': 'ok'}
205
@named_operation(u'edit')
206
def edit_testcase(self, req, suiteid, testid, passmsg, failmsg, default):
208
suite = req.store.find(TestSuite,
209
TestSuite.suiteid == int(suiteid),
210
TestSuite.exercise_id == self.context.id).one()
214
test_case = req.store.find(TestCase,
215
TestCase.suiteid == suite.suiteid,
216
TestCase.testid == int(testid)).one()
217
if test_case is None:
220
test_case.passmsg = unicode(passmsg)
221
test_case.failmsg = unicode(failmsg)
222
test_case.default = unicode(default)
224
return {'result': 'ok'}
226
@named_operation(u'edit')
227
def delete_testcase(self, req, suiteid, testid):
229
suite = req.store.find(TestSuite,
230
TestSuite.suiteid == int(suiteid),
231
TestSuite.exercise_id == self.context.id).one()
235
test_case = req.store.find(TestCase,
236
TestCase.suiteid == suite.suiteid,
237
TestCase.testid == int(testid)).one()
238
if test_case is None:
243
return {'result': 'ok'}
245
@named_operation(u'edit')
246
def edit_testpart(self, req, suiteid, testid, partid, part_type, test_type,
249
suite = req.store.find(TestSuite,
250
TestSuite.suiteid == int(suiteid),
251
TestSuite.exercise_id == self.context.id).one()
255
test_case = req.store.find(TestCase,
256
TestCase.suiteid == suite.suiteid,
257
TestCase.testid == int(testid)).one()
258
if test_case is None:
261
test_part = req.store.find(TestCasePart,
262
TestCasePart.testid == test_case.testid,
263
TestCasePart.partid == int(partid)).one()
264
if test_part is None:
265
raise NotFound('testcasepart')
267
test_part.part_type = unicode(part_type)
268
test_part.test_type = unicode(test_type)
269
test_part.data = unicode(data)
270
test_part.filename = unicode(filename)
272
return {'result': 'ok'}
274
@named_operation(u'edit')
275
def add_testpart(self, req, suiteid, testid, part_type, test_type,
278
suite = req.store.find(TestSuite,
279
TestSuite.suiteid == int(suiteid),
280
TestSuite.exercise_id == self.context.id).one()
284
test_case = req.store.find(TestCase,
285
TestCase.suiteid == suite.suiteid,
286
TestCase.testid == int(testid)).one()
287
if test_case is None:
290
test_part = TestCasePart()
291
test_part.part_type = unicode(part_type)
292
test_part.test_type = unicode(test_type)
293
test_part.data = unicode(data)
294
test_part.filename = unicode(filename)
296
test_case.parts.add(test_part)
298
return {'result': 'ok'}
300
@named_operation(u'edit')
301
def delete_testpart(self, req, suiteid, testid, partid):
302
suite = req.store.find(TestSuite,
303
TestSuite.suiteid == int(suiteid),
304
TestSuite.exercise_id == self.context.id).one()
308
test_case = req.store.find(TestCase,
309
TestCase.suiteid == suite.suiteid,
310
TestCase.testid == int(testid)).one()
311
if test_case is None:
314
test_part = req.store.find(TestCasePart,
315
TestCasePart.testid == test_case.testid,
316
TestCasePart.partid == int(partid)).one()
317
if test_part is None:
322
return {'result': 'ok'}