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

« back to all changes in this revision

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

Updated the exercises to be loaded from the database, not a local file.

This updated fixes some things broken in my previous commit, and now
it should leave the worksheets in a viewable (not working) state.

I have also updated ivle-addexercise and the database schema to reflect
the schema of an exercise.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import ivle.util
26
26
import ivle.console
27
27
import ivle.database
 
28
from ivle.database import Exercise, ExerciseAttempt, ExerciseSave
28
29
import ivle.worksheet
29
30
import ivle.conf
30
31
import ivle.webapp.tutorial.test
42
43
 
43
44
class AttemptsRESTView(JSONRESTView):
44
45
    '''REST view of a user's attempts at an exercise.'''
45
 
    def __init__(self, req, subject, worksheet, exercise, username):
46
 
        # TODO: Find exercise within worksheet.
 
46
    def __init__(self, req, subject, year, semester, worksheet, 
 
47
                                                exercise, username):
47
48
        self.user = ivle.database.User.get_by_login(req.store, username)
48
49
        if self.user is None:
49
50
            raise NotFound()
50
51
        self.exercise = exercise
 
52
        
51
53
        self.context = self.user # XXX: Not quite right.
52
54
 
53
55
    @require_permission('edit')
54
56
    def GET(self, req):
55
57
        """Handles a GET Attempts action."""
56
 
        exercise = ivle.database.Exercise.get_by_name(req.store, 
57
 
                                                        self.exercise)
 
58
        exercise = req.store.find(Exercise, Exercise.id == self.exercise).one()
58
59
 
59
 
        attempts = ivle.worksheet.get_exercise_attempts(req.store, self.user,
60
 
                            exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
 
60
        attempts = req.store.find(ExerciseAttempt, 
 
61
                ExerciseAttempt.exercise_id == exercise.id,
 
62
                ExerciseAttempt.user_id == self.user.id)
61
63
        # attempts is a list of ExerciseAttempt objects. Convert to dictionaries
62
64
        time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
63
65
        attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
69
71
    @require_permission('edit')
70
72
    def PUT(self, req, data):
71
73
        """ Tests the given submission """
72
 
        exercisefile = ivle.util.open_exercise_file(self.exercise)
73
 
        if exercisefile is None:
 
74
        exercise = req.store.find(Exercise, Exercise.id == self.exercise).one()
 
75
        if exercise is None:
74
76
            raise NotFound()
75
77
 
76
78
        # Start a console to run the tests on
80
82
 
81
83
        # Parse the file into a exercise object using the test suite
82
84
        exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
83
 
                                                            exercisefile, cons)
84
 
        exercisefile.close()
 
85
                                                            exercise, cons)
85
86
 
86
87
        # Run the test cases. Get the result back as a JSONable object.
87
88
        # Return it.
90
91
        # Close the console
91
92
        cons.close()
92
93
 
93
 
        # Get the Exercise from the database
94
 
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
95
 
 
96
94
        attempt = ivle.database.ExerciseAttempt(user=req.user,
97
95
                                                exercise=exercise,
98
96
                                                date=datetime.datetime.now(),
155
153
    def save(self, req, text):
156
154
        # Need to open JUST so we know this is a real exercise.
157
155
        # (This avoids users submitting code for bogus exercises).
158
 
        exercisefile = ivle.util.open_exercise_file(self.exercise)
159
 
        if exercisefile is None:
160
 
            raise NotFound()
161
 
        exercisefile.close()
162
 
 
163
 
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
 
156
        exercise = req.store.find(Exercise,
 
157
            Exercise.id == self.exercise).one()
164
158
        ivle.worksheet.save_exercise(req.store, req.user, exercise,
165
159
                                     unicode(text), datetime.datetime.now())
166
160
        return {"result": "ok"}