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

« back to all changes in this revision

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

Fix views in ivle.webapp.admin and ivle.webapp.tutorial to return 404 wherever
possible, rather than crashing.

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
33
34
 
34
35
# If True, getattempts or getattempt will allow browsing of inactive/disabled
35
36
# attempts. If False, will not allow this.
40
41
 
41
42
class AttemptsRESTView(JSONRESTView):
42
43
    '''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
 
43
51
    def GET(self, req):
44
52
        """Handles a GET Attempts action."""
45
53
        exercise = ivle.database.Exercise.get_by_name(req.store, 
46
54
                                                        self.exercise)
47
 
        user = ivle.database.User.get_by_login(req.store, self.username)
48
55
 
49
 
        attempts = ivle.worksheet.get_exercise_attempts(req.store, user,
 
56
        attempts = ivle.worksheet.get_exercise_attempts(req.store, self.user,
50
57
                            exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
51
58
        # attempts is a list of ExerciseAttempt objects. Convert to dictionaries
52
59
        time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
106
113
class AttemptRESTView(JSONRESTView):
107
114
    '''REST view of an exercise attempt.'''
108
115
 
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,
 
116
    def __init__(self, req, subject, worksheet, exercise, username, date):
 
117
        # TODO: Find exercise within worksheet.
 
118
        user = ivle.database.User.get_by_login(req.store, username)
 
119
        if user is None:
 
120
            raise NotFound()
 
121
 
 
122
        try:
 
123
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
 
124
        except ValueError:
 
125
            raise NotFound()
 
126
 
 
127
        exercise = ivle.database.Exercise.get_by_name(req.store, exercise)
 
128
        attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
115
129
            exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
116
 
        if attempt is not None:
117
 
            attempt = attempt.text
118
 
        # attempt may be None; will write "null"
119
 
        return {'code': attempt}
 
130
 
 
131
        if attempt is None:
 
132
            raise NotFound()
 
133
 
 
134
        self.context = attempt
 
135
 
 
136
    def GET(self, req):
 
137
        return {'code': self.context.text}
120
138
 
121
139
 
122
140
class ExerciseRESTView(JSONRESTView):
127
145
        # (This avoids users submitting code for bogus exercises).
128
146
        exercisefile = ivle.util.open_exercise_file(self.exercise)
129
147
        if exercisefile is None:
130
 
            req.throw_error(req.HTTP_NOT_FOUND,
131
 
                "The exercise was not found.")
 
148
            raise NotFound()
132
149
        exercisefile.close()
133
150
 
134
151
        exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)