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.Selectors import EncodingSelector, PathSelector 14 from WebStack.Resources.Static import DirectoryResource 15 from WebStack.Resources.Login import LoginAuthenticator 16 from WebStack.Resources.LoginRedirect import LoginRedirectAuthenticator 17 import XSLForms.Resources.Login 18 19 # Configuration settings. 20 21 encoding = "utf-8" 22 23 # Resource classes. 24 25 class VerySimpleResource(XSLForms.Resources.WebResources.XSLFormsResource): 26 27 "A very simple resource providing a hierarchy of editable fields." 28 29 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 30 template_resources = { 31 #"structure" : ("structure_template.xhtml", "structure_output.xsl") 32 #"structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") 33 "structure" : ("structure_multivalue_label_template.xhtml", "structure_output.xsl") 34 } 35 init_resources = { 36 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 37 #"structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 38 "structure" : ("structure_multivalue_label_template.xhtml", "structure_input.xsl") 39 } 40 transform_resources = { 41 "comments" : ["structure_comments.xsl"] 42 } 43 document_resources = { 44 #"types" : "structure_types.xml" 45 "types" : "structure_types_label.xml", 46 "translations" : "translations.xml" 47 } 48 in_page_resources = { 49 "comments" : ("structure", "structure_output_comments.xsl", "comment-node") 50 } 51 52 def respond_to_form(self, trans, form): 53 54 """ 55 Respond to a request having the given transaction 'trans' and the given 56 'form' information. 57 """ 58 59 in_page_resource = self.get_in_page_resource(trans) 60 parameters = form.get_parameters() 61 documents = form.get_documents() 62 63 # Ensure the presence of a document. 64 65 if documents.has_key("structure"): 66 structure = documents["structure"] 67 else: 68 structure = form.new_instance("structure") 69 70 # Add and remove elements according to the selectors found. 71 72 selectors = form.get_selectors() 73 XSLForms.Utils.remove_elements(selectors.get("remove2")) 74 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 75 XSLForms.Utils.remove_elements(selectors.get("remove")) 76 XSLForms.Utils.add_elements(selectors.get("add"), "item") 77 78 # Initialise the document, adding enumerations/ranges. 79 80 structure_xsl = self.prepare_initialiser("structure") 81 types_xml = self.prepare_document("types") 82 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 83 84 # Add the comments. 85 86 comments_xsl_list = self.prepare_transform("comments") 87 structure = self.get_result(comments_xsl_list, structure) 88 89 # Start the response. 90 91 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) 92 93 # Ensure that an output stylesheet exists. 94 95 if in_page_resource in self.in_page_resources.keys(): 96 trans_xsl = self.prepare_fragment(in_page_resource) 97 stylesheet_parameters = self.prepare_parameters(parameters) 98 else: 99 trans_xsl = self.prepare_output("structure") 100 stylesheet_parameters = {} 101 102 # Complete the response. 103 104 try: 105 stylesheet_parameters["locale"] = trans.get_content_languages()[0] 106 except IndexError: 107 pass 108 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters, 109 references={"translations" : self.prepare_document("translations")}) 110 111 class LoginResource(XSLForms.Resources.Login.LoginResource): 112 113 "Special login screen for this application." 114 115 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 116 template_resources = { 117 "login" : ("login_template.xhtml", "login_output.xsl"), 118 } 119 120 # Site map initialisation. 121 122 def get_site(host): 123 124 "Return a simple Web site resource." 125 126 # Authentication objects. 127 128 secret_key="this_must_be_secret" 129 credentials = [ 130 ("badger", "123"), 131 ("vole", "abc") 132 ] 133 login_authenticator = LoginAuthenticator(secret_key, credentials) 134 login_redirect_authenticator = LoginRedirectAuthenticator(secret_key) 135 136 # Get the main resource and the directory used by the application. 137 138 very_simple_resource = VerySimpleResource() 139 directory = very_simple_resource.resource_dir 140 141 # Get some login resources. 142 143 login_redirect_resource = XSLForms.Resources.Login.LoginRedirectResource( 144 host, 145 "login", # see below 146 resource=very_simple_resource, 147 authenticator=login_redirect_authenticator 148 ) 149 login_resource = LoginResource(login_authenticator) 150 151 # Make a simple Web site. 152 153 resource = MapResource({ 154 # Static resources: 155 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 156 # Login resource: 157 "login" : login_resource, 158 # Main page and in-page resources: 159 # (Now protected!) 160 None : login_redirect_resource 161 }) 162 163 # Wrap the resource up with information about the application root. 164 165 return EncodingSelector(PathSelector(resource), encoding) 166 167 # Resource preparation ahead of time - useful for making installations. 168 169 def prepare_resources(): 170 for cls in [VerySimpleResource, LoginResource]: 171 XSLForms.Resources.WebResources.prepare_resources(cls) 172 173 # vim: tabstop=4 expandtab shiftwidth=4