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