paulb@235 | 1 | #!/usr/bin/env python |
paulb@235 | 2 | |
paulb@235 | 3 | "A job candidate editing application." |
paulb@235 | 4 | |
paulb@235 | 5 | import WebStack.Generic |
paulb@405 | 6 | from WebStack.Repositories.Directory import DirectoryRepository |
paulb@638 | 7 | from XSLForms.Resources.WebResources import \ |
paulb@638 | 8 | XSLFormsResource, input, output, resources, prepare_resources as xslforms_prepare_resources |
paulb@235 | 9 | import os |
paulb@402 | 10 | import libxml2dom |
paulb@235 | 11 | |
paulb@235 | 12 | # Site map imports. |
paulb@235 | 13 | |
paulb@235 | 14 | from WebStack.Resources.ResourceMap import MapResource |
paulb@560 | 15 | from WebStack.Resources.Selectors import EncodingSelector |
paulb@235 | 16 | from WebStack.Resources.Static import DirectoryResource |
paulb@235 | 17 | |
paulb@560 | 18 | # Configuration setting. |
paulb@560 | 19 | |
paulb@560 | 20 | encoding = "utf-8" |
paulb@560 | 21 | |
paulb@235 | 22 | # Resource classes. |
paulb@235 | 23 | |
paulb@634 | 24 | class AdminResource(XSLFormsResource): |
paulb@402 | 25 | |
paulb@402 | 26 | "A resource providing administration facilities for job candidate profiles." |
paulb@402 | 27 | |
paulb@634 | 28 | resource_dir = resources(__file__) |
paulb@402 | 29 | template_resources = { |
paulb@634 | 30 | "admin" : output("admin_template.xhtml") |
paulb@402 | 31 | } |
paulb@402 | 32 | init_resources = { |
paulb@634 | 33 | "admin" : input("admin_template.xhtml") |
paulb@402 | 34 | } |
paulb@402 | 35 | |
paulb@498 | 36 | def __init__(self, repository): |
paulb@498 | 37 | self.repository = repository |
paulb@498 | 38 | |
paulb@638 | 39 | def select_activity(self, trans, form): |
paulb@642 | 40 | form.set_activity("admin") |
paulb@402 | 41 | |
paulb@638 | 42 | def create_document(self, trans, form): |
paulb@638 | 43 | is_new = XSLFormsResource.create_document(self, trans, form) |
paulb@638 | 44 | trans.get_attributes()["form_is_new"] = is_new |
paulb@638 | 45 | return is_new |
paulb@402 | 46 | |
paulb@638 | 47 | def respond_to_input(self, trans, form): |
paulb@402 | 48 | |
paulb@402 | 49 | # Get the "show" and "edit" resource paths. |
paulb@402 | 50 | # NOTE: These should be obtained from the site map. |
paulb@402 | 51 | |
paulb@560 | 52 | vpath = trans.get_processed_virtual_path_info() |
paulb@560 | 53 | show_path = trans.get_path_without_info() + trans.update_path(vpath, "show") |
paulb@560 | 54 | edit_path = trans.get_path_without_info() + trans.update_path(vpath, "edit") |
paulb@402 | 55 | |
paulb@402 | 56 | # Redirect if one of the CVs is to be shown or edited. |
paulb@402 | 57 | |
paulb@402 | 58 | selectors = form.get_selectors() |
paulb@402 | 59 | if selectors.has_key("show"): |
paulb@402 | 60 | name = selectors["show"][0].getAttribute("name") |
paulb@560 | 61 | trans.redirect(trans.encode_path(show_path) + |
paulb@560 | 62 | "?name=%s" % trans.encode_path(name)) |
paulb@402 | 63 | elif selectors.has_key("edit"): |
paulb@402 | 64 | name = selectors["edit"][0].getAttribute("name") |
paulb@560 | 65 | trans.redirect(trans.encode_path(edit_path) + |
paulb@560 | 66 | "?name=%s" % trans.encode_path(name)) |
paulb@402 | 67 | |
paulb@402 | 68 | # Add and remove elements according to the selectors found. |
paulb@402 | 69 | |
paulb@638 | 70 | self.remove_elements(selectors.get("remove")) |
paulb@638 | 71 | self.add_elements(selectors.get("new"), "cv", "cvs") |
paulb@402 | 72 | |
paulb@638 | 73 | def respond_to_document(self, trans, form): |
paulb@402 | 74 | |
paulb@642 | 75 | admin = form.get_document() |
paulb@402 | 76 | |
paulb@408 | 77 | # Synchronise the repository with the CVs found. |
paulb@402 | 78 | |
paulb@402 | 79 | cvs = admin.xpath("/admin/cvs")[0] |
paulb@498 | 80 | for key in self.repository.keys(): |
paulb@402 | 81 | if key.startswith("candidate-"): |
paulb@402 | 82 | name = key[len("candidate-"):] |
paulb@402 | 83 | # NOTE: Apostrophes not quoted. |
paulb@402 | 84 | if not cvs.xpath("cv[@name = '%s']" % name): |
paulb@638 | 85 | if trans.get_attributes()["form_is_new"]: |
paulb@402 | 86 | cv = admin.createElement("cv") |
paulb@402 | 87 | cv.setAttribute("name", name) |
paulb@402 | 88 | cvs.appendChild(cv) |
paulb@402 | 89 | else: |
paulb@498 | 90 | del self.repository[key] |
paulb@402 | 91 | |
paulb@638 | 92 | class CandidateUtils: |
paulb@638 | 93 | |
paulb@638 | 94 | "Methods used by candidate-related resources." |
paulb@638 | 95 | |
paulb@638 | 96 | def __init__(self, repository): |
paulb@638 | 97 | self.repository = repository |
paulb@402 | 98 | |
paulb@638 | 99 | def select_activity(self, trans, form): |
paulb@642 | 100 | form.set_activity("candidate") |
paulb@402 | 101 | |
paulb@638 | 102 | def create_document(self, trans, form): |
paulb@638 | 103 | documents = form.get_documents() |
paulb@638 | 104 | fields = trans.get_fields_from_path() |
paulb@638 | 105 | name = fields.get("name", [u"None"])[0] |
paulb@402 | 106 | |
paulb@638 | 107 | # Ensure the presence of a document. |
paulb@402 | 108 | |
paulb@638 | 109 | if documents.has_key("candidate"): |
paulb@642 | 110 | form.set_document(documents["candidate"]) |
paulb@638 | 111 | else: |
paulb@638 | 112 | if self.repository is None or not self.repository.has_key("candidate-%s" % name): |
paulb@642 | 113 | form.set_document(form.new_instance("candidate")) |
paulb@638 | 114 | else: |
paulb@642 | 115 | form.set_document(libxml2dom.parseString(self.repository["candidate-%s" % name])) |
paulb@402 | 116 | |
paulb@638 | 117 | def init_document(self, trans, form): |
paulb@638 | 118 | status_xml = self.prepare_document("status") |
paulb@638 | 119 | XSLFormsResource.init_document(self, trans, form, references={"status" : status_xml}) |
paulb@402 | 120 | |
paulb@638 | 121 | class DisplayResource(CandidateUtils, XSLFormsResource): |
paulb@402 | 122 | |
paulb@402 | 123 | "A resource providing editing facilities for a job candidate profile." |
paulb@402 | 124 | |
paulb@634 | 125 | resource_dir = resources(__file__) |
paulb@402 | 126 | template_resources = { |
paulb@638 | 127 | "candidate" : output("candidate_display_template.xhtml") |
paulb@402 | 128 | } |
paulb@402 | 129 | init_resources = { |
paulb@634 | 130 | "candidate" : input("candidate_template.xhtml") |
paulb@402 | 131 | } |
paulb@402 | 132 | document_resources = { |
paulb@402 | 133 | "status" : "candidate_status.xml" |
paulb@402 | 134 | } |
paulb@402 | 135 | |
paulb@638 | 136 | class CandidateResource(CandidateUtils, XSLFormsResource): |
paulb@235 | 137 | |
paulb@235 | 138 | "A resource providing editing facilities for a job candidate profile." |
paulb@235 | 139 | |
paulb@634 | 140 | resource_dir = resources(__file__) |
paulb@235 | 141 | template_resources = { |
paulb@634 | 142 | "candidate" : output("candidate_template.xhtml") |
paulb@235 | 143 | } |
paulb@249 | 144 | init_resources = { |
paulb@634 | 145 | "candidate" : input("candidate_template.xhtml") |
paulb@235 | 146 | } |
paulb@235 | 147 | document_resources = { |
paulb@235 | 148 | "status" : "candidate_status.xml" |
paulb@235 | 149 | } |
paulb@235 | 150 | |
paulb@638 | 151 | def respond_to_input(self, trans, form): |
paulb@498 | 152 | |
paulb@638 | 153 | # Add and remove elements according to the selectors found. |
paulb@235 | 154 | |
paulb@638 | 155 | selectors = form.get_selectors() |
paulb@638 | 156 | self.remove_elements(selectors.get("remove")) |
paulb@638 | 157 | self.add_elements(selectors.get("add-skill"), "skill", "skills") |
paulb@638 | 158 | self.add_elements(selectors.get("add-qualification"), "qualification", "qualifications") |
paulb@638 | 159 | self.add_elements(selectors.get("add-employment"), "employment", "experience") |
paulb@235 | 160 | |
paulb@638 | 161 | def respond_to_document(self, trans, form): |
paulb@638 | 162 | |
paulb@642 | 163 | candidate = form.get_document() |
paulb@235 | 164 | parameters = form.get_parameters() |
paulb@560 | 165 | fields = trans.get_fields_from_path() |
paulb@402 | 166 | name = fields.get("name", [u"None"])[0] |
paulb@402 | 167 | |
paulb@402 | 168 | # Get the "show" resource path. |
paulb@402 | 169 | # NOTE: This should be obtained from the site map. |
paulb@402 | 170 | |
paulb@560 | 171 | vpath = trans.get_processed_virtual_path_info() |
paulb@560 | 172 | show_path = trans.get_path_without_info() + trans.update_path(vpath, "show") |
paulb@560 | 173 | admin_path = trans.get_path_without_info() + trans.update_path(vpath, "") |
paulb@235 | 174 | |
paulb@402 | 175 | # Redirect if the CV is to be shown. |
paulb@402 | 176 | |
paulb@402 | 177 | if parameters.has_key("show"): |
paulb@402 | 178 | |
paulb@402 | 179 | # Save the candidate information. |
paulb@402 | 180 | |
paulb@498 | 181 | self.repository["candidate-%s" % name] = candidate.toString() |
paulb@560 | 182 | trans.redirect(trans.encode_path(show_path) + |
paulb@560 | 183 | "?name=%s" % trans.encode_path(name)) |
paulb@402 | 184 | |
paulb@402 | 185 | # Redirect if the administration interface is to be used. |
paulb@402 | 186 | |
paulb@402 | 187 | elif parameters.has_key("admin"): |
paulb@402 | 188 | |
paulb@402 | 189 | # Save the candidate information. |
paulb@402 | 190 | |
paulb@498 | 191 | self.repository["candidate-%s" % name] = candidate.toString() |
paulb@560 | 192 | trans.redirect(trans.encode_path(admin_path)) |
paulb@402 | 193 | |
paulb@235 | 194 | # Site map initialisation. |
paulb@235 | 195 | |
paulb@513 | 196 | def get_site(fsencoding=None, use_cwd=0): |
paulb@235 | 197 | |
paulb@235 | 198 | "Return a simple Web site resource." |
paulb@235 | 199 | |
paulb@513 | 200 | if use_cwd: |
paulb@513 | 201 | resource_dir = os.getcwd() |
paulb@513 | 202 | else: |
paulb@634 | 203 | resource_dir = resources(__file__) |
paulb@498 | 204 | repository = DirectoryRepository(os.path.join(resource_dir, "candidates"), fsencoding) |
paulb@498 | 205 | |
paulb@235 | 206 | # Get the main resource and the directory used by the application. |
paulb@235 | 207 | |
paulb@498 | 208 | candidate_resource = CandidateResource(repository) |
paulb@498 | 209 | display_resource = DisplayResource(repository) |
paulb@498 | 210 | admin_resource = AdminResource(repository) |
paulb@235 | 211 | |
paulb@235 | 212 | # Make a simple Web site. |
paulb@235 | 213 | |
paulb@235 | 214 | resource = MapResource({ |
paulb@402 | 215 | "edit" : candidate_resource, |
paulb@402 | 216 | "show" : display_resource, |
paulb@402 | 217 | "" : admin_resource |
paulb@235 | 218 | }) |
paulb@235 | 219 | |
paulb@560 | 220 | return EncodingSelector(resource, encoding) |
paulb@235 | 221 | |
paulb@513 | 222 | # Resource preparation ahead of time - useful for making installations. |
paulb@513 | 223 | |
paulb@513 | 224 | def prepare_resources(): |
paulb@513 | 225 | for cls in [AdminResource, DisplayResource, CandidateResource]: |
paulb@638 | 226 | xslforms_prepare_resources(cls) |
paulb@513 | 227 | |
paulb@235 | 228 | # vim: tabstop=4 expandtab shiftwidth=4 |