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

« back to all changes in this revision

Viewing changes to www/apps/tutorialservice/__init__.py

  • Committer: William Grant
  • Date: 2009-01-22 04:47:42 UTC
  • mfrom: (1080.1.93 storm)
  • Revision ID: grantw@unimelb.edu.au-20090122044742-sa8gnww0ma2bm2rv
Merge Storm branch. ivle.db is dead. Watch out for the schema change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
import os
45
45
import time
 
46
import datetime
46
47
 
47
48
import cjson
48
49
 
49
 
from ivle import (db, util, console)
 
50
from ivle import util
 
51
from ivle import console
 
52
import ivle.database
 
53
import ivle.worksheet
50
54
import ivle.conf
51
55
import test # XXX: Really .test, not real test.
52
56
 
54
58
# attempts. If False, will not allow this.
55
59
HISTORY_ALLOW_INACTIVE = False
56
60
 
 
61
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
 
62
 
 
63
 
57
64
def handle(req):
58
65
    """Handler for Ajax backend TutorialService app."""
59
66
    # Set request attributes
94
101
        # The time *should* be in the same format as the DB (since it should
95
102
        # be bounced back to us from the getattempts output). Assume this.
96
103
        try:
97
 
            date = time.strptime(date, db.TIMESTAMP_FORMAT)
 
104
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
98
105
        except ValueError:
99
106
            # Date was not in correct format
100
107
            req.throw_error(req.HTTP_BAD_REQUEST)
102
109
    else:
103
110
        req.throw_error(req.HTTP_BAD_REQUEST)
104
111
 
105
 
def handle_save(req, exercise, code, fields):
 
112
def handle_save(req, exercisename, code, fields):
106
113
    """Handles a save action. This saves the user's code without executing it.
107
114
    """
108
115
    # Need to open JUST so we know this is a real exercise.
109
116
    # (This avoids users submitting code for bogus exercises).
110
 
    exercisefile = util.open_exercise_file(exercise)
 
117
    exercisefile = util.open_exercise_file(exercisename)
111
118
    if exercisefile is None:
112
119
        req.throw_error(req.HTTP_NOT_FOUND,
113
120
            "The exercise was not found.")
114
121
    exercisefile.close()
115
122
 
 
123
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
 
124
    ivle.worksheet.save_exercise(req.store, req.user, exercise,
 
125
                                 unicode(code), datetime.datetime.now())
 
126
    req.store.commit()
 
127
 
116
128
    req.write('{"result": "ok"}')
117
129
 
118
 
    conn = db.DB()
119
 
 
120
 
    try:
121
 
        conn.write_problem_save(
122
 
            login = req.user.login,
123
 
            exercisename = exercise,
124
 
            date = time.localtime(),
125
 
            text = code)
126
 
    finally:
127
 
        conn.close()
128
 
 
129
 
def handle_test(req, exercise, code, fields):
 
130
 
 
131
def handle_test(req, exercisesrc, code, fields):
130
132
    """Handles a test action."""
131
133
 
132
 
    exercisefile = util.open_exercise_file(exercise)
 
134
    exercisefile = util.open_exercise_file(exercisesrc)
133
135
    if exercisefile is None:
134
136
        req.throw_error(req.HTTP_NOT_FOUND,
135
137
            "The exercise was not found.")
150
152
    # Close the console
151
153
    cons.close()
152
154
 
153
 
    conn = db.DB()
154
 
    try:
155
 
        conn.insert_problem_attempt(
156
 
            login = req.user.login,
157
 
            exercisename = exercise,
158
 
            date = time.localtime(),
159
 
            complete = test_results['passed'],
160
 
            attempt = code)
161
 
 
162
 
        # Query the DB to get an updated score on whether or not this problem
163
 
        # has EVER been completed (may be different from "passed", if it has
164
 
        # been completed before), and the total number of attempts.
165
 
        completed, attempts = conn.get_problem_status(req.user.login,
166
 
            exercise)
167
 
        test_results["completed"] = completed
168
 
        test_results["attempts"] = attempts
169
 
 
170
 
        req.write(cjson.encode(test_results))
171
 
    finally:
172
 
        conn.close()
173
 
 
174
 
def handle_getattempts(req, exercise):
 
155
    # Get the Exercise from the database
 
156
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisesrc)
 
157
 
 
158
    attempt = ivle.database.ExerciseAttempt(user=req.user,
 
159
                                            exercise=exercise,
 
160
                                            date=datetime.datetime.now(),
 
161
                                            complete=test_results['passed'],
 
162
                                            text=unicode(code)) # XXX
 
163
 
 
164
    req.store.add(attempt)
 
165
    req.store.commit()
 
166
    # Query the DB to get an updated score on whether or not this problem
 
167
    # has EVER been completed (may be different from "passed", if it has
 
168
    # been completed before), and the total number of attempts.
 
169
    completed, attempts = ivle.worksheet.get_exercise_status(req.store,
 
170
        req.user, exercise)
 
171
    test_results["completed"] = completed
 
172
    test_results["attempts"] = attempts
 
173
 
 
174
    req.write(cjson.encode(test_results))
 
175
 
 
176
def handle_getattempts(req, exercisename):
175
177
    """Handles a getattempts action."""
176
 
    conn = db.DB()
177
 
    try:
178
 
        attempts = conn.get_problem_attempts(
179
 
            login=req.user.login,
180
 
            exercisename=exercise,
181
 
            allow_inactive=HISTORY_ALLOW_INACTIVE)
182
 
        req.write(cjson.encode(attempts))
183
 
    finally:
184
 
        conn.close()
 
178
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
 
179
    attempts = ivle.worksheet.get_exercise_attempts(req.store, req.user,
 
180
        exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
 
181
    # attempts is a list of ExerciseAttempt objects. Convert to dictionaries.
 
182
    time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
 
183
    attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
 
184
                for a in attempts]
 
185
    req.write(cjson.encode(attempts))
185
186
 
186
 
def handle_getattempt(req, exercise, date):
187
 
    """Handles a getattempts action. Date is a struct_time."""
188
 
    conn = db.DB()
189
 
    try:
190
 
        attempt = conn.get_problem_attempt(
191
 
            login=req.user.login,
192
 
            exercisename=exercise,
193
 
            as_of=date,
194
 
            allow_inactive=HISTORY_ALLOW_INACTIVE)
195
 
        # attempt may be None; will write "null"
196
 
        req.write(cjson.encode({'code': attempt}))
197
 
    finally:
198
 
        conn.close()
 
187
def handle_getattempt(req, exercisename, date):
 
188
    """Handles a getattempts action. Date is a datetime.datetime."""
 
189
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
 
190
    attempt = ivle.worksheet.get_exercise_attempt(req.store, req.user,
 
191
        exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
 
192
    if attempt is not None:
 
193
        attempt = attempt.text
 
194
    # attempt may be None; will write "null"
 
195
    req.write(cjson.encode({'code': attempt}))