~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 05:55:20 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:698
Added Save feature to tutorial system.
tutorialservice: Added "save" action, which works the same as submit, but
    saves to the DB instead of making an attempt, and doesn't run the code.
    Factored out code to open an exercise file.
tutorial: Added Save button to exercise boxen.
tutorial.js: Added saveexercise function, which makes an Ajax request to the
    "save" action of tutorialservice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
    exercise = exercise.value
65
65
    code = code.value
66
66
 
67
 
    if act == "test":
 
67
    if act == "save":
 
68
        handle_save(req, exercise, code, fields)
 
69
    elif act == "test":
68
70
        handle_test(req, exercise, code, fields)
69
71
    elif act == "run":
70
72
        handle_run(req, exercise, code, fields)
71
73
    else:
72
74
        req.throw_error(req.HTTP_BAD_REQUEST)
73
75
 
74
 
def handle_test(req, exercise, code, fields):
75
 
    """Handles a test action."""
76
 
 
 
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
    """
77
80
    # First normalise the path
78
 
    exercise = os.path.normpath(exercise)
 
81
    exercisename = os.path.normpath(exercisename)
79
82
    # Now if it begins with ".." or separator, then it's illegal
80
 
    if exercise.startswith("..") or exercise.startswith(os.sep):
 
83
    if exercisename.startswith("..") or exercisename.startswith(os.sep):
81
84
        exercisefile = None
82
85
    else:
83
 
        exercisefile = os.path.join(conf.exercises_base, exercise)
 
86
        exercisefile = os.path.join(conf.exercises_base, exercisename)
84
87
 
85
88
    try:
86
 
        exercisefile = open(exercisefile)
 
89
        return open(exercisefile)
87
90
    except (TypeError, IOError):    # TypeError if exercisefile == None
88
 
        req.throw_error(req.HTTP_NOT_FOUND)
 
91
        return None
 
92
 
 
93
def handle_save(req, exercise, code, fields):
 
94
    """Handles a save action. This saves the user's code without executing it.
 
95
    """
 
96
    # Need to open JUST so we know this is a real exercise.
 
97
    # (This avoids users submitting code for bogus exercises).
 
98
    exercisefile = open_exercise_file(exercise)
 
99
    if exercisefile is None:
 
100
        req.throw_error(req.HTTP_NOT_FOUND,
 
101
            "The exercise was not found.")
 
102
    exercisefile.close()
 
103
 
 
104
    req.write('{"result": "ok"}')
 
105
 
 
106
    conn = db.DB()
 
107
 
 
108
    conn.write_problem_save(
 
109
        login = req.user.login,
 
110
        exercisename = exercise,
 
111
        date = time.localtime(),
 
112
        text = code)
 
113
 
 
114
def handle_test(req, exercise, code, fields):
 
115
    """Handles a test action."""
 
116
 
 
117
    exercisefile = open_exercise_file(exercise)
 
118
    if exercisefile is None:
 
119
        req.throw_error(req.HTTP_NOT_FOUND,
 
120
            "The exercise was not found.")
89
121
 
90
122
    # Parse the file into a exercise object using the test suite
91
123
    exercise_obj = test.parse_exercise_file(exercisefile)