~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-01-25 00:49:07 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:297
tutorial: Now presents problems correctly, by parsing XML source and writing
out the name, description, and a text box with a partial solution.
tutorial.css: Extra styling for text box.

Show diffs side-by-side

added added

removed removed

Lines of Context:
193
193
            # Just treat this as a normal HTML element
194
194
            req.write(elem.toxml() + '\n')
195
195
 
 
196
def innerXML(elem):
 
197
    """Given an element, returns its children as XML strings concatenated
 
198
    together."""
 
199
    s = ""
 
200
    for child in elem.childNodes:
 
201
        s += child.toxml()
 
202
    return s
 
203
 
 
204
def getTextData(element):
 
205
    """ Get the text and cdata inside an element
 
206
    Leading and trailing whitespace are stripped
 
207
    """
 
208
    data = ''
 
209
    for child in element.childNodes:
 
210
        if child.nodeType == child.CDATA_SECTION_NODE:
 
211
            data += child.data
 
212
        if child.nodeType == child.TEXT_NODE:
 
213
            data += child.data
 
214
 
 
215
    return data.strip()
 
216
 
196
217
def present_problem(req, subject, problemsrc):
197
218
    """Open a problem file, and write out the problem to the request in HTML.
198
219
    subject: Subject name.
217
238
        req.write("</div>\n")
218
239
        return
219
240
    
220
 
    # TODO: Read problem file and present the problem
221
 
    # TEMP: Print out the problem XML source
222
 
    req.write("<p><b>Problem:</b></p>\n")
223
 
    req.write("<pre>%s</pre>\n" % cgi.escape(problemfile.read()))
 
241
    # Read problem file and present the problem
 
242
    # Note: We do not use the testing framework because it does a lot more
 
243
    # work than we need. We just need to get the problem name and a few other
 
244
    # fields from the XML.
 
245
 
 
246
    problemdom = minidom.parse(problemfile)
 
247
    problemfile.close()
 
248
    problemdom = problemdom.documentElement
 
249
    if problemdom.tagName != "problem":
 
250
        # TODO: Nicer error message, to help authors
 
251
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR)
 
252
    problemname = problemdom.getAttribute("name")
 
253
    # Look for some other fields we need, which are elements:
 
254
    # - desc
 
255
    # - partial
 
256
    problemdesc = None
 
257
    problempartial= ""
 
258
    for elem in problemdom.childNodes:
 
259
        if elem.nodeType == elem.ELEMENT_NODE:
 
260
            if elem.tagName == "desc":
 
261
                problemdesc = innerXML(elem).strip()
 
262
            if elem.tagName == "partial":
 
263
                problempartial= getTextData(elem) + '\n'
 
264
 
 
265
    # Print this problem out to HTML 
 
266
    req.write("<p><b>Problem:</b> %s</p>\n" % problemname)
 
267
    if problemdesc is not None:
 
268
        req.write("<p>%s</p>" % problemdesc)
 
269
    req.write('<textarea class="problembox">%s</textarea>' \
 
270
            % problempartial)
224
271
    req.write("</div>\n")