paulb@122 | 1 | #!/usr/bin/env python |
paulb@122 | 2 | |
paulb@122 | 3 | "A very simple example application." |
paulb@122 | 4 | |
paulb@122 | 5 | import WebStack.Generic |
paulb@122 | 6 | import XSLForms.Resources |
paulb@122 | 7 | import XSLForms.Utils |
paulb@122 | 8 | import os |
paulb@122 | 9 | |
paulb@122 | 10 | # Resource classes. |
paulb@122 | 11 | |
paulb@122 | 12 | class VerySimpleResource(XSLForms.Resources.XSLFormsResource): |
paulb@122 | 13 | |
paulb@122 | 14 | "A very simple resource providing a hierarchy of editable fields." |
paulb@122 | 15 | |
paulb@122 | 16 | resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") |
paulb@122 | 17 | encoding = "utf-8" |
paulb@122 | 18 | template_resources = { |
paulb@150 | 19 | #"structure" : ("structure_template.xhtml", "structure_output.xsl") |
paulb@150 | 20 | "structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") |
paulb@122 | 21 | } |
paulb@147 | 22 | transform_resources = { |
paulb@150 | 23 | #"types" : ["structure_types.xsl"] |
paulb@150 | 24 | "types" : ["structure_multivalue_types.xsl"] |
paulb@147 | 25 | } |
paulb@147 | 26 | document_resources = { |
paulb@147 | 27 | "types" : "structure_types.xml" |
paulb@147 | 28 | } |
paulb@122 | 29 | |
paulb@122 | 30 | def respond_to_form(self, trans, form): |
paulb@122 | 31 | |
paulb@122 | 32 | """ |
paulb@122 | 33 | Respond to a request having the given transaction 'trans' and the given |
paulb@122 | 34 | 'form' information. |
paulb@122 | 35 | """ |
paulb@122 | 36 | |
paulb@122 | 37 | documents = form.get_documents() |
paulb@122 | 38 | |
paulb@122 | 39 | # Ensure the presence of a document. |
paulb@122 | 40 | |
paulb@122 | 41 | if documents.has_key("structure"): |
paulb@122 | 42 | structure = documents["structure"] |
paulb@122 | 43 | else: |
paulb@122 | 44 | structure = form.new_instance("structure") |
paulb@122 | 45 | |
paulb@122 | 46 | # Add and remove elements according to the selectors found. |
paulb@122 | 47 | |
paulb@122 | 48 | selectors = form.get_selectors() |
paulb@122 | 49 | XSLForms.Utils.remove_elements(selectors.get("remove2")) |
paulb@122 | 50 | XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") |
paulb@122 | 51 | XSLForms.Utils.remove_elements(selectors.get("remove")) |
paulb@122 | 52 | XSLForms.Utils.add_elements(selectors.get("add"), "item") |
paulb@122 | 53 | |
paulb@136 | 54 | # Transform, adding enumerations/ranges. |
paulb@136 | 55 | |
paulb@147 | 56 | types_xsl_list = self.prepare_transform("types") |
paulb@147 | 57 | types_xml = self.prepare_document("types") |
paulb@147 | 58 | structure = self.get_result(types_xsl_list, structure, references={"types" : types_xml}) |
paulb@136 | 59 | |
paulb@122 | 60 | # Start the response. |
paulb@122 | 61 | |
paulb@122 | 62 | trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) |
paulb@122 | 63 | |
paulb@122 | 64 | # Ensure that an output stylesheet exists. |
paulb@122 | 65 | |
paulb@122 | 66 | trans_xsl = self.prepare_output("structure") |
paulb@122 | 67 | |
paulb@122 | 68 | # Complete the response. |
paulb@122 | 69 | |
paulb@122 | 70 | self.send_output(trans, [trans_xsl], structure) |
paulb@122 | 71 | |
paulb@122 | 72 | # Site map initialisation. |
paulb@122 | 73 | |
paulb@122 | 74 | def get_site(): |
paulb@122 | 75 | |
paulb@122 | 76 | "Return a simple Web site resource." |
paulb@122 | 77 | |
paulb@122 | 78 | return VerySimpleResource() |
paulb@122 | 79 | |
paulb@122 | 80 | # vim: tabstop=4 expandtab shiftwidth=4 |