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

« back to all changes in this revision

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

Remove the legacy console app and plugin, and the legacy plugin infrastructure
from ivle.dispatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
import ivle.webapp.tutorial.test
31
31
 
32
32
from ivle.webapp.base.rest import JSONRESTView, named_operation
33
 
from ivle.webapp.errors import NotFound
34
33
 
35
34
# If True, getattempts or getattempt will allow browsing of inactive/disabled
36
35
# attempts. If False, will not allow this.
41
40
 
42
41
class AttemptsRESTView(JSONRESTView):
43
42
    '''REST view of a user's attempts at an exercise.'''
44
 
    def __init__(self, req, subject, worksheet, exercise, username):
45
 
        # TODO: Find exercise within worksheet.
46
 
        self.user = ivle.database.User.get_by_login(req.store, username)
47
 
        if self.user is None:
48
 
            raise NotFound()
49
 
        self.exercise = exercise
50
 
 
51
43
    def GET(self, req):
52
44
        """Handles a GET Attempts action."""
53
45
        exercise = ivle.database.Exercise.get_by_name(req.store, 
54
46
                                                        self.exercise)
 
47
        user = ivle.database.User.get_by_login(req.store, self.username)
55
48
 
56
 
        attempts = ivle.worksheet.get_exercise_attempts(req.store, self.user,
 
49
        attempts = ivle.worksheet.get_exercise_attempts(req.store, user,
57
50
                            exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
58
51
        # attempts is a list of ExerciseAttempt objects. Convert to dictionaries
59
52
        time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
67
60
        ''' Tests the given submission '''
68
61
        exercisefile = ivle.util.open_exercise_file(self.exercise)
69
62
        if exercisefile is None:
70
 
            raise NotFound()
 
63
            req.throw_error(req.HTTP_NOT_FOUND,
 
64
                "The exercise was not found.")
71
65
 
72
66
        # Start a console to run the tests on
73
67
        jail_path = os.path.join(ivle.conf.jail_base, req.user.login)
112
106
class AttemptRESTView(JSONRESTView):
113
107
    '''REST view of an exercise attempt.'''
114
108
 
115
 
    def __init__(self, req, subject, worksheet, exercise, username, date):
116
 
        # TODO: Find exercise within worksheet.
117
 
        user = ivle.database.User.get_by_login(req.store, username)
118
 
        if user is None:
119
 
            raise NotFound()
120
 
 
121
 
        try:
122
 
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
123
 
        except ValueError:
124
 
            raise NotFound()
125
 
 
126
 
        exercise = ivle.database.Exercise.get_by_name(req.store, exercise)
127
 
        attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
 
109
    def GET(self, req):
 
110
        # Get an actual date object, rather than a string
 
111
        date = datetime.datetime.strptime(self.date, TIMESTAMP_FORMAT)
 
112
 
 
113
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
 
114
        attempt = ivle.worksheet.get_exercise_attempt(req.store, req.user,
128
115
            exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
129
 
 
130
 
        if attempt is None:
131
 
            raise NotFound()
132
 
 
133
 
        self.context = attempt
134
 
 
135
 
    def GET(self, req):
136
 
        return {'code': self.context.text}
 
116
        if attempt is not None:
 
117
            attempt = attempt.text
 
118
        # attempt may be None; will write "null"
 
119
        return {'code': attempt}
137
120
 
138
121
 
139
122
class ExerciseRESTView(JSONRESTView):
144
127
        # (This avoids users submitting code for bogus exercises).
145
128
        exercisefile = ivle.util.open_exercise_file(self.exercise)
146
129
        if exercisefile is None:
147
 
            raise NotFound()
 
130
            req.throw_error(req.HTTP_NOT_FOUND,
 
131
                "The exercise was not found.")
148
132
        exercisefile.close()
149
133
 
150
134
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)