1 #!/usr/bin/env python 2 # -*- coding: iso-8859-1 -*- 3 4 "A very simple example application." 5 6 import WebStack.Generic 7 import XSLForms.Resources.WebResources 8 import XSLForms.Utils 9 import os 10 11 # Site map imports. 12 13 from WebStack.Resources.ResourceMap import MapResource 14 from WebStack.Resources.Selectors import EncodingSelector, PathSelector 15 from WebStack.Resources.Static import DirectoryResource, StringResource 16 from XSLForms.Resources.OpenIDLogin import OpenIDLoginResource, Authenticator 17 from XSLForms.Resources.OpenIDRedirect import OpenIDRedirectResource, OpenIDRedirectAuthenticator 18 from XSLForms.Resources.OpenIDInitiation import OpenIDInitiationResource 19 20 # Configuration settings. 21 22 encoding = "utf-8" 23 24 # Resource classes. 25 26 class VerySimpleResource(XSLForms.Resources.WebResources.XSLFormsResource): 27 28 "A very simple resource providing a hierarchy of editable fields." 29 30 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 31 template_resources = { 32 #"structure" : ("structure_template.xhtml", "structure_output.xsl") 33 #"structure" : ("structure_multivalue_template.xhtml", "structure_output.xsl") 34 "structure" : ("structure_multivalue_label_template.xhtml", "structure_output.xsl") 35 } 36 init_resources = { 37 #"structure" : ("structure_template.xhtml", "structure_input.xsl") 38 #"structure" : ("structure_multivalue_template.xhtml", "structure_input.xsl") 39 "structure" : ("structure_multivalue_label_template.xhtml", "structure_input.xsl") 40 } 41 transform_resources = { 42 "comments" : ["structure_comments.xsl"] 43 } 44 document_resources = { 45 #"types" : "structure_types.xml" 46 "types" : "structure_types_label.xml", 47 "translations" : "translations.xml" 48 } 49 in_page_resources = { 50 "comments" : ("structure", "structure_output_comments.xsl", "comment-node") 51 } 52 53 def respond_to_form(self, trans, form): 54 55 """ 56 Respond to a request having the given transaction 'trans' and the given 57 'form' information. 58 """ 59 60 in_page_resource = self.get_in_page_resource(trans) 61 parameters = form.get_parameters() 62 documents = form.get_documents() 63 64 # Ensure the presence of a document. 65 66 if documents.has_key("structure"): 67 structure = documents["structure"] 68 else: 69 structure = form.new_instance("structure") 70 71 # Add and remove elements according to the selectors found. 72 73 selectors = form.get_selectors() 74 XSLForms.Utils.remove_elements(selectors.get("remove2")) 75 XSLForms.Utils.add_elements(selectors.get("add2"), "subitem") 76 XSLForms.Utils.remove_elements(selectors.get("remove")) 77 XSLForms.Utils.add_elements(selectors.get("add"), "item") 78 79 # Initialise the document, adding enumerations/ranges. 80 81 structure_xsl = self.prepare_initialiser("structure") 82 types_xml = self.prepare_document("types") 83 structure = self.get_result([structure_xsl], structure, references={"type" : types_xml}) 84 85 # Add the comments. 86 87 comments_xsl_list = self.prepare_transform("comments") 88 structure = self.get_result(comments_xsl_list, structure) 89 90 # Start the response. 91 92 trans.set_content_type(WebStack.Generic.ContentType("application/xhtml+xml", encoding)) 93 94 # Ensure that an output stylesheet exists. 95 96 if in_page_resource in self.in_page_resources.keys(): 97 trans_xsl = self.prepare_fragment(in_page_resource) 98 stylesheet_parameters = self.prepare_parameters(parameters) 99 else: 100 trans_xsl = self.prepare_output("structure") 101 stylesheet_parameters = {} 102 103 # Complete the response. 104 105 try: 106 stylesheet_parameters["locale"] = trans.get_content_languages()[0] 107 except IndexError: 108 pass 109 self.send_output(trans, [trans_xsl], structure, stylesheet_parameters, 110 references={"translations" : self.prepare_document("translations")}) 111 112 class InitiationResource(XSLForms.Resources.OpenIDInitiation.OpenIDInitiationResource): 113 114 "Special initiation screen for this application." 115 116 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 117 template_resources = { 118 "initiation" : ("initiation_template.xhtml", "initiation_output.xsl"), 119 "success" : ("initiation_success_template.xhtml", "initiation_success_output.xsl") 120 } 121 122 class LoginResource(XSLForms.Resources.OpenIDLogin.OpenIDLoginResource): 123 124 "Special login screen for this application." 125 126 resource_dir = os.path.join(os.path.split(__file__)[0], "Resources") 127 template_resources = { 128 "login" : ("login_template.xhtml", "login_output.xsl"), 129 "success" : ("success_template.xhtml", "success_output.xsl") 130 } 131 132 # Site map initialisation. 133 134 def get_site(host, secret_key, use_redirect=1): 135 136 "Return a simple Web site resource." 137 138 # Authentication objects. 139 140 credentials = [ 141 ("badger", "123"), 142 ("vole", "abc") 143 ] 144 login_authenticator = Authenticator(credentials) 145 login_redirect_authenticator = OpenIDRedirectAuthenticator(secret_key, host) 146 147 # Get the main resource and the directory used by the application. 148 149 very_simple_resource = VerySimpleResource() 150 directory = very_simple_resource.resource_dir 151 152 # Get some initiation/login resources. 153 154 login_redirect_resource = XSLForms.Resources.OpenIDRedirect.OpenIDRedirectResource( 155 host, 156 "initiation", # see below 157 resource=very_simple_resource, 158 authenticator=login_redirect_authenticator, 159 logout_parameter_name="logout" 160 ) 161 initiation_resource = InitiationResource(use_redirect=use_redirect) 162 login_resource = LoginResource(login_authenticator, use_redirect) 163 164 # Make a simple Web site. 165 166 resource = MapResource({ 167 # Static resources: 168 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 169 # Initiation resource: 170 "initiation" : initiation_resource, 171 # Login resource: 172 "login" : login_resource, 173 174 # Some local OpenID pages. 175 176 "badger" : 177 StringResource(""" 178 <html> 179 <head> 180 <link rel="openid2.provider openid.server" href="%s/login" /> 181 <link rel="openid2.local_id openid.delegate" href="%s/badger" /> 182 <title>Badger's Home Page</title> 183 </head> 184 <body> 185 <p>Home page for the OpenID authenticated user, <code>badger</code>.</p> 186 </body> 187 </html> 188 """ % (host, host), WebStack.Generic.ContentType("text/html")), 189 u"???" : 190 StringResource(u""" 191 <html> 192 <head> 193 <link rel="openid2.provider openid.server" href="%s/login" /> 194 <link rel="openid2.local_id openid.delegate" href="%s/???" /> 195 <title>Home Page for ???</title> 196 </head> 197 <body> 198 <p>Home page for the OpenID authenticated user, <code>???</code>.</p> 199 </body> 200 </html> 201 """ % (host, host), WebStack.Generic.ContentType("text/html", "iso-8859-1")), 202 "vole" : 203 StringResource(""" 204 <html> 205 <head> 206 <link rel="openid2.provider openid.server" href="%s/login" /> 207 <link rel="openid2.local_id openid.delegate" href="%s/vole" /> 208 <title>Vole's Home Page</title> 209 </head> 210 <body> 211 <p>Home page for the OpenID authenticated user, <code>vole</code>.</p> 212 </body> 213 </html> 214 """ % (host, host), WebStack.Generic.ContentType("text/html")), 215 216 # Main page and in-page resources: 217 # (Now protected!) 218 None : login_redirect_resource 219 }) 220 221 # Wrap the resource up with information about the application root. 222 223 return EncodingSelector(PathSelector(resource), encoding) 224 225 # Resource preparation ahead of time - useful for making installations. 226 227 def prepare_resources(): 228 for cls in [VerySimpleResource, LoginResource, InitiationResource]: 229 XSLForms.Resources.WebResources.prepare_resources(cls) 230 231 # vim: tabstop=4 expandtab shiftwidth=4