~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
# 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: Matt Giuca, Nick Chadwick

'''AJAX backend for the tutorial application.'''

import os
import datetime
import genshi

import ivle.console
import ivle.database
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
                          Offering, Subject, Semester, WorksheetExercise
import ivle.worksheet.utils
import ivle.webapp.tutorial.test
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
                                   require_permission)
from ivle.webapp.errors import NotFound

# If True, getattempts or getattempt will allow browsing of inactive/disabled
# attempts. If False, will not allow this.
HISTORY_ALLOW_INACTIVE = False

TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'

class AttemptsRESTView(JSONRESTView):
    '''REST view of a user's attempts at an exercise.'''
    
    def __init__(self, req, subject, year, semester, worksheet, 
                                                exercise, username):
        self.user = ivle.database.User.get_by_login(req.store, username)
        if self.user is None:
            raise NotFound()
        
        self.worksheet_exercise = req.store.find(WorksheetExercise,
            WorksheetExercise.exercise_id == unicode(exercise),
            WorksheetExercise.worksheet_id == Worksheet.id,
            Worksheet.offering_id == Offering.id,
            Worksheet.identifier == unicode(worksheet),
            Offering.subject_id == Subject.id,
            Subject.short_name == subject,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester).one()
        
        self.context = self.user # XXX: Not quite right.

    @require_permission('edit')
    def GET(self, req):
        """Handles a GET Attempts action."""
        attempts = req.store.find(ExerciseAttempt, 
                ExerciseAttempt.ws_ex_id == self.worksheet_exercise.id,
                ExerciseAttempt.user_id == self.user.id)
        # attempts is a list of ExerciseAttempt objects. Convert to dictionaries
        time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
        attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
                for a in attempts]

        return attempts


    @require_permission('edit')
    def PUT(self, req, data):
        """ Tests the given submission """
        exercise = req.store.find(Exercise, 
            Exercise.id == self.worksheet_exercise.exercise_id).one()
        if exercise is None:
            raise NotFound()

        # Start a console to run the tests on
        jail_path = os.path.join(req.config['paths']['jails']['mounts'],
                                 req.user.login)
        working_dir = os.path.join("/home", req.user.login)
        cons = ivle.console.Console(req.user.unixid, jail_path, working_dir)

        # Parse the file into a exercise object using the test suite
        exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
                                                            exercise, cons)

        # Run the test cases. Get the result back as a JSONable object.
        # Return it.
        test_results = exercise_obj.run_tests(data['code'])

        # Close the console
        cons.close()

        attempt = ivle.database.ExerciseAttempt(user=req.user,
            worksheet_exercise = self.worksheet_exercise,
            date = datetime.datetime.now(),
            complete = test_results['passed'],
            text = unicode(data['code'])
        )

        req.store.add(attempt)

        # Query the DB to get an updated score on whether or not this problem
        # has EVER been completed (may be different from "passed", if it has
        # been completed before), and the total number of attempts.
        completed, attempts = ivle.worksheet.utils.get_exercise_status(
                req.store, req.user, self.worksheet_exercise)
        test_results["completed"] = completed
        test_results["attempts"] = attempts

        return test_results


class AttemptRESTView(JSONRESTView):
    '''REST view of an exercise attempt.'''

    def __init__(self, req, subject, year, semester, worksheet, exercise, 
                 username, date):
        # TODO: Find exercise within worksheet.
        user = ivle.database.User.get_by_login(req.store, username)
        if user is None:
            raise NotFound()

        try:
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
        except ValueError:
            raise NotFound()

        # XXX Hack around Google Code issue #87
        # Query from the given date +1 secnod.
        # Date is in seconds (eg. 3:47:12), while the data is in finer time
        # (eg. 3:47:12.3625). The query "date <= 3:47:12" will fail because
        # 3:47:12.3625 is greater. Hence we do the query from +1 second,
        # "date <= 3:47:13", and it finds the correct submission, UNLESS there
        # are multiple submissions inside the same second.
        date += datetime.timedelta(seconds=1)

        worksheet_exercise = req.store.find(WorksheetExercise,
            WorksheetExercise.exercise_id == exercise,
            WorksheetExercise.worksheet_id == Worksheet.id,
            Worksheet.identifier == worksheet,
            Worksheet.offering_id == Offering.id,
            Offering.subject_id == Subject.id,
            Subject.short_name == subject,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester).one()
            
        attempt = ivle.worksheet.utils.get_exercise_attempt(req.store, user,
                        worksheet_exercise, as_of=date,
                        allow_inactive=HISTORY_ALLOW_INACTIVE) 

        if attempt is None:
            raise NotFound()

        self.context = attempt

    @require_permission('view')
    def GET(self, req):
        return {'code': self.context.text}


