~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/tutorial/exercise_service.py

  • Committer: William Grant
  • Date: 2009-03-17 04:48:07 UTC
  • mfrom: (1099.1.243 exercise-ui)
  • Revision ID: grantw@unimelb.edu.au-20090317044807-pozdt54fapazp2sp
Merge lp:~ivle-dev/ivle/exercise-ui.

Lecturers can now add and edit exercises, and worksheets can be written
in RST directly inside IVLE.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# IVLE - Informatics Virtual Learning Environment
 
2
# Copyright (C) 2007-2009 The University of Melbourne
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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
 
17
 
 
18
# Author: Nick Chadwick
 
19
 
 
20
 
 
21
import ivle.database
 
22
from ivle.database import Exercise, TestSuite, TestCase, \
 
23
                          TestSuiteVar, TestCasePart
 
24
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
 
25
                                   require_permission)
 
26
from ivle.webapp.errors import NotFound, BadRequest
 
27
 
 
28
 
 
29
class ExercisesRESTView(JSONRESTView):
 
30
    """Rest view for adding an exercise."""
 
31
    
 
32
    #Only lecturers and admins can add exercises
 
33
    def get_permissions(self, user):
 
34
        if user is not None:
 
35
            if user.admin:
 
36
                return set(['save'])
 
37
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
 
38
                return set(['save'])
 
39
            else:
 
40
                return set()
 
41
        else:
 
42
            return set()
 
43
    
 
44
    @named_operation('save')
 
45
    def add_exercise(self, req, identifier, name, description, partial, solution, include, num_rows):
 
46
    
 
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)
 
55
        
 
56
        req.store.add(new_exercise)
 
57
        
 
58
        return {'result': 'ok'}
 
59
        
 
60
        
 
61
 
 
62
class ExerciseRESTView(JSONRESTView):
 
63
    """View for updating Exercises"""
 
64
    
 
65
    def __init__(self, req, exercise):
 
66
        
 
67
        self.context = req.store.find(Exercise,
 
68
            Exercise.id == exercise).one()
 
69
        
 
70
        if self.context is None:
 
71
            raise NotFound()
 
72
 
 
73
    @named_operation(u'edit')
 
74
    def edit_exercise(self, req, name, description, partial, 
 
75
                      solution, include, num_rows):
 
76
        
 
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'}
 
84
    
 
85
    @named_operation(u'edit')
 
86
    def delete_exercise(self, req, id):
 
87
        
 
88
        self.context.delete()
 
89
        return {'result': 'ok'}
 
90
 
 
91
    @named_operation(u'edit')
 
92
    def add_suite(self, req, description, function, stdin):
 
93
        
 
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
 
100
        
 
101
        req.store.add(new_suite)
 
102
        
 
103
        return {'result': 'ok'}
 
104
        
 
105
    @named_operation(u'edit')
 
106
    def edit_suite(self, req, suiteid, description, function, stdin):
 
107
        
 
108
        suite = req.store.find(TestSuite,
 
109
            TestSuite.suiteid == int(suiteid),
 
110
            TestSuite.exercise_id == self.context.id).one()
 
111
        
 
112
        if suite is None:
 
113
            raise NotFound()
 
114
        
 
115
        suite.description = unicode(description)
 
116
        suite.function = unicode(function)
 
117
        suite.stdin = unicode(stdin)
 
118
        
 
119
        return {'result': 'ok'}
 
120
    
 
121
    @named_operation(u'edit')
 
122
    def delete_suite(self, req, suiteid):
 
123
        
 
124
        suite = req.store.find(TestSuite,
 
125
            TestSuite.suiteid == int(suiteid),
 
126
            TestSuite.exercise_id == self.context.id).one()
 
127
        if suite is None:
 
128
            raise NotFound()
 
129
        
 
130
        suite.delete()
 
131
        
 
132
        return {'result': 'ok'}
 
133
      
 
134
    @named_operation(u'edit')
 
135
    def add_var(self, req, suiteid, var_type, var_name, var_val, argno):
 
136
 
 
137
        suite = req.store.find(TestSuite,
 
138
            TestSuite.suiteid == int(suiteid),
 
139
            TestSuite.exercise_id == self.context.id).one()
 
140
        
 
141
        if suite is None:
 
142
            raise NotFound()
 
143
        
 
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
 
150
        
 
151
        req.store.add(new_var)
 
152
        
 
153
        return {'result': 'ok'}
 
154
 
 
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)
 
160
        ).one()
 
161
        
 
162
        if var is None:
 
163
            raise NotFound("Var not found.")
 
164
            
 
165
        var.var_type = unicode(var_type)
 
166
        var.var_name = unicode(var_name)
 
