~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-01-25 01:05:22 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:300
conf/apps: Added TutorialService as a registered app.
tutorialservice: Started writing app. Currently reads some arguments, and if
    action=test and code is provided, echoes the code back in JSON.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# Date:   25/1/2008
21
21
 
22
22
# Provides the AJAX backend for the tutorial application.
23
 
# Not yet implemented.
 
23
# This allows several actions to be performed on the code the student has
 
24
# typed into one of the problem boxes.
 
25
 
 
26
# Calling syntax
 
27
# The "path" to this app is the path to a problem file (including the .xml
 
28
# extension), relative to the subjects base directory.
 
29
# The arguments determine what is to be done on this file.
 
30
 
 
31
# "code" - Full text of the student's code being submitted.
 
32
# "action". May be "test". (More to come).
 
33
 
 
34
# Returns a JSON response string indicating the results.
 
35
 
 
36
import cjson
 
37
 
 
38
def handle(req):
 
39
    """Handler for Ajax backend TutorialService app."""
 
40
    # Set request attributes
 
41
    req.write_html_head_foot = False     # No HTML
 
42
 
 
43
    # Get all the arguments, if POST.
 
44
    # Ignore arguments if not POST, since we aren't allowed to cause
 
45
    # side-effects on the server.
 
46
    fields = req.get_fieldstorage()
 
47
    act = fields.getfirst('action')
 
48
    code = fields.getfirst('code')
 
49
 
 
50
    if code == None or act == None:
 
51
        req.throw_error(req.HTTP_BAD_REQUEST)
 
52
    act = act.value
 
53
    code = code.value
 
54
 
 
55
    if act == "test":
 
56
        handle_test(req, code, fields)
 
57
    else:
 
58
        req.throw_error(req.HTTP_BAD_REQUEST)
 
59
 
 
60
def handle_test(req, code, fields):
 
61
    """Handles a test action."""
 
62
    # TEMP: Just echo the code back in JSON form
 
63
    req.write(cjson.encode({"code": code}))