~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-08-18 10:05:46 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1025
tutorialservice: Added two new GET actions: getattempts and getattempt.
    getattempts retrieves a list of all attempts the user has made for a
    particular question.
    getattempt returns the code for a specific attempt.
    Note that BY DEFAULT, these do not allow you to browse disabled attempts
    (but this can be easily activated via a flag at the top of
    tutorialservice/__init__.py).
db.py: Added get_problem_attempts and get_problem_attempt methods to do the DB
    queries for the above actions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#              relative to the subjects base directory.
33
33
# action "save" or "test" (POST only):
34
34
#   "code" - Full text of the student's code being submitted.
 
35
# action "getattempts": No arguments. Returns a list of
 
36
#   {'date': 'formatted_date', 'complete': bool} dicts.
 
37
# action "getattempt":
 
38
#   "date" - Formatted date. Gets most recent attempt before (and including)
 
39
#   that date.
 
40
#   Returns JSON string containing code, or null.
35
41
 
36
42
# Returns a JSON response string indicating the results.
37
43
 
44
50
import test
45
51
import conf
46
52
 
 
53
# If True, getattempts or getattempt will allow browsing of inactive/disabled
 
54
# attempts. If False, will not allow this.
 
55
HISTORY_ALLOW_INACTIVE = False
 
56
 
47
57
def handle(req):
48
58
    """Handler for Ajax backend TutorialService app."""
49
59
    # Set request attributes
63
73
        # Must be POST
64
74
        if req.method != 'POST':
65
75
            req.throw_error(req.HTTP_BAD_REQUEST)
 
76
 
66
77
        code = fields.getfirst('code')
67
 
 
68
78
        if code is None:
69
79
            req.throw_error(req.HTTP_BAD_REQUEST)
70
80
        code = code.value
73
83
            handle_save(req, exercise, code, fields)
74
84
        else:   # act == "test"
75
85
            handle_test(req, exercise, code, fields)
 
86
    elif act == 'getattempts':
 
87
        handle_getattempts(req, exercise)
 
88
    elif act == 'getattempt':
 
89
        date = fields.getfirst('date')
 
90
        if date is None:
 
91
            req.throw_error(req.HTTP_BAD_REQUEST)
 
92
        date = date.value
 
93
        # Convert into a struct_time
 
94
        # The time *should* be in the same format as the DB (since it should
 
95
        # be bounced back to us from the getattempts output). Assume this.
 
96
        try:
 
97
            date = time.strptime(date, db.TIMESTAMP_FORMAT)
 
98
        except ValueError:
 
99
            # Date was not in correct format
 
100
            req.throw_error(req.HTTP_BAD_REQUEST)
 
101
        handle_getattempt(req, exercise, date)
76
102
    else:
77
103
        req.throw_error(req.HTTP_BAD_REQUEST)
78
104
 
135
161
        req.write(cjson.encode(test_results))
136
162
    finally:
137
163
        conn.close()
 
164
 
 
165
def handle_getattempts(req, exercise):
 
166
    """Handles a getattempts action."""
 
167
    conn = db.DB()
 
168
    try:
 
169
        attempts = conn.get_problem_attempts(
 
170
            login=req.user.login,
 
171
            exercisename=exercise,
 
172
            allow_inactive=HISTORY_ALLOW_INACTIVE)
 
173
        req.write(cjson.encode(attempts))
 
174
    finally:
 
175
        conn.close()
 
176
 
 
177
def handle_getattempt(req, exercise, date):
 
178
    """Handles a getattempts action. Date is a struct_time."""
 
179
    conn = db.DB()
 
180
    try:
 
181
        attempt = conn.get_problem_attempt(
 
182
            login=req.user.login,
 
183
            exercisename=exercise,
 
184
            as_of=date,
 
185
            allow_inactive=HISTORY_ALLOW_INACTIVE)
 
186
        # attempt may be None; will write "null"
 
187
        req.write(cjson.encode(attempt))
 
188
    finally:
 
189
        conn.close()