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

« back to all changes in this revision

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

Updated the Worksheets to use a new tutorialservice, hosted in the
new webapps dir.

This now means the worksheets use RESTful calls to the server when
performing their Ajax

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
import ivle.database
53
53
import ivle.worksheet
54
54
import ivle.conf
55
 
import test # XXX: Really .test, not real test.
 
55
import ivle.webapp.tutorial.test # XXX: Really .test, not real test.
56
56
 
57
57
from ivle.webapp.base.rest import JSONRESTView
58
58
import ivle.database
81
81
        time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
82
82
        attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
83
83
                for a in attempts]
84
 
        attempts.append(self.exercise)
85
 
        
 
84
                
86
85
        return attempts
87
86
        
88
87
    def PUT(self, req, data):
89
 
        
 
88
        ''' Tests the given submission '''
90
89
        exercisefile = util.open_exercise_file(self.exercise)
91
90
        if exercisefile is None:
92
91
            req.throw_error(req.HTTP_NOT_FOUND,
98
97
        cons = console.Console(req.user.unixid, jail_path, working_dir)
99
98
 
100
99
        # Parse the file into a exercise object using the test suite
101
 
        exercise_obj = test.parse_exercise_file(exercisefile, cons)
 
100
        exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
 
101
                                                            exercisefile, cons)
102
102
        exercisefile.close()
103
103
 
104
104
        # Run the test cases. Get the result back as a JSONable object.
105
105
        # Return it.
106
 
        test_results = exercise_obj.run_tests(code)
 
106
        test_results = exercise_obj.run_tests(data['code'])
107
107
 
108
108
        # Close the console
109
109
        cons.close()
110
110
 
111
111
        # Get the Exercise from the database
112
 
        exercise = ivle.database.Exercise.get_by_name(req.store, exercisesrc)
 
112
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
113
113
 
114
114
        attempt = ivle.database.ExerciseAttempt(user=req.user,
115
115
                                                exercise=exercise,
116
116
                                                date=datetime.datetime.now(),
117
117
                                                complete=test_results['passed'],
118
 
                                                text=unicode(code)) # XXX
 
118
                                                # XXX
 
119
                                                text=unicode(data['code']))
119
120
 
120
121
        req.store.add(attempt)
121
122
        req.store.commit()
152
153
    Handles a save action. This saves the user's code without executing it.
153
154
    '''
154
155
    @named_operation
155
 
    def save(self, req, user, text):    
 
156
    def save(self, req, text):
156
157
        # Need to open JUST so we know this is a real exercise.
157
158
        # (This avoids users submitting code for bogus exercises).
158
159
        exercisefile = util.open_exercise_file(self.exercise)
163
164
 
164
165
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
165
166
        ivle.worksheet.save_exercise(req.store, req.user, exercise,
166
 
                                     unicode(code), datetime.datetime.now())
 
167
                                     unicode(text), datetime.datetime.now())
167
168
        req.store.commit()
168
169
        return {"result": "ok"}