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@668 | 6 | from XSLForms.Resources.WebResources import \ |
paulb@668 | 7 | XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources |
paulb@122 | 8 | import os |
paulb@122 | 9 | |
paulb@174 | 10 | # Site map imports. |
paulb@174 | 11 | |
paulb@174 | 12 | from WebStack.Resources.ResourceMap import MapResource |
paulb@560 | 13 | from WebStack.Resources.Selectors import EncodingSelector |
paulb@174 | 14 | from WebStack.Resources.Static import DirectoryResource |
paulb@174 | 15 | |
paulb@560 | 16 | # Configuration settings. |
paulb@560 | 17 | |
paulb@560 | 18 | encoding = "utf-8" |
paulb@560 | 19 | |
paulb@122 | 20 | # Resource classes. |
paulb@122 | 21 | |
paulb@668 | 22 | class VerySimpleResource(XSLFormsResource): |
paulb@122 | 23 | |
paulb@122 | 24 | "A very simple resource providing a hierarchy of editable fields." |
paulb@122 | 25 | |
paulb@668 | 26 | resource_dir = resources(__file__) |
paulb@122 | 27 | template_resources = { |
paulb@668 | 28 | #"structure" : output("structure_template.xhtml") |
paulb@668 | 29 | #"structure" : output("structure_multivalue_template.xhtml") |
paulb@668 | 30 | "structure" : output("structure_multivalue_label_template.xhtml") |
paulb@122 | 31 | } |
paulb@260 | 32 | init_resources = { |
paulb@668 | 33 | #"structure" : input("structure_template.xhtml") |
paulb@668 | 34 | #"structure" : input("structure_multivalue_template.xhtml") |
paulb@668 | 35 | "structure" : input("structure_multivalue_label_template.xhtml") |
paulb@260 | 36 | } |
paulb@147 | 37 | transform_resources = { |
paulb@260 | 38 | "comments" : ["structure_comments.xsl"] |
paulb@147 | 39 | } |
paulb@147 | 40 | document_resources = { |
paulb@445 | 41 | #"types" : "structure_types.xml" |
paulb@445 | 42 | "types" : "structure_types_label.xml", |
paulb@445 | 43 | "translations" : "translations.xml" |
paulb@147 | 44 | } |
paulb@174 | 45 | in_page_resources = { |
paulb@514 | 46 | "comments" : ("structure", "structure_output_comments.xsl", "comment-node") |
paulb@174 | 47 | } |
paulb@122 | 48 | |
paulb@668 | 49 | def select_activity(self, trans, form): |
paulb@668 | 50 | form.set_activity("structure") |
paulb@122 | 51 | |
paulb@668 | 52 | def respond_to_input(self, trans, form): |
paulb@122 | 53 | |
paulb@122 | 54 | # Add and remove elements according to the selectors found. |
paulb@122 | 55 | |
paulb@122 | 56 | selectors = form.get_selectors() |
paulb@668 | 57 | self.remove_elements(selectors.get("remove2")) |
paulb@668 | 58 | self.add_elements(selectors.get("add2"), "subitem") |
paulb@668 | 59 | self.remove_elements(selectors.get("remove")) |
paulb@668 | 60 | self.add_elements(selectors.get("add"), "item") |
paulb@668 | 61 | |
paulb@668 | 62 | def init_document(self, trans, form): |
paulb@122 | 63 | |
paulb@668 | 64 | # Transform, adding enumerations/ranges. |
paulb@136 | 65 | |
paulb@147 | 66 | types_xml = self.prepare_document("types") |
paulb@668 | 67 | XSLFormsResource.init_document(self, trans, form, references={"type" : types_xml}) |
paulb@668 | 68 | |
paulb@668 | 69 | def respond_to_document(self, trans, form): |
paulb@260 | 70 | |
paulb@260 | 71 | # Add the comments. |
paulb@260 | 72 | |
paulb@260 | 73 | comments_xsl_list = self.prepare_transform("comments") |
paulb@668 | 74 | form.set_document( |
paulb@668 | 75 | self.get_result(comments_xsl_list, form.get_document()) |
paulb@668 | 76 | ) |
paulb@122 | 77 | |
paulb@668 | 78 | def create_output(self, trans, form): |
paulb@174 | 79 | |
paulb@122 | 80 | # Complete the response. |
paulb@122 | 81 | |
paulb@668 | 82 | stylesheet_parameters = {} |
paulb@668 | 83 | |
paulb@499 | 84 | try: |
paulb@499 | 85 | stylesheet_parameters["locale"] = trans.get_content_languages()[0] |
paulb@499 | 86 | except IndexError: |
paulb@499 | 87 | pass |
paulb@668 | 88 | |
paulb@668 | 89 | XSLFormsResource.create_output(self, trans, form, |
paulb@668 | 90 | stylesheet_parameters=stylesheet_parameters, |
paulb@668 | 91 | references={ |
paulb@668 | 92 | "translations" : self.prepare_document("translations") |
paulb@668 | 93 | }) |
paulb@122 | 94 | |
paulb@122 | 95 | # Site map initialisation. |
paulb@122 | 96 | |
paulb@122 | 97 | def get_site(): |
paulb@122 | 98 | |
paulb@122 | 99 | "Return a simple Web site resource." |
paulb@122 | 100 | |
paulb@174 | 101 | # Get the main resource and the directory used by the application. |
paulb@174 | 102 | |
paulb@174 | 103 | very_simple_resource = VerySimpleResource() |
paulb@174 | 104 | directory = very_simple_resource.resource_dir |
paulb@174 | 105 | |
paulb@174 | 106 | # Make a simple Web site. |
paulb@174 | 107 | |
paulb@174 | 108 | resource = MapResource({ |
paulb@174 | 109 | # Static resources: |
paulb@174 | 110 | "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), |
paulb@181 | 111 | # Main page and in-page resources: |
paulb@181 | 112 | None : very_simple_resource |
paulb@174 | 113 | }) |
paulb@174 | 114 | |
paulb@560 | 115 | return EncodingSelector(resource, encoding) |
paulb@122 | 116 | |
paulb@514 | 117 | # Resource preparation ahead of time - useful for making installations. |
paulb@514 | 118 | |
paulb@514 | 119 | def prepare_resources(): |
paulb@514 | 120 | for cls in [VerySimpleResource]: |
paulb@668 | 121 | xslforms_prepare_resources(cls) |
paulb@514 | 122 | |
paulb@122 | 123 | # vim: tabstop=4 expandtab shiftwidth=4 |