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

« back to all changes in this revision

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

  • Committer: Matt Giuca
  • Date: 2009-01-19 17:06:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: matt.giuca@gmail.com-20090119170624-3bowlh5rs7lf7eqz
ivle.worksheet: Added get_exercise_attempts and get_exercise_attempt.
tutorialservice: Updated to use these new functions rather than ivle.db. Also
    uses datetime and does its own dictionarification rather than relying on
    ivle.db to do it.
    NOTE: This is just for the above two functions, not the whole module
    (still reliant on ivle.db).
ivle.db: Removed get_problem_attempts and get_problem_attempt.

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 db
 
51
from ivle import util
 
52
from ivle import console
 
53
import ivle.database
50
54
import ivle.worksheet
51
55
import ivle.conf
52
56
import test # XXX: Really .test, not real test.
95
99
        # The time *should* be in the same format as the DB (since it should
96
100
        # be bounced back to us from the getattempts output). Assume this.
97
101
        try:
98
 
            date = time.strptime(date, db.TIMESTAMP_FORMAT)
 
102
            date = datetime.datetime.strptime(date, db.TIMESTAMP_FORMAT)
99
103
        except ValueError:
100
104
            # Date was not in correct format
101
105
            req.throw_error(req.HTTP_BAD_REQUEST)
175
179
 
176
180
    req.write(cjson.encode(test_results))
177
181
 
178
 
def handle_getattempts(req, exercise):
 
182
def handle_getattempts(req, exercisename):
179
183
    """Handles a getattempts action."""
180
 
    conn = db.DB()
181
 
    try:
182
 
        attempts = conn.get_problem_attempts(
183
 
            login=req.user.login,
184
 
            exercisename=exercise,
185
 
            allow_inactive=HISTORY_ALLOW_INACTIVE)
186
 
        req.write(cjson.encode(attempts))
187
 
    finally:
188
 
        conn.close()
 
184
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
 
185
    attempts = ivle.worksheet.get_exercise_attempts(req.store, req.user,
 
186
        exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
 
187
    # attempts is a list of ExerciseAttempt objects. Convert to dictionaries.
 
188
    time_fmt = lambda dt: datetime.datetime.strftime(dt, db.TIMESTAMP_FORMAT)
 
189
    attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
 
190
                for a in attempts]
 
191
    req.write(cjson.encode(attempts))
189
192
 
190
 
def handle_getattempt(req, exercise, date):
191
 
    """Handles a getattempts action. Date is a struct_time."""
 
193
def handle_getattempt(req, exercisename, date):
 
194
    """Handles a getattempts action. Date is a datetime.datetime."""
192
195
    conn = db.DB()
193
 
    try:
194
 
        attempt = conn.get_problem_attempt(
195
 
            login=req.user.login,
196
 
            exercisename=exercise,
197
 
            as_of=date,
198
 
            allow_inactive=HISTORY_ALLOW_INACTIVE)
199
 
        # attempt may be None; will write "null"
200
 
        req.write(cjson.encode({'code': attempt}))
201
 
    finally:
202
 
        conn.close()
 
196
    exercise = ivle.database.Exercise.get_by_name(req.store, exercisename)
 
197
    attempt = ivle.worksheet.get_exercise_attempt(req.store, req.user,
 
198
        exercise, as_of=date, allow_inactive=HISTORY_ALLOW_INACTIVE)
 
199
    if attempt is not None:
 
200
        attempt = attempt.text
 
201
    # attempt may be None; will write "null"
 
202
    req.write(cjson.encode({'code': attempt}))