# HG changeset patch # User paulb # Date 1106315087 0 # Node ID 76ae67a06159f20060e07ce501fc6d189d10ebc8 # Parent 8ba9ea11cbe5bec2c2570857a1a3d75ab0f3dc0d [project @ 2005-01-21 13:44:42 by paulb] Added another example. diff -r 8ba9ea11cbe5 -r 76ae67a06159 examples/BaseHTTPRequestHandler/QuestionnaireApp.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/BaseHTTPRequestHandler/QuestionnaireApp.py Fri Jan 21 13:44:47 2005 +0000 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +from WebStack.Adapters import BaseHTTPRequestHandler +from Questionnaire import QuestionnaireEditorResource +import BaseHTTPServer + +# Special magic incantation. + +handler = BaseHTTPRequestHandler.HandlerFactory(QuestionnaireEditorResource()) +address = ("", 8080) +server = BaseHTTPServer.HTTPServer(address, handler) +print "Serving..." +server.serve_forever() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8ba9ea11cbe5 -r 76ae67a06159 examples/Common/Questionnaire/Resources/question_template.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/Questionnaire/Resources/question_template.xhtml Fri Jan 21 13:44:47 2005 +0000 @@ -0,0 +1,77 @@ + + + + + Questionnaire Editor + + + + + +

Questionnaire Editor

+ +

Enter questions and possible responses below.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Question +
Response Text
Multiple choice
+
+
+ +

+ to make + the questionnaire longer.

+ +

+ when all the questions + and responses are ready.

+
+ + diff -r 8ba9ea11cbe5 -r 76ae67a06159 examples/Common/Questionnaire/Resources/question_template_original.xhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/Questionnaire/Resources/question_template_original.xhtml Fri Jan 21 13:44:47 2005 +0000 @@ -0,0 +1,74 @@ + + + + + Questionnaire Editor + + + + + +

Questionnaire Editor

+ +

Enter questions and possible responses below.

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Question +
Response Text
Multiple choice
+
+
+ +

+ to make + the questionnaire longer.

+ +

+ when all the questions + and responses are ready.

+
+ + diff -r 8ba9ea11cbe5 -r 76ae67a06159 examples/Common/Questionnaire/Resources/styles.css --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/Questionnaire/Resources/styles.css Fri Jan 21 13:44:47 2005 +0000 @@ -0,0 +1,22 @@ +table.questionnaire td, table.questionnaire th { + padding: 5px; + margin: 0px; +} + +.question, .question-options { + background-color: #ffffff; + color: #000000; + vertical-align: top; +} + +.response, .response-options { + background-color: #eeeeee; + color: #000000; + vertical-align: top; +} + +.choice, .choice-options { + background-color: #dddddd; + color: #000000; + vertical-align: top; +} diff -r 8ba9ea11cbe5 -r 76ae67a06159 examples/Common/Questionnaire/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/Common/Questionnaire/__init__.py Fri Jan 21 13:44:47 2005 +0000 @@ -0,0 +1,128 @@ +#!/usr/bin/env python + +"A WebStack questionnaire application." + +import WebStack.Generic +import XSLForms.Fields +import XSLForms.Output +import XSLOutput +import libxml2dom +import os + +# NOTE: Move these functions into a common utility library. + +def add_elements(positions, element_name, element_parent_name=None): + if not positions: + return + for position in positions: + if element_parent_name: + parent_elements = position.xpath(element_parent_name) + if not parent_elements: + parent_element = position.ownerDocument.createElementNS(None, element_parent_name) + position.appendChild(parent_element) + else: + parent_element = parent_elements[0] + else: + parent_element = position + parent_element.appendChild(position.ownerDocument.createElementNS(None, element_name)) + +def remove_elements(positions): + if not positions: + return + for position in positions: + position.parentNode.removeChild(position) + +# NOTE: This is so much like ConfiguratorResource that some common parts could +# NOTE: be extracted. + +class QuestionnaireEditorResource: + + "A resource providing a questionnaire editor." + + resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") + encoding = "utf-8" + + def respond(self, trans): + + # Produce stylesheet. + + if trans.get_path_info().split("/")[-1] == "styles.css": + trans.set_content_type(WebStack.Generic.ContentType("text/css", self.encoding)) + out = trans.get_response_stream() + f = open(os.path.join(self.resource_dir, "styles.css"), "rb") + out.write(f.read()) + f.close() + raise WebStack.Generic.EndOfResponse + + global XSLForms # NOTE: Strangely required to avoid UnboundLocalError! + method = trans.get_request_method() + + # Only obtain field information according to the stated method. + + if method == "GET": + fields = XSLForms.Fields.Fields(encoding="iso-8859-1", values_are_lists=1) + parameters = trans.get_fields_from_path() + documents = fields.make_documents(parameters.items()) + elif method == "POST": + fields = XSLForms.Fields.Fields(encoding=self.encoding, values_are_lists=1) + parameters = trans.get_fields_from_body(self.encoding) + documents = fields.make_documents(parameters.items()) + else: + trans.set_response_code(405) + raise WebStack.Generic.EndOfResponse + + # Ensure the presence of a document. + + if documents.has_key("questionnaire"): + questionnaire = documents["questionnaire"] + else: + questionnaire = fields.new_instance("questionnaire") + + # Add and remove elements according to the selectors found. + + selectors = fields.get_selectors(parameters.items(), documents) + remove_elements(selectors.get("remove-question")) + add_elements(selectors.get("add-choice"), "choice") + remove_elements(selectors.get("remove-choice")) + + # Add questions using the normal request parameter. + + if parameters.has_key("add-question"): + new_question = questionnaire.ownerDocument.createElementNS(None, "question") + questionnaire.xpath("questionnaire")[0].appendChild(new_question) + + # Send a response according to certain parameters. + # When exported, an XML version of the data is returned. + + if parameters.has_key("export"): + trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) + libxml2dom.toStream(questionnaire, trans.get_response_stream(), trans.get_response_stream_encoding()) + + # When not exported, the data is transformed to produce a normal Web + # page. + + else: + + # Start the response. + + trans.set_content_type(WebStack.Generic.ContentType("text/html", self.encoding)) + + # Ensure that an output stylesheet exists. + + trans_xsl = os.path.join(self.resource_dir, "question_output.xsl") + template_xml = os.path.join(self.resource_dir, "question_template.xhtml") + + if not os.path.exists(trans_xsl) or \ + os.path.getmtime(trans_xsl) < os.path.getmtime(template_xml): + + import XSLForms.Prepare + template_xml = os.path.join(self.resource_dir, "question_template.xhtml") + XSLForms.Prepare.make_stylesheet(template_xml, trans_xsl) + + # Complete the response. + + proc = XSLOutput.Processor([trans_xsl]) + proc.send_output(trans.get_response_stream(), trans.get_response_stream_encoding(), + questionnaire) + +# vim: tabstop=4 expandtab shiftwidth=4