167
        var.var_val = unicode(var_val)
 
168
        var.argno = argno
 
169
        
 
170
        return {'result': 'ok'}
 
171
    
 
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()
 
177
        if var is None:
 
178
            raise NotFound()
 
179
        
 
180
        var.delete()
 
181
        
 
182
        return {'result': 'ok'}
 
183
        
 
184
    @named_operation(u'edit')
 
185
    def add_testcase(self, req, suiteid, passmsg, failmsg, default):
 
186
        
 
187
        suite = req.store.find(TestSuite,
 
188
            TestSuite.suiteid == int(suiteid),
 
189
            TestSuite.exercise_id == self.context.id).one()
 
190
        
 
191
        if suite is None:
 
192
            raise NotFound()
 
193
        
 
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)
 
200
        
 
201
        req.store.add(new_case)
 
202
        
 
203
        return {'result': 'ok'}
 
204
    
 
205
    @named_operation(u'edit')
 
206
    def edit_testcase(self, req, suiteid, testid, passmsg, failmsg, default):
 
207
        
 
208
        suite = req.store.find(TestSuite,
 
209
            TestSuite.suiteid == int(suiteid),
 
210
            TestSuite.exercise_id == self.context.id).one()
 
211
        if suite is None:
 
212
            raise NotFound()
 
213
        
 
214
        test_case = req.store.find(TestCase,
 
215
            TestCase.suiteid == suite.suiteid,
 
216
            TestCase.testid == int(testid)).one()
 
217
        if test_case is None:
 
218
            raise NotFound()
 
219
        
 
220
        test_case.passmsg = unicode(passmsg)
 
221
        test_case.failmsg = unicode(failmsg)
 
222
        test_case.default = unicode(default)
 
223
        
 
224
        return {'result': 'ok'}
 
225
    
 
226
    @named_operation(u'edit')
 
227
    def delete_testcase(self, req, suiteid, testid):
 
228
        
 
229
        suite = req.store.find(TestSuite,
 
230
            TestSuite.suiteid == int(suiteid),
 
231
            TestSuite.exercise_id == self.context.id).one()
 
232
        if suite is None:
 
233
            raise NotFound()
 
234
        
 
235
        test_case = req.store.find(TestCase,
 
236
            TestCase.suiteid == suite.suiteid,
 
237
            TestCase.testid == int(testid)).one()
 
238
        if test_case is None:   
 
239
            raise NotFound()
 
240
 
 
241
        test_case.delete()
 
242
 
 
243
        return {'result': 'ok'}
 
244
    
 
245
    @named_operation(u'edit')
 
246
    def edit_testpart(self, req, suiteid, testid, partid, part_type, test_type, 
 
247
                      data, filename):
 
248
    
 
249
        suite = req.store.find(TestSuite,
 
250
            TestSuite.suiteid == int(suiteid),
 
251
            TestSuite.exercise_id == self.context.id).one()
 
252
        if suite is None:
 
253
            raise NotFound()
 
254
        
 
255
        test_case = req.store.find(TestCase,
 
256
            TestCase.suiteid == suite.suiteid,
 
257
            TestCase.testid == int(testid)).one()
 
258
        if test_case is None:
 
259
            raise NotFound()
 
260
        
 
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')
 
266
        
 
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)
 
271
        
 
272
        return {'result': 'ok'}
 
273
    
 
274
    @named_operation(u'edit')
 
275
    def add_testpart(self, req, suiteid, testid, part_type, test_type, 
 
276
                      data, filename):
 
277
    
 
278
        suite = req.store.find(TestSuite,
 
279
            TestSuite.suiteid == int(suiteid),
 
280
            TestSuite.exercise_id == self.context.id).one()
 
281
        if suite is None:
 
282
            raise NotFound()
 
283
        
 
284
        test_case = req.store.find(TestCase,
 
285
            TestCase.suiteid == suite.suiteid,
 
286
            TestCase.testid == int(testid)).one()
 
287
        if test_case is None:
 
288
            raise NotFound()
 
289
        
 
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)
 
295
        
 
296
        test_case.parts.add(test_part)
 
297
        
 
298
        return {'result': 'ok'}
 
299
    
 
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()
 
305
        if suite is None:
 
306
            raise NotFound()
 
307
        
 
308
        test_case = req.store.find(TestCase,
 
309
            TestCase.suiteid == suite.suiteid,
 
310
            TestCase.testid == int(testid)).one()
 
311
        if test_case is None:
 
312
            raise NotFound()
 
313
        
 
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:
 
318
            raise NotFound()
 
319
        
 
320
        test_part.delete()
 
321
        
 
322
        return {'result': 'ok'}