1 #!/usr/bin/env python 2 3 "A WebStack questionnaire application." 4 5 import WebStack.Generic 6 import XSLForms.Resources 7 import XSLForms.Utils 8 import os 9 10 class QuestionnaireEditorResource(XSLForms.Resources.XSLFormsResource): 11 12 "A resource providing a questionnaire editor." 13 14 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 15 encoding = "utf-8" 16 template_resources = { 17 "question" : ("question_template.xhtml", "question_output.xsl") 18 } 19 20 def respond_to_form(self, trans, form): 21 22 """ 23 Respond to a request having the given transaction 'trans' and the given 24 'form' information. 25 """ 26 27 parameters = form.get_parameters() 28 documents = form.get_documents() 29 selectors = form.get_selectors() 30 31 # Ensure the presence of a document. 32 33 if documents.has_key("questionnaire"): 34 questionnaire = documents["questionnaire"] 35 else: 36 questionnaire = form.new_instance("questionnaire") 37 38 # Add and remove elements according to the selectors found. 39 40 XSLForms.Utils.remove_elements(selectors.get("remove-question")) 41 XSLForms.Utils.add_elements(selectors.get("add-choice"), "choice") 42 XSLForms.Utils.remove_elements(selectors.get("remove-choice")) 43 44 # Add questions using the normal request parameter. 45 46 if parameters.has_key("add-question"): 47 new_question = questionnaire.ownerDocument.createElementNS(None, "question") 48 questionnaire.xpath("questionnaire")[0].appendChild(new_question) 49 50 # Send a response according to certain parameters. 51 # When exported, an XML version of the data is returned. 52 53 if parameters.has_key("export"): 54 trans.set_content_type(WebStack.Generic.ContentType("text/xml", self.encoding)) 55 questionnaire.toStream(trans.get_response_stream(), trans.get_response_stream_encoding()) 56 57 # When not exported, the data is transformed to produce a normal Web 58 # page. 59 60 else: 61 62 # Start the response. 63 64 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 65 66 # Ensure that an output stylesheet exists. 67 68 trans_xsl = self.prepare_output("question") 69 70 # Complete the response. 71 72 self.send_output(trans, [trans_xsl], questionnaire) 73 74 # vim: tabstop=4 expandtab shiftwidth=4