1 #!/usr/bin/env python 2 3 "A very simple example application." 4 5 import WebStack.Generic 6 import XSLForms.Resources 7 import XSLForms.Utils 8 import os 9 10 # Site map imports. 11 12 from WebStack.Resources.ResourceMap import MapResource 13 from WebStack.Resources.Static import DirectoryResource 14 15 # Resource classes. 16 17 class VerySimpleResource(XSLForms.Resources.XSLFormsResource): 18 19 "A very simple resource providing a hierarchy of editable fields." 20 21 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 22 encoding = "utf-8" 23 template_resources = { 24 #"structure" : ("structure_template.xhtml", "structure_output.xsl") 25 "structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") 26 } 27 init_resources = { 28 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 29 "structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 30 } 31 transform_resources = { 32 "comments" : ["structure_comments.xsl"] 33 } 34 document_resources = { 35 "types" : "structure_types.xml" 36 } 37 in_page_resources = { 38 "comments" : ("structure_output_comments.xsl", "comment-node") 39 } 40 41 def respond_to_form(self, trans, form): 42 43 """ 44 Respond to a request having the given transaction 'trans' and the given 45 'form' information. 46 """ 47 48 in_page_resource = self.get_in_page_resource(trans) 49 parameters = form.get_parameters() 50 documents = form.get_documents() 51 52 # Ensure the presence of a document. 53 54 if documents.has_key("structure"): 55 structure = documents["structure"] 56 else: 57 structure = form.new_instance("structure") 58 59 # Add and remove elements according to the selectors found. 60 61 selectors = form.get_selectors() 62 XSLForms.Utils.remove_elements(selectors.get("remove2")) 63 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 64 XSLForms.Utils.remove_elements(selectors.get("remove")) 65 XSLForms.Utils.add_elements(selectors.get("add"), "item") 66 67 # Initialise the document, adding enumerations/ranges. 68 69 structure_xsl = self.prepare_initialiser("structure") 70 types_xml = self.prepare_document("types") 71 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 72 73 # Add the comments. 74 75 comments_xsl_list = self.prepare_transform("comments") 76 structure = self.get_result(comments_xsl_list, structure) 77 78 # Start the response. 79 80 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 81 82 # Define the stylesheet parameters. 83 84 stylesheet_parameters = {} 85 86 # Ensure that an output stylesheet exists. 87 88 if in_page_resource in self.in_page_resources.keys(): 89 trans_xsl = self.prepare_fragment("structure", in_page_resource) 90 element_path = parameters.get("element-path", [""])[0] 91 stylesheet_parameters["element-path"] = element_path 92 else: 93 trans_xsl = self.prepare_output("structure") 94 95 # Complete the response. 96 97 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters) 98 99 # Site map initialisation. 100 101 def get_site(): 102 103 "Return a simple Web site resource." 104 105 # Get the main resource and the directory used by the application. 106 107 very_simple_resource = VerySimpleResource() 108 directory = very_simple_resource.resource_dir 109 110 # Make a simple Web site. 111 112 resource = MapResource({ 113 # Static resources: 114 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 115 # Main page and in-page resources: 116 None : very_simple_resource 117 }) 118 119 return resource 120 121 # vim: tabstop=4 expandtab shiftwidth=4