1 #!/usr/bin/env python 2 3 "A very simple example application." 4 5 import WebStack.Generic 6 import XSLForms.Resources.WebResources 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.WebResources.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 "structure" : ("structure_multivalue_label_template.xhtml", "structure_output.xsl") 27 } 28 init_resources = { 29 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 30 #"structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 31 "structure" : ("structure_multivalue_label_template.xhtml", "structure_input.xsl") 32 } 33 transform_resources = { 34 "comments" : ["structure_comments.xsl"] 35 } 36 document_resources = { 37 #"types" : "structure_types.xml" 38 "types" : "structure_types_label.xml", 39 "translations" : "translations.xml" 40 } 41 in_page_resources = { 42 "comments" : ("structure", "structure_output_comments.xsl", "comment-node") 43 } 44 45 def respond_to_form(self, trans, form): 46 47 """ 48 Respond to a request having the given transaction 'trans' and the given 49 'form' information. 50 """ 51 52 in_page_resource = self.get_in_page_resource(trans) 53 parameters = form.get_parameters() 54 documents = form.get_documents() 55 56 # Ensure the presence of a document. 57 58 if documents.has_key("structure"): 59 structure = documents["structure"] 60 else: 61 structure = form.new_instance("structure") 62 63 # Add and remove elements according to the selectors found. 64 65 selectors = form.get_selectors() 66 XSLForms.Utils.remove_elements(selectors.get("remove2")) 67 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 68 XSLForms.Utils.remove_elements(selectors.get("remove")) 69 XSLForms.Utils.add_elements(selectors.get("add"), "item") 70 71 # Initialise the document, adding enumerations/ranges. 72 73 structure_xsl = self.prepare_initialiser("structure") 74 types_xml = self.prepare_document("types") 75 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 76 77 # Add the comments. 78 79 comments_xsl_list = self.prepare_transform("comments") 80 structure = self.get_result(comments_xsl_list, structure) 81 82 # Start the response. 83 84 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", self.encoding)) 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(in_page_resource) 90 stylesheet_parameters = self.prepare_parameters(parameters) 91 else: 92 trans_xsl = self.prepare_output("structure") 93 stylesheet_parameters = {} 94 95 # Complete the response. 96 97 try: 98 stylesheet_parameters["locale"] = trans.get_content_languages()[0] 99 except IndexError: 100 pass 101 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters, 102 references={"translations" : self.prepare_document("translations")}) 103 104 # Site map initialisation. 105 106 def get_site(): 107 108 "Return a simple Web site resource." 109 110 # Get the main resource and the directory used by the application. 111 112 very_simple_resource = VerySimpleResource() 113 directory = very_simple_resource.resource_dir 114 115 # Make a simple Web site. 116 117 resource = MapResource({ 118 # Static resources: 119 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 120 # Main page and in-page resources: 121 None : very_simple_resource 122 }) 123 124 return resource 125 126 # Resource preparation ahead of time - useful for making installations. 127 128 def prepare_resources(): 129 for cls in [VerySimpleResource]: 130 XSLForms.Resources.WebResources.prepare_resources(cls) 131 132 # vim: tabstop=4 expandtab shiftwidth=4