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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2009-12-08 03:50:24 UTC
  • mfrom: (1294.2.143 ui-the-third)
  • Revision ID: grantw@unimelb.edu.au-20091208035024-wjx8zp54gth15ph8
Merge ui-the-third. This is another UI revamp.

The header is now thin! Thin! The yellow bar is gone. The tabs are gone.
Breadcrumbs are here. Routes is replaced (with an object publishing
mechanism). Views are less repetitive. etc.

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
22
23
import datetime
23
24
 
24
25
import genshi
25
26
from storm.locals import Store
26
27
 
 
28
import ivle.console
27
29
import ivle.database
28
30
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave, Worksheet, \
29
31
                          Offering, Subject, Semester, User, WorksheetExercise
30
32
import ivle.worksheet.utils
31
 
from ivle.webapp.base.rest import (JSONRESTView, write_operation,
 
33
import ivle.webapp.tutorial.test
 
34
from ivle.webapp.base.rest import (JSONRESTView, named_operation,
32
35
                                   require_permission)
33
36
from ivle.webapp.errors import NotFound
34
37
 
35
38
 
36
39
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
37
40
 
38
 
 
39
41
class AttemptsRESTView(JSONRESTView):
40
42
    '''REST view of a user's attempts at an exercise.'''
41
43
 
56
58
    @require_permission('edit')
57
59
    def PUT(self, req, data):
58
60
        """ Tests the given submission """
59
 
        test_results = ivle.worksheet.utils.test_exercise_submission(
60
 
            req.config, req.user, self.context.worksheet_exercise.exercise,
61
 
            data['code'])
 
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()
62
78
 
63
79
        attempt = ivle.database.ExerciseAttempt(user=req.user,
64
80
            worksheet_exercise = self.context.worksheet_exercise,
91
107
class WorksheetExerciseRESTView(JSONRESTView):
92
108
    '''REST view of a worksheet exercise.'''
93
109
 
94
 
    @write_operation('view')
 
110
    @named_operation('view')
95
111
    def save(self, req, text):
96
112
        # Find the appropriate WorksheetExercise to save to. If its not found,
97
113
        # the user is submitting against a non-existant worksheet/exercise
115
131
        return {"result": "ok"}
116
132
 
117
133
 
 
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
 
118
150
class WorksheetsRESTView(JSONRESTView):
119
151
    """View used to update and create Worksheets."""
120
152
 
121
 
    @write_operation('edit_worksheets')
 
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')
122
176
    def move_up(self, req, worksheetid):
123
177
        """Takes a list of worksheet-seq_no pairs and updates their 
124
178
        corresponding Worksheet objects to match."""
139
193
        
140
194
        return {'result': 'ok'}
141
195
 
142
 
    @write_operation('edit_worksheets')
 
196
    @named_operation('edit')
143
197
    def move_down(self, req, worksheetid):
144
198
        """Takes a list of worksheet-seq_no pairs and updates their 
145
199
        corresponding Worksheet objects to match."""