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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Author: Nick Chadwick


import ivle.database
from ivle.database import Exercise, TestSuite, TestCase, \
                          TestSuiteVar, TestCasePart
from ivle.webapp.base.forms import VALID_URL_NAME
from ivle.webapp.base.rest import (JSONRESTView, write_operation,
                                   require_permission)
from ivle.webapp.errors import NotFound, BadRequest
from ivle.webapp.tutorial.test.TestFramework import (
    TestCreationError, TestError)


class ExercisesRESTView(JSONRESTView):
    """Rest view for adding an exercise."""
    
    #Only lecturers, tutors and admins can add exercises
    def get_permissions(self, user, config):
        if user is not None:
            if user.admin:
                return set(['save'])
            elif 'lecturer' in set((e.role for e in user.active_enrolments)):
                return set(['save'])
            elif 'tutor' in set((e.role for e in user.active_enrolments)):
                return set(['save'])
            else:
                return set()
        else:
            return set()
    
    @write_operation('save')
    def add_exercise(self, req, identifier, name, description, partial, solution, include, num_rows):
        if not VALID_URL_NAME.match(identifier):
            raise BadRequest(
                "Exercise names must consist of a lowercase alphanumeric "
                "character followed by any number of lowercase alphanumerics, "
                "., +, - or _.")

        if req.store.find(Exercise, id=unicode(identifier)).one():
            raise BadRequest("An exercise with that URL name already exists.")

        new_exercise = Exercise()
        new_exercise.id = unicode(identifier)
        new_exercise.name = unicode(name)
        new_exercise.set_description(unicode(description))
        new_exercise.partial = unicode(partial)
        new_exercise.solution = unicode(solution)
        new_exercise.include = unicode(include)
        new_exercise.num_rows = int(num_rows)
        
        req.store.add(new_exercise)
        
        return {'result': 'ok'}
        
        

