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

« back to all changes in this revision

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

  • Committer: mattgiuca
  • Date: 2008-02-05 22:56:59 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:425
tutorial: Refactored present_worksheet so it has a separate function for
presenting an XML element (so it can be recursive). (But does not solve the
recursion problem yet).

Show diffs side-by-side

added added

removed removed

Lines of Context:
192
192
        # TODO: Nicer error message, to help authors
193
193
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
194
194
    worksheetname = worksheetdom.getAttribute("name")
195
 
    elements = []     # List of DOM elements
196
 
    for elem in worksheetdom.childNodes:
197
 
        if elem.nodeType == elem.ELEMENT_NODE:
198
 
            elements.append(elem)
199
195
 
200
196
    # Now all the errors are out the way, we can begin writing
201
197
    req.title = "Tutorial - %s" % worksheetname
203
199
    req.write('<div id="ivle_padding">\n')
204
200
    req.write("<h1>IVLE Tutorials - %s</h1>\n<h2>%s</h2>\n"
205
201
        % (cgi.escape(subject), cgi.escape(worksheetname)))
 
202
 
206
203
    # Write each element
207
204
    problemid = 0
208
 
    for elem in elements:
209
 
        if elem.tagName == "problem":
210
 
            present_problem(req, subject, elem.getAttribute("src"), problemid)
211
 
            problemid += 1
212
 
        else:
213
 
            # Just treat this as a normal HTML element
214
 
            req.write(elem.toxml() + '\n')
 
205
    for elem in worksheetdom.childNodes:
 
206
        if elem.nodeType == elem.ELEMENT_NODE:
 
207
            problemid = present_worksheet_element(req, elem, problemid)
215
208
    req.write("</div>\n")   # tutorialbody
216
209
 
 
210
def present_worksheet_element(req, elem, problemid):
 
211
    """Given an element of a worksheet XML document, writes it out to the
 
212
    request. This recursively searches for "problem" elements and handles
 
213
    those specially (presenting their XML problem spec and input box), and
 
214
    just dumps the other elements as regular HTML.
 
215
 
 
216
    problemid is the ID to use for the first problem.
 
217
    Returns the new problemid after all the problems have been written
 
218
    (since we need unique IDs for each problem).
 
219
    """
 
220
    if elem.tagName == "problem":
 
221
        present_problem(req, elem.getAttribute("src"), problemid)
 
222
        problemid += 1
 
223
    else:
 
224
        # Just treat this as a normal HTML element
 
225
        req.write(elem.toxml() + '\n')
 
226
    return problemid
 
227
 
217
228
def innerXML(elem):
218
229
    """Given an element, returns its children as XML strings concatenated
219
230
    together."""
235
246
 
236
247
    return data.strip()
237
248
 
238
 
def present_problem(req, subject, problemsrc, problemid):
 
249
def present_problem(req, problemsrc, problemid):
239
250
    """Open a problem file, and write out the problem to the request in HTML.
240
 
    subject: Subject name.
241
251
    problemsrc: "src" of the problem file. A path relative to the top-level
242
 
        subjects base directory, as configured in conf.
 
252
        problems base directory, as configured in conf.
243
253
    """
244
254
    req.write('<div class="tuteproblem" id="problem%d">\n'
245
255
        % problemid)