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