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 transform_resources = { 28 #"types" : ["structure_types.xsl"] 29 #"types" : ["structure_multivalue_types.xsl"] 30 "types" : ["structure_multivalue_types.xsl", "structure_comments.xsl"] 31 } 32 document_resources = { 33 "types" : "structure_types.xml" 34 } 35 in_page_resources = { 36 "comments" : ("structure_output_comments.xsl", "comment-node") 37 } 38 39 def respond_to_form(self, trans, form): 40 41 """ 42 Respond to a request having the given transaction 'trans' and the given 43 'form' information. 44 """ 45 46 in_page_resource = self.get_in_page_resource(trans) 47 parameters = form.get_parameters() 48 documents = form.get_documents() 49 50 # Ensure the presence of a document. 51 52 if documents.has_key("structure"): 53 structure = documents["structure"] 54 else: 55 structure = form.new_instance("structure") 56 57 # Add and remove elements according to the selectors found. 58 59 selectors = form.get_selectors() 60 XSLForms.Utils.remove_elements(selectors.get("remove2")) 61 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 62 XSLForms.Utils.remove_elements(selectors.get("remove")) 63 XSLForms.Utils.add_elements(selectors.get("add"), "item") 64 65 # Transform, adding enumerations/ranges. 66 67 types_xsl_list = self.prepare_transform("types") 68 types_xml = self.prepare_document("types") 69 structure = self.get_result(types_xsl_list, structure, references={"types" : types_xml}) 70 71 # Start the response. 72 73 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 74 75 # Define the stylesheet parameters. 76 77 stylesheet_parameters = {} 78 79 # Ensure that an output stylesheet exists. 80 81 if in_page_resource in self.in_page_resources.keys(): 82 trans_xsl = self.prepare_fragment("structure", in_page_resource) 83 element_path = parameters.get("element-path", [""])[0] 84 stylesheet_parameters["element-path"] = element_path 85 else: 86 trans_xsl = self.prepare_output("structure") 87 88 # Add information essential for in-page requests. 89 90 if trans.get_server_port() == "80": 91 stylesheet_parameters["application-url"] = \ 92 "http://%s%s" % (trans.get_server_name(), trans.get_path_without_query()) 93 else: 94 stylesheet_parameters["application-url"] = \ 95 "http://%s:%s%s" % (trans.get_server_name(), trans.get_server_port(), trans.get_path_without_query()) 96 97 # Complete the response. 98 99 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters) 100 101 # Site map initialisation. 102 103 def get_site(): 104 105 "Return a simple Web site resource." 106 107 # Get the main resource and the directory used by the application. 108 109 very_simple_resource = VerySimpleResource() 110 directory = very_simple_resource.resource_dir 111 112 # Make a simple Web site. 113 114 resource = MapResource({ 115 # Static resources: 116 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 117 # Main page: 118 "" : very_simple_resource, 119 # Fragments: 120 "comments" : very_simple_resource 121 }) 122 123 return resource 124 125 # vim: tabstop=4 expandtab shiftwidth=4