1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# IVLE - Informatics Virtual Learning Environment
# Copyright (C) 2007-2009 The University of Melbourne
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# Author: Matt Giuca, Nick Chadwick
'''AJAX backend for the tutorial application.'''
import os
import datetime
import ivle.util
import ivle.console
import ivle.database
import ivle.worksheet
import ivle.conf
import ivle.webapp.tutorial.test
from ivle.webapp.base.rest import JSONRESTView, named_operation
from ivle.webapp.errors import NotFound
# If True, getattempts or getattempt will allow browsing of inactive/disabled
# attempts. If False, will not allow this.
HISTORY_ALLOW_INACTIVE = False
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
class AttemptsRESTView(JSONRESTView):
'''REST view of a user's attempts at an exercise.'''
def __init__(self, req, subject, worksheet, exercise, username):
# TODO: Find exercise within worksheet.
self.user = ivle.database.User.get_by_login(req.store, username)
if self.user is None:
raise NotFound()
self.exercise = exercise
def GET(self, req):
"""Handles a GET Attempts action."""
exercise = ivle.database.Exercise.get_by_name(req.store,
self.exercise)
attempts = ivle.worksheet.get_exercise_attempts(req.store, self.user,
exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
# attempts is a list of ExerciseAttempt objects. Convert to dictionaries
time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
for a in attempts]
return attempts
def PUT(self, req, data):
''' Tests the given submission '''
exercisefile = ivle.util.open_exercise_file(self.exercise)
if exercisefile is None:
raise NotFound()
# Start a console to run the tests on
jail_path = os.path.join(ivle.conf.jail_base, req.user.login)
working_dir = os.path.join("/home", req.user.login)
cons = ivle.console.Console(req.user.unixid, jail_path, working_dir)
# Parse the file into a exercise object using the test suite
exercise_obj = ivle.webapp.tutorial.test.parse_exercise_file(
exercisefile, cons)
exercisefile.close()
# Run the test cases. Get the result back as a JSONable object.
# Return it.
test_results = exercise_obj.run_tests(data['code'])
# Close the console
cons.close()
# Get the Exercise from the database
exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
attempt = ivle.database.ExerciseAttempt(user=req.user,
exercise=exercise,
date=datetime.datetime.now(),
complete=test_results['passed'],
# XXX
text=unicode(data['code']))
req.store.add(attempt)
# Query the DB to get an updated score on whether or not this problem
# has EVER been completed (may be different from "passed", if it has
# been completed before), and the total number of attempts.
completed, attempts = ivle.worksheet.get_exercise_status(req.store,
req.user, exercise)
test_results["completed"] = completed
test_results["attempts"] = attempts
return test_results
class AttemptRESTView(JSONRESTView):
'''REST view of an exercise attempt.'''
def __init__(self, req, subject, worksheet, exercise, username, date):
# TODO: Find exercise within worksheet.
user = ivle.database.User.get_by_login(req.store, username)
if user is None:
raise NotFound()
try:
date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
except ValueError:
raise NotFound()
exercise = ivle.database.Exercise.get_by_name(req.store, exercise)
attempt = ivle.worksheet.get_exercise_attempt(req.store, user,
exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
if attempt is None:
raise NotFound()
self.context = attempt
def GET(self, req):
return {'code': self.context.text}
class ExerciseRESTView(JSONRESTView):
'''REST view of an exercise.'''
@named_operation
def save(self, req, text):
# Need to open JUST so we know this is a real exercise.
# (This avoids users submitting code for bogus exercises).
exercisefile = ivle.util.open_exercise_file(self.exercise)
if exercisefile is None:
raise NotFound()
exercisefile.close()
exercise = ivle.database.Exercise.get_by_name(req.store, self.exercise)
ivle.worksheet.save_exercise(req.store, req.user, exercise,
unicode(text), datetime.datetime.now())
return {"result": "ok"}
|