~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-20 05:03:55 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120050355-1tm3j13zazd9ik32
ivle.worksheet: Add a save_exercise function.
www/apps/tutorialservice: Import db.TIMESTAMP_FORMAT locally.
    Remove last reference to ivle.db, replacing it with save_exercise.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
 
48
48
import cjson
49
49
 
50
 
from ivle import db
51
50
from ivle import util
52
51
from ivle import console
53
52
import ivle.database
59
58
# attempts. If False, will not allow this.
60
59
HISTORY_ALLOW_INACTIVE = False
61
60
 
 
61
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
 
62
 
 
63
 
62
64
def handle(req):
63
65
    """Handler for Ajax backend TutorialService app."""
64
66
    # Set request attributes
99
101
        # The time *should* be in the same format as the DB (since it should
100
102
        # be bounced back to us from the getattempts output). Assume this.
101
103
        try:
102
 
            date = datetime.datetime.strptime(date, db.TIMESTAMP_FORMAT)
 
104
            date = datetime.datetime.strptime(date, TIMESTAMP_FORMAT)
103
105
        except ValueError:
104
106
            # Date was not in correct format
105
107
            req.throw_error(req.HTTP_BAD_REQUEST)
107
109
    else:
108
110
        req.throw_error(req.HTTP_BAD_REQUEST)
109
111
 
110
 
def handle_save(req, exercise, code, fields):
 
112
def handle_save(req, exercisename, code, fields):
111
113
    """Handles a save action. This saves the user's code without executing it.
112
114
    """
113
115
    # Need to open JUST so we know this is a real exercise.
114
116
    # (This avoids users submitting code for bogus exercises).
115
 
    exercisefile = util.open_exercise_file(exercise)
 
117
    exercisefile = util.open_exercise_file(exercisename)
116
118
    if exercisefile is None:
117
119
        req.throw_error(req.HTTP_NOT_FOUND,
118
120
            "The exercise was not found.")
119
121
    exercisefile.close()
120
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
 
121
128
    req.write('{"result": "ok"}')
122
129
 
123
 
    conn = db.DB()
124
 
 
125
 
    try:
126
 
        conn.write_problem_save(
127
 
            user = req.user,
128
 
            exercisename = exercise,
129
 
            date = time.localtime(),
130
 
            text = code)
131
 
    finally:
132
 
        conn.close()
133
130
 
134
131
def handle_test(req, exercisesrc, code, fields):
135
132
    """Handles a test action."""
182
179
    attempts = ivle.worksheet.get_exercise_attempts(req.store, req.user,
183
180
        exercise, allow_inactive=HISTORY_ALLOW_INACTIVE)
184
181
    # attempts is a list of ExerciseAttempt objects. Convert to dictionaries.
185
 
    time_fmt = lambda dt: datetime.datetime.strftime(dt, db.TIMESTAMP_FORMAT)
 
182
    time_fmt = lambda dt: datetime.datetime.strftime(dt, TIMESTAMP_FORMAT)
186
183
    attempts = [{'date': time_fmt(a.date), 'complete': a.complete}
187
184
                for a in attempts]
188
185
    req.write(cjson.encode(attempts))