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 "success" : ("success_template.xhtml", "success_output.xsl") 119 } 120 121 # Site map initialisation. 122 123 def get_site(host, use_redirect=1): 124 125 "Return a simple Web site resource." 126 127 # Authentication objects. 128 129 secret_key="this_must_be_secret" 130 credentials = [ 131 ("badger", "123"), 132 ("vole", "abc") 133 ] 134 login_authenticator = LoginAuthenticator(secret_key, credentials) 135 login_redirect_authenticator = LoginRedirectAuthenticator(secret_key) 136 137 # Get the main resource and the directory used by the application. 138 139 very_simple_resource = VerySimpleResource() 140 directory = very_simple_resource.resource_dir 141 142 # Get some login resources. 143 144 login_redirect_resource = XSLForms.Resources.Login.LoginRedirectResource( 145 host, 146 "login", # see below 147 resource=very_simple_resource, 148 authenticator=login_redirect_authenticator 149 ) 150 login_resource = LoginResource(login_authenticator, use_redirect) 151 152 # Make a simple Web site. 153 154 resource = MapResource({ 155 # Static resources: 156 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 157 # Login resource: 158 "login" : login_resource, 159 # Main page and in-page resources: 160 # (Now protected!) 161 None : login_redirect_resource 162 }) 163 164 # Wrap the resource up with information about the application root. 165 166 return EncodingSelector(PathSelector(resource), encoding) 167 168 # Resource preparation ahead of time - useful for making installations. 169 170 def prepare_resources(): 171 for cls in [VerySimpleResource, LoginResource]: 172 XSLForms.Resources.WebResources.prepare_resources(cls) 173 174 # vim: tabstop=4 expandtab shiftwidth=4