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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-03-15 07:49:26 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:701
Moved "open_exercise_file" from tutorialservice to util.
    Will be used by tutorial app as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
import cjson
41
41
 
42
 
from common import db
 
42
from common import (db, util)
43
43
import test
44
44
import conf
45
45
 
73
73
    else:
74
74
        req.throw_error(req.HTTP_BAD_REQUEST)
75
75
 
76
 
def open_exercise_file(exercisename):
77
 
    """Given an exercise name, opens the corresponding XML file for reading.
78
 
    Returns None if the exercise file was not found.
79
 
    """
80
 
    # First normalise the path
81
 
    exercisename = os.path.normpath(exercisename)
82
 
    # Now if it begins with ".." or separator, then it's illegal
83
 
    if exercisename.startswith("..") or exercisename.startswith(os.sep):
84
 
        exercisefile = None
85
 
    else:
86
 
        exercisefile = os.path.join(conf.exercises_base, exercisename)
87
 
 
88
 
    try:
89
 
        return open(exercisefile)
90
 
    except (TypeError, IOError):    # TypeError if exercisefile == None
91
 
        return None
92
 
 
93
76
def handle_save(req, exercise, code, fields):
94
77
    """Handles a save action. This saves the user's code without executing it.
95
78
    """
96
79
    # Need to open JUST so we know this is a real exercise.
97
80
    # (This avoids users submitting code for bogus exercises).
98
 
    exercisefile = open_exercise_file(exercise)
 
81
    exercisefile = util.open_exercise_file(exercise)
99
82
    if exercisefile is None:
100
83
        req.throw_error(req.HTTP_NOT_FOUND,
101
84
            "The exercise was not found.")
114
97
def handle_test(req, exercise, code, fields):
115
98
    """Handles a test action."""
116
99
 
117
 
    exercisefile = open_exercise_file(exercise)
 
100
    exercisefile = util.open_exercise_file(exercise)
118
101
    if exercisefile is None:
119
102
        req.throw_error(req.HTTP_NOT_FOUND,
120
103
            "The exercise was not found.")