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

1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
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)
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
26
from ivle.webapp.errors import NotFound, BadRequest
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
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:
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
35
            if user.admin:
36
                return set(['save'])
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
37
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
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)
1099.1.229 by Nick Chadwick
Fixed a slight oversight, which meant there was no way to add a new
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)
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
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
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
73
    @named_operation(u'edit')
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
74
    def edit_exercise(self, req, name, description, partial, 
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
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)
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
83
        return {'result': 'ok'}
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
84
    
85
    @named_operation(u'edit')
86
    def delete_exercise(self, req, id):
87
        
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
88
        self.context.delete()
89
        return {'result': 'ok'}
90
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
91
    @named_operation(u'edit')
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
92
    def add_suite(self, req, description, function, stdin):
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
93
        
94
        new_suite = TestSuite()
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
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)
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
99
        new_suite.exercise = self.context
100
        
101
        req.store.add(new_suite)
102
        
103
        return {'result': 'ok'}
104
        
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
105
    @named_operation(u'edit')
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
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'}
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
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
        
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
130
        suite.delete()
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
131
        
132
        return {'result': 'ok'}
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
133
      
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
134
    @named_operation(u'edit')
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
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'}
1099.1.220 by Nick Chadwick
Merged from trunk
154
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
155
    @named_operation(u'edit')
1099.1.220 by Nick Chadwick
Merged from trunk
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:
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
163
            raise NotFound("Var not found.")
1099.1.220 by Nick Chadwick
Merged from trunk
164
            
165
        var.var_type = unicode(var_type)
166
        var.var_name = unicode(var_name)
167
        var.var_val = unicode(var_val)
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
168
        var.argno = argno
1099.1.220 by Nick Chadwick
Merged from trunk
169
        
170
        return {'result': 'ok'}
171
    
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
172
    @named_operation(u'edit')
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
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
        
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
180
        var.delete()
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
181
        
182
        return {'result': 'ok'}
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
183
        
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
184
    @named_operation(u'edit')
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
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'}
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
212
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
218
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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')
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
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()
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
238
        if test_case is None:   
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
239
            raise NotFound()
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
240
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
241
        test_case.delete()
1099.1.237 by Nick Chadwick
Updated exercise_service to make use of the new deletion methods
242
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
243
        return {'result': 'ok'}
244
    
245
    @named_operation(u'edit')
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
253
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
259
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
282
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
288
            raise NotFound()
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
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'}
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
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:
1099.1.238 by Nick Chadwick
Removed some extraneous output in exercise REST views, when encountering
318
            raise NotFound()
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
319
        
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
320
        test_part.delete()
1099.6.3 by Nick Chadwick
Edited the exercise service to delete individual parts of an exercise.
321
        
322
        return {'result': 'ok'}