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

« back to all changes in this revision

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

  • Committer: David Coles
  • Date: 2010-08-30 03:26:13 UTC
  • Revision ID: coles.david@gmail.com-20100830032613-d14vng0jkelniu3l
python-console: Fix globals broken with new JSON library.

simplejson always returns unicode strings. cJSON would return ordinary strings 
if possible. cPickle.loads() only accepts strings. At present we use pickle 
version 0 so they should all works as ASCII strings. Higher versions of pickle 
are not plain ASCII and are likely to break this and so this should be fixed 
at some point.

Also replaced unconditional exception with one that catches Pickle errors. Not 
sure the best way to report failures of these functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
'''AJAX backend for the tutorial application.'''
21
21
 
22
 
import os
23
22
import datetime
24
23
 
25
24
import genshi
26
25
from storm.locals import Store
27
26
 
28
 
import ivle.console
29
27
import ivle.database
30
28
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
31
29
                          Offering, Subject, Semester, User, WorksheetExercise
32
30
import ivle.worksheet.utils
33
 
import ivle.webapp.tutorial.test
34
 
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
 
31
from ivle.webapp.base.rest import (JSONRESTView, write_operation,
35
32
                                   require_permission)
36
33
from ivle.webapp.errors import NotFound
37
34
 
38
35
 
39
36
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
40
37
 
 
38
 
41
39
class AttemptsRESTView(JSONRESTView):
42
40
    '''REST view of a user's attempts at an exercise.'''
43
41
 
58
56
    @require_permission('edit')
59
57
    def PUT(self, req, data):
60
58
        """ Tests the given submission """
61
 
        # Start a console to run the tests on
62
 
        jail_path = os.path.join(req.config['paths']['jails']['mounts'],
63
 
                                 req.user.login)
64
 
        working_dir = os.path.join("/home", req.user.login)
65
 
        cons = ivle.console.Console(req.config, req.user.unixid, jail_path,
66
 
                                    working_dir)
67
 
 
68
 
        # Parse the file into a exercise object using the test suite
69
 
        exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
70
 
                            self.context.worksheet_exercise.exercise, cons)
71
 
 
72
 
        # Run the test cases. Get the result back as a JSONable object.
73
 
        # Return it.
74
 
        test_results = exercise_obj.run_tests(data['code'])
75
 
 
76
 
        # Close the console
77
 
        cons.close()
 
59
        # Trim off any trailing whitespace (can cause syntax errors in python)
 
60
        # While technically this is a user error, it causes a lot of confusion 
 
61
        # for student since it's "invisible".
 
62
        code = data['code'].rstrip()
 
63
 
 
64
        test_results = ivle.worksheet.utils.test_exercise_submission(
 
65
            req.config, req.user, self.context.worksheet_exercise.exercise,
 
66
            code)
78
67
 
79
68
        attempt = ivle.database.ExerciseAttempt(user=req.user,
80
69
            worksheet_exercise = self.context.worksheet_exercise,
81
70
            date = datetime.datetime.now(),
82
71
            complete = test_results['passed'],
83
 
            text = unicode(data['code'])
 
72
            text = unicode(code)
84
73
        )
85
74
 
86
75
        req.store.add(attempt)
107
96
class WorksheetExerciseRESTView(JSONRESTView):
108
97
    '''REST view of a worksheet exercise.'''
109
98
 
110
 
    @named_operation('view')
 
99
    @write_operation('view')
111
100
    def save(self, req, text):
112
101
        # Find the appropriate WorksheetExercise to save to. If its not found,
113
102
        # the user is submitting against a non-existant worksheet/exercise
131
120
        return {"result": "ok"}
132
121
 
133
122
 
134
 
# Note that this is the view of an existing worksheet. Creation is handled
135
 
# by OfferingRESTView (as offerings have worksheets)
136
 
class WorksheetRESTView(JSONRESTView):
137
 
    """View used to update a worksheet."""
138
 
 
139
 
    @named_operation('edit')
140
 
    def save(self, req, name, assessable, data, format):
141
 
        """Takes worksheet data and saves it."""
142
 
        self.context.name = unicode(name)
143
 
        self.context.assessable = self.convert_bool(assessable)
144
 
        self.context.data = unicode(data)
145
 
        self.context.format = unicode(format)
146
 
        ivle.worksheet.utils.update_exerciselist(self.context)
147
 
        
148
 
        return {"result": "ok"}
149
 
 
150
123
class WorksheetsRESTView(JSONRESTView):
151
124
    """View used to update and create Worksheets."""
152
125
 
153
 
    @named_operation('edit')
154
 
    def add_worksheet(self, req, identifier, name, assessable, data, format):
155
 
        """Takes worksheet data and adds it."""
156
 
        
157
 
        new_worksheet = Worksheet()
158
 
        new_worksheet.seq_no = self.context.worksheets.count()
159
 
        # Setting new_worksheet.offering implicitly adds new_worksheet,
160
 
        # hence worksheets.count MUST be called above it
161
 
        new_worksheet.offering = self.context
162
 
        new_worksheet.identifier = unicode(identifier)
163
 
        new_worksheet.name = unicode(name)
164
 
        new_worksheet.assessable = self.convert_bool(assessable)
165
 
        new_worksheet.data = unicode(data)
166
 
        new_worksheet.format = unicode(format)
167
 
        
168
 
        # This call is added for clarity, as the worksheet is implicitly added.        
169
 
        req.store.add(new_worksheet)
170
 
 
171
 
        ivle.worksheet.utils.update_exerciselist(new_worksheet)
172
 
 
173
 
        return {"result": "ok"}
174
 
 
175
 
    @named_operation('edit')
 
126
    @write_operation('edit_worksheets')
176
127
    def move_up(self, req, worksheetid):
177
128
        """Takes a list of worksheet-seq_no pairs and updates their 
178
129
        corresponding Worksheet objects to match."""
193
144
        
194
145
        return {'result': 'ok'}
195
146
 
196
 
    @named_operation('edit')
 
147
    @write_operation('edit_worksheets')
197
148
    def move_down(self, req, worksheetid):
198
149
        """Takes a list of worksheet-seq_no pairs and updates their 
199
150
        corresponding Worksheet objects to match."""