class WorksheetExerciseRESTView(JSONRESTView):
    '''REST view of a worksheet exercise.'''

    def __init__(self, req, subject, year, semester, worksheet, exercise):
        self.context = req.store.find(WorksheetExercise,
            WorksheetExercise.exercise_id == exercise,
            WorksheetExercise.worksheet_id == Worksheet.id,
            Worksheet.offering_id == Offering.id,
            Offering.subject_id == Subject.id,
            Subject.short_name == subject,
            Offering.semester_id == Semester.id,
            Semester.year == year,
            Semester.semester == semester).one()
        
        if self.context is None:
            raise NotFound()

    @named_operation('view')
    def save(self, req, text):
        # Find the appropriate WorksheetExercise to save to. If its not found,
        # the user is submitting against a non-existant worksheet/exercise

        old_save = req.store.find(ExerciseSave,
            ExerciseSave.ws_ex_id == self.context.id,
            ExerciseSave.user == req.user).one()
        
        #Overwrite the old, or create a new if there isn't one
        if old_save is None:
            new_save = ExerciseSave()
            req.store.add(new_save)
        else:
            new_save = old_save
        
        new_save.worksheet_exercise = self.context
        new_save.user = req.user
        new_save.text = unicode(text)
        new_save.date = datetime.datetime.now()

        return {"result": "ok"}


# Note that this is the view of an existing worksheet. Creation is handled
# by OfferingRESTView (as offerings have worksheets)
class WorksheetRESTView(JSONRESTView):
    """View used to update a worksheet."""

    def __init__(self, req, **kwargs):
    
        self.worksheet = kwargs['worksheet']
        self.subject = kwargs['subject']
        self.year = kwargs['year']
        self.semester = kwargs['semester']
    
        self.context = req.store.find(Worksheet,
            Worksheet.identifier == self.worksheet,
            Worksheet.offering_id == Offering.id,
            Offering.subject_id == Subject.id,
            Subject.short_name == self.subject,
            Offering.semester_id == Semester.id,
            Semester.year == self.year,
            Semester.semester == self.semester).one()
        
        if self.context is None:
            raise NotFound()
    
    @named_operation('edit')
    def save(self, req, name, assessable, data, format):
        """Takes worksheet data and saves it."""
        self.context.name = unicode(name)
        self.context.assessable = self.convert_bool(assessable)
        self.context.data = unicode(data)
        self.context.format = unicode(format)
        ivle.worksheet.utils.update_exerciselist(self.context)
        
        return {"result": "ok"}

class WorksheetsRESTView(JSONRESTView):
    """View used to update and create Worksheets."""
    
    def __init__(self, req, **kwargs):
    
        self.subject = kwargs['subject']
        self.year = kwargs['year']
        self.semester = kwargs['semester']
    
        self.context = req.store.find(Offering,
            Offering.subject_id == Subject.id,
            Subject.short_name == self.subject,
            Offering.semester_id == Semester.id,
            Semester.year == self.year,
            Semester.semester == self.semester).one()
        
        if self.context is None:
            raise NotFound()

    @named_operation('edit')
    def add_worksheet(self, req, identifier, name, assessable, data, format):
        """Takes worksheet data and adds it."""
        
        new_worksheet = Worksheet()
        new_worksheet.seq_no = self.context.worksheets.count()
        # Setting new_worksheet.offering implicitly adds new_worksheet,
        # hence worksheets.count MUST be called above it
        new_worksheet.offering = self.context
        new_worksheet.identifier = unicode(identifier)
        new_worksheet.name = unicode(name)
        new_worksheet.assessable = self.convert_bool(assessable)
        new_worksheet.data = unicode(data)
        new_worksheet.format = unicode(format)
        
        # This call is added for clarity, as the worksheet is implicitly added.        
        req.store.add(new_worksheet)

        ivle.worksheet.utils.update_exerciselist(new_worksheet)

        return {"result": "ok"}

    @named_operation('edit')
    def move_up(self, req, worksheetid):
        """Takes a list of worksheet-seq_no pairs and updates their 
        corresponding Worksheet objects to match."""
        
        worksheet_below = req.store.find(Worksheet,
            Worksheet.offering_id == self.context.id,
            Worksheet.identifier == unicode(worksheetid)).one()
        if worksheet_below is None:
            raise NotFound('worksheet_below')
        worksheet_above = req.store.find(Worksheet,
            Worksheet.offering_id == self.context.id,
            Worksheet.seq_no == (worksheet_below.seq_no - 1)).one()
        if worksheet_above is None:
            raise NotFound('worksheet_above')

        worksheet_below.seq_no = worksheet_below.seq_no - 1
        worksheet_above.seq_no = worksheet_above.seq_no + 1
        
        return {'result': 'ok'}

    @named_operation('edit')
    def move_down(self, req, worksheetid):
        """Takes a list of worksheet-seq_no pairs and updates their 
        corresponding Worksheet objects to match."""
        
        worksheet_above = req.store.find(Worksheet,
            Worksheet.offering_id == self.context.id,
            Worksheet.identifier == unicode(worksheetid)).one()
        if worksheet_above is None:
            raise NotFound('worksheet_below')
        worksheet_below = req.store.find(Worksheet,
            Worksheet.offering_id == self.context.id,
            Worksheet.seq_no == (worksheet_above.seq_no + 1)).one()
        if worksheet_below is None:
            raise NotFound('worksheet_above')

        worksheet_below.seq_no = worksheet_below.seq_no - 1
        worksheet_above.seq_no = worksheet_above.seq_no + 1
        
        return {'result': 'ok'}