class ExerciseRESTView(JSONRESTView):
    """View for updating Exercises"""
    
    @write_operation(u'edit')
    def edit_exercise(self, req, name, description, partial, 
                      solution, include, num_rows):
        
        self.context.name = unicode(name)
        self.context.set_description(unicode(description))
        self.context.partial = unicode(partial)
        self.context.solution = unicode(solution)
        self.context.include = unicode(include)
        self.context.num_rows = int(num_rows)
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def delete_exercise(self, req, id):
        
        self.context.delete()
        return {'result': 'ok'}

    @write_operation(u'edit')
    def add_suite(self, req, description, function, stdin):
        
        new_suite = TestSuite()
        new_suite.description = unicode(description)
        new_suite.seq_no = self.context.test_suites.count()
        new_suite.function = unicode(function)
        new_suite.stdin = unicode(stdin)
        new_suite.exercise = self.context
        
        req.store.add(new_suite)
        
        return {'result': 'ok'}
        
    @write_operation(u'edit')
    def edit_suite(self, req, suiteid, description, function, stdin):
        
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        
        if suite is None:
            raise NotFound()
        
        suite.description = unicode(description)
        suite.function = unicode(function)
        suite.stdin = unicode(stdin)
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def delete_suite(self, req, suiteid):
        
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        suite.delete()
        
        return {'result': 'ok'}
      
    @write_operation(u'edit')
    def add_var(self, req, suiteid, var_type, var_name, var_val, argno):

        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        
        if suite is None:
            raise NotFound()
        
        new_var = TestSuiteVar()
        new_var.var_type = unicode(var_type)
        new_var.var_name = unicode(var_name)
        new_var.var_value = unicode(var_val)
        new_var.arg_no = int(argno) if len(argno) else None
        new_var.suite = suite
        
        req.store.add(new_var)
        
        return {'result': 'ok'}

    @write_operation(u'edit')
    def edit_var(self, req, suiteid, varid, var_type, var_name, var_val, argno):
        var = req.store.find(TestSuiteVar,
            TestSuiteVar.varid == int(varid),
            TestSuiteVar.suiteid == int(suiteid)
        ).one()
        
        if var is None:
            raise NotFound("Var not found.")
            
        var.var_type = unicode(var_type)
        var.var_name = unicode(var_name)
        var.var_value = unicode(var_val)
        var.arg_no = int(argno) if len(argno) else None
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def delete_var(self, req, suiteid, varid):
        var = req.store.find(TestSuiteVar,
            TestSuiteVar.varid == int(varid),
            TestSuiteVar.suiteid == int(suiteid)).one()
        if var is None:
            raise NotFound()
        
        var.delete()
        
        return {'result': 'ok'}
        
    @write_operation(u'edit')
    def add_testcase(self, req, suiteid, passmsg, failmsg):
        
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        
        if suite is None:
            raise NotFound()
        
        new_case = TestCase()
        new_case.passmsg = unicode(passmsg)
        new_case.failmsg = unicode(failmsg)
        new_case.seq_no = suite.test_cases.count()
        # XXX: Force this for now, since we don't support the
        #      'match' default. It might make sense to support
        #      this again once file testing support returns.
        new_case.test_default = u'ignore'
        suite.test_cases.add(new_case)
        
        req.store.add(new_case)
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def edit_testcase(self, req, suiteid, testid, passmsg, failmsg):
        
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        test_case = req.store.find(TestCase,
            TestCase.suiteid == suite.suiteid,
            TestCase.testid == int(testid)).one()
        if test_case is None:
            raise NotFound()
        
        test_case.passmsg = unicode(passmsg)
        test_case.failmsg = unicode(failmsg)
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def delete_testcase(self, req, suiteid, testid):
        
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        test_case = req.store.find(TestCase,
            TestCase.suiteid == suite.suiteid,
            TestCase.testid == int(testid)).one()
        if test_case is None:   
            raise NotFound()

        test_case.delete()

        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def edit_testpart(self, req, suiteid, testid, partid, part_type, test_type, 
                      data):
    
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        test_case = req.store.find(TestCase,
            TestCase.suiteid == suite.suiteid,
            TestCase.testid == int(testid)).one()
        if test_case is None:
            raise NotFound()
        
        test_part = req.store.find(TestCasePart,
            TestCasePart.testid == test_case.testid,
            TestCasePart.partid == int(partid)).one()
        if test_part is None:
            raise NotFound('testcasepart')
        
        test_part.part_type = unicode(part_type)
        test_part.test_type = unicode(test_type)
        test_part.data = unicode(data)
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def add_testpart(self, req, suiteid, testid, part_type, test_type, 
                      data):
    
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        test_case = req.store.find(TestCase,
            TestCase.suiteid == suite.suiteid,
            TestCase.testid == int(testid)).one()
        if test_case is None:
            raise NotFound()
        
        test_part = TestCasePart()
        test_part.part_type = unicode(part_type)
        test_part.test_type = unicode(test_type)
        test_part.data = unicode(data)
        
        test_case.parts.add(test_part)
        
        return {'result': 'ok'}
    
    @write_operation(u'edit')
    def delete_testpart(self, req, suiteid, testid, partid):
        suite = req.store.find(TestSuite,
            TestSuite.suiteid == int(suiteid),
            TestSuite.exercise_id == self.context.id).one()
        if suite is None:
            raise NotFound()
        
        test_case = req.store.find(TestCase,
            TestCase.suiteid == suite.suiteid,
            TestCase.testid == int(testid)).one()
        if test_case is None:
            raise NotFound()
        
        test_part = req.store.find(TestCasePart,
            TestCasePart.testid == test_case.testid,
            TestCasePart.partid == int(partid)).one()
        if test_part is None:
            raise NotFound()
        
        test_part.delete()
        
        return {'result': 'ok'}

    @write_operation(u'edit')
    def test(self, req, code):
        from ivle.worksheet.utils import test_exercise_submission
        try:
            return test_exercise_submission(
                req.config, req.user, self.context, code)
        except TestCreationError, e:
            return {'critical_error': {'name': 'TestCreationError', 'detail': e._reason}}
        except TestError, e:
            return {'critical_error': {'name': 'TestError', 'detail': str(e)}}