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