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, deployed_at=None): 135 136 "Return a simple Web site resource." 137 138 # Configure the user home pages below. 139 140 deployed_at = deployed_at or host 141 142 # Authentication objects. 143 144 credentials = [ 145 ((deployed_at + "/badger", "badger"), "abc"), 146 ((deployed_at + "/vole", "vole"), "xyz"), 147 ((deployed_at + u"/???", u"???"), u"???") 148 ] 149 login_authenticator = Authenticator(credentials) 150 login_redirect_authenticator = OpenIDRedirectAuthenticator(secret_key, host) 151 152 # Get the main resource and the directory used by the application. 153 154 very_simple_resource = VerySimpleResource() 155 directory = very_simple_resource.resource_dir 156 157 # Get some initiation/login resources. 158 159 login_redirect_resource = XSLForms.Resources.OpenIDRedirect.OpenIDRedirectResource( 160 host, 161 "initiation", # see below 162 resource=very_simple_resource, 163 authenticator=login_redirect_authenticator, 164 logout_parameter_name="logout" 165 ) 166 initiation_resource = InitiationResource(use_redirect=use_redirect) 167 login_resource = LoginResource(host, login_authenticator, use_redirect=use_redirect) 168 169 # Make a simple Web site. 170 171 resource = MapResource({ 172 # Static resources: 173 "scripts" : DirectoryResource(os.path.join(directory, "scripts"), {"js" : "text/javascript"}), 174 # Initiation resource: 175 "initiation" : initiation_resource, 176 # Login resource: 177 "login" : login_resource, 178 179 # Some local OpenID pages. 180 181 "badger" : 182 StringResource(""" 183 <html> 184 <head> 185 <link rel="openid2.provider openid.server" href="%s/login" /> 186 <link rel="openid2.local_id openid.delegate" href="%s/badger" /> 187 <title>Badger's Home Page</title> 188 </head> 189 <body> 190 <p>Home page for the OpenID authenticated user, <code>badger</code>.</p> 191 </body> 192 </html> 193 """ % (deployed_at, deployed_at), WebStack.Generic.ContentType("text/html")), 194 u"???" : 195 StringResource(u""" 196 <html> 197 <head> 198 <link rel="openid2.provider openid.server" href="%s/login" /> 199 <link rel="openid2.local_id openid.delegate" href="%s/???" /> 200 <title>Home Page for ???</title> 201 </head> 202 <body> 203 <p>Home page for the OpenID authenticated user, <code>???</code>.</p> 204 </body> 205 </html> 206 """ % (deployed_at, deployed_at), WebStack.Generic.ContentType("text/html", "iso-8859-1")), 207 "vole" : 208 StringResource(""" 209 <html> 210 <head> 211 <link rel="openid2.provider openid.server" href="%s/login" /> 212 <link rel="openid2.local_id openid.delegate" href="%s/vole" /> 213 <title>Vole's Home Page</title> 214 </head> 215 <body> 216 <p>Home page for the OpenID authenticated user, <code>vole</code>.</p> 217 </body> 218 </html> 219 """ % (deployed_at, deployed_at), WebStack.Generic.ContentType("text/html")), 220 221 # Main page and in-page resources: 222 # (Now protected!) 223 None : login_redirect_resource 224 }) 225 226 # Wrap the resource up with information about the application root. 227 228 return EncodingSelector(PathSelector(resource), encoding) 229 230 # Resource preparation ahead of time - useful for making installations. 231 232 def prepare_resources(): 233 for cls in [VerySimpleResource, LoginResource, InitiationResource]: 234 XSLForms.Resources.WebResources.prepare_resources(cls) 235 236 # vim: tabstop=4 expandtab shiftwidth=4