paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinForms library |
paul@0 | 4 | |
paul@21 | 5 | @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@23 | 9 | from compiler import parse |
paul@23 | 10 | from compiler.ast import Const, Dict, Discard, List, Module, Stmt |
paul@3 | 11 | from MoinMoin.action import do_show |
paul@8 | 12 | from MoinMoin.Page import Page |
paul@24 | 13 | from MoinMoin import security, wikiutil |
paul@0 | 14 | from MoinSupport import * |
paul@46 | 15 | from ItemSupport import ItemStoreBase, getSubpageItemStoreForPage, \ |
paul@46 | 16 | getDirectoryItemStoreForPage |
paul@0 | 17 | import re |
paul@0 | 18 | |
paul@35 | 19 | try: |
paul@35 | 20 | from cStringIO import StringIO |
paul@35 | 21 | except ImportError: |
paul@35 | 22 | from StringIO import StringIO |
paul@35 | 23 | |
paul@0 | 24 | __version__ = "0.1" |
paul@0 | 25 | |
paul@4 | 26 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 27 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 28 | |
paul@31 | 29 | class MoinFormDataError(Exception): |
paul@31 | 30 | |
paul@31 | 31 | "An exception indicating a problem with form data." |
paul@31 | 32 | |
paul@31 | 33 | pass |
paul@31 | 34 | |
paul@3 | 35 | # Common action functionality. |
paul@3 | 36 | |
paul@3 | 37 | class MoinFormHandlerAction: |
paul@3 | 38 | |
paul@3 | 39 | "A handler action that can be specialised for individual forms." |
paul@3 | 40 | |
paul@3 | 41 | def __init__(self, pagename, request): |
paul@3 | 42 | self.pagename = pagename |
paul@3 | 43 | self.request = request |
paul@26 | 44 | self.access_handler = None |
paul@29 | 45 | self.attributes = None |
paul@26 | 46 | |
paul@29 | 47 | def getAccessHandler(self): |
paul@29 | 48 | |
paul@29 | 49 | """ |
paul@29 | 50 | Return an access handler for the form whose attributes have been |
paul@29 | 51 | obtained and stored in this instance. |
paul@29 | 52 | """ |
paul@29 | 53 | |
paul@29 | 54 | return FormAccess(self.pagename, self.request, self.attributes) |
paul@3 | 55 | |
paul@3 | 56 | def processForm(self): |
paul@3 | 57 | |
paul@3 | 58 | """ |
paul@3 | 59 | Interpret the request details and modify them according to the structure |
paul@3 | 60 | of the interpreted information. |
paul@3 | 61 | """ |
paul@3 | 62 | |
paul@13 | 63 | _ = self.request.getText |
paul@13 | 64 | |
paul@3 | 65 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 66 | |
paul@3 | 67 | form = get_form(self.request) |
paul@3 | 68 | fields = getFields(form, remove=True) |
paul@3 | 69 | |
paul@29 | 70 | # Detect any request to load data. |
paul@13 | 71 | |
paul@29 | 72 | if fields.has_key("load"): |
paul@29 | 73 | try: |
paul@29 | 74 | number = int(fields["load"][0]) |
paul@29 | 75 | except ValueError: |
paul@29 | 76 | fields = {} |
paul@29 | 77 | else: |
paul@29 | 78 | self.attributes, text = self.getFormForFragment(fields) |
paul@29 | 79 | self.access_handler = self.getAccessHandler() |
paul@31 | 80 | |
paul@31 | 81 | # Attempt to load the form. |
paul@31 | 82 | |
paul@31 | 83 | try: |
paul@35 | 84 | headers, fields = self.loadFields(number) |
paul@31 | 85 | |
paul@31 | 86 | # Absent or inaccessible forms will result in an IndexError. |
paul@31 | 87 | |
paul@31 | 88 | except IndexError: |
paul@31 | 89 | self.request.theme.add_msg(_("The stored data for this form cannot be accessed."), "error") |
paul@31 | 90 | do_show(self.pagename, self.request) |
paul@31 | 91 | return |
paul@31 | 92 | |
paul@31 | 93 | # Bad data will result in a MoinFormDataError. |
paul@31 | 94 | |
paul@31 | 95 | except MoinFormDataError: |
paul@31 | 96 | self.request.theme.add_msg(_("The stored data for this form is in the wrong format."), "error") |
paul@31 | 97 | do_show(self.pagename, self.request) |
paul@31 | 98 | return |
paul@23 | 99 | |
paul@29 | 100 | # Otherwise, process any supplied data. |
paul@29 | 101 | |
paul@29 | 102 | else: |
paul@29 | 103 | # Modify and validate the form. |
paul@29 | 104 | |
paul@29 | 105 | self.modifyFields(fields) |
paul@29 | 106 | |
paul@29 | 107 | # Get the form definition. |
paul@23 | 108 | |
paul@29 | 109 | self.attributes, text = self.getFormForFragment(fields) |
paul@29 | 110 | self.access_handler = self.getAccessHandler() |
paul@29 | 111 | structure = getFormStructure(text, self.request) |
paul@29 | 112 | |
paul@29 | 113 | # Check the permissions on the form. |
paul@13 | 114 | |
paul@29 | 115 | if not self.checkPermissions("write"): |
paul@29 | 116 | self.request.theme.add_msg(_("You do not appear to have access to this form."), "error") |
paul@29 | 117 | do_show(self.pagename, self.request) |
paul@29 | 118 | return |
paul@29 | 119 | |
paul@29 | 120 | # Without any form definition, the page is probably the wrong one. |
paul@13 | 121 | |
paul@29 | 122 | if not structure: |
paul@29 | 123 | self.request.theme.add_msg(_("This page does not provide a form."), "error") |
paul@29 | 124 | do_show(self.pagename, self.request) |
paul@29 | 125 | return |
paul@13 | 126 | |
paul@29 | 127 | # With a form definition, attempt to validate the fields. |
paul@13 | 128 | |
paul@29 | 129 | if self.validateFields(fields, structure): |
paul@29 | 130 | if self.shouldFinish(fields): |
paul@29 | 131 | self.finished(fields, form) |
paul@29 | 132 | return |
paul@29 | 133 | |
paul@32 | 134 | self.unfinished(fields, form) |
paul@8 | 135 | |
paul@8 | 136 | def finished(self, fields, form): |
paul@8 | 137 | |
paul@8 | 138 | "Handle the finished 'fields' and 'form'." |
paul@8 | 139 | |
paul@23 | 140 | self.storeFields(fields) |
paul@8 | 141 | self.unfinished(fields, form) |
paul@8 | 142 | |
paul@8 | 143 | def unfinished(self, fields, form): |
paul@8 | 144 | |
paul@8 | 145 | "Handle the unfinished 'fields' and 'form'." |
paul@8 | 146 | |
paul@8 | 147 | # Serialise and show the form. |
paul@8 | 148 | |
paul@3 | 149 | self.serialiseFields(fields, form) |
paul@3 | 150 | do_show(self.pagename, self.request) |
paul@3 | 151 | |
paul@29 | 152 | def shouldFinish(self, fields): |
paul@29 | 153 | |
paul@29 | 154 | """ |
paul@29 | 155 | Subject to the attributes stored for the form in this instance, return |
paul@29 | 156 | whether any field referenced by the "finishing" attribute is present |
paul@29 | 157 | and thus indicate whether the form handling should finish. |
paul@29 | 158 | """ |
paul@29 | 159 | |
paul@29 | 160 | finishing = self.attributes.has_key("finishing") and self.attributes["finishing"].split(",") |
paul@29 | 161 | |
paul@29 | 162 | if finishing: |
paul@29 | 163 | for name in finishing: |
paul@29 | 164 | if fields.has_key(name): |
paul@29 | 165 | return True |
paul@29 | 166 | |
paul@29 | 167 | return False |
paul@29 | 168 | |
paul@26 | 169 | def getFormForFragment(self, fields): |
paul@8 | 170 | |
paul@26 | 171 | "Return the attributes and text of the form being handled." |
paul@8 | 172 | |
paul@22 | 173 | fragment = fields.get("fragment", [None])[0] |
paul@8 | 174 | text = Page(self.request, self.pagename).get_raw_body() |
paul@26 | 175 | return getFormForFragment(text, fragment) |
paul@23 | 176 | |
paul@23 | 177 | def checkPermissions(self, action): |
paul@23 | 178 | |
paul@23 | 179 | """ |
paul@23 | 180 | Check the permissions of the user against any restrictions specified in |
paul@23 | 181 | the form's 'attributes'. |
paul@23 | 182 | """ |
paul@23 | 183 | |
paul@26 | 184 | return self.access_handler.checkPermissions(action) |
paul@13 | 185 | |
paul@13 | 186 | def validateFields(self, fields, structure): |
paul@13 | 187 | |
paul@13 | 188 | """ |
paul@13 | 189 | Validate the given 'fields' using the given form 'structure', |
paul@13 | 190 | introducing error fields where the individual fields do not conform to |
paul@13 | 191 | their descriptions. |
paul@13 | 192 | """ |
paul@8 | 193 | |
paul@8 | 194 | return self.validateFieldsUsingStructure(fields, structure) |
paul@8 | 195 | |
paul@8 | 196 | def validateFieldsUsingStructure(self, fields, structure): |
paul@8 | 197 | |
paul@8 | 198 | "Validate the given 'fields' using the given 'structure'." |
paul@8 | 199 | |
paul@8 | 200 | _ = self.request.getText |
paul@8 | 201 | valid = True |
paul@8 | 202 | |
paul@8 | 203 | for key, definition in structure.items(): |
paul@8 | 204 | value = fields.get(key) |
paul@8 | 205 | |
paul@8 | 206 | # Enter form sections and validate them. |
paul@8 | 207 | |
paul@8 | 208 | if isinstance(definition, dict): |
paul@8 | 209 | if value: |
paul@8 | 210 | for element in getSectionElements(value): |
paul@11 | 211 | valid = self.validateFieldsUsingStructure(element, structure[key]) and valid |
paul@8 | 212 | |
paul@8 | 213 | # Validate individual fields. |
paul@8 | 214 | |
paul@8 | 215 | elif structure.has_key(key): |
paul@8 | 216 | path, dictpage, label, section, field_args, allowed_values = definition |
paul@8 | 217 | errors = [] |
paul@8 | 218 | |
paul@8 | 219 | # Test for obligatory values. |
paul@8 | 220 | |
paul@10 | 221 | if not value or not value[0]: |
paul@8 | 222 | if field_args.get("required"): |
paul@11 | 223 | |
paul@11 | 224 | # Detect new parts of the structure and avoid producing |
paul@11 | 225 | # premature error messages. |
paul@11 | 226 | |
paul@11 | 227 | if not fields.has_key("_new"): |
paul@11 | 228 | errors.append(_("This field must be filled out.")) |
paul@11 | 229 | else: |
paul@11 | 230 | valid = False |
paul@8 | 231 | else: |
paul@8 | 232 | # Test for unacceptable values. |
paul@8 | 233 | |
paul@8 | 234 | if allowed_values and set(value).difference(allowed_values): |
paul@8 | 235 | errors.append(_("At least one of the choices is not acceptable.")) |
paul@8 | 236 | |
paul@8 | 237 | # Test the number of values. |
paul@8 | 238 | |
paul@8 | 239 | if field_args.get("type") == "select": |
paul@8 | 240 | if field_args.has_key("maxselected"): |
paul@8 | 241 | if len(value) > int(field_args["maxselected"]): |
paul@8 | 242 | errors.append(_("Incorrect number of choices given: need %s.") % field_args["maxselected"]) |
paul@8 | 243 | |
paul@8 | 244 | if errors: |
paul@8 | 245 | fields["%s-error" % key] = errors |
paul@8 | 246 | valid = False |
paul@8 | 247 | |
paul@8 | 248 | return valid |
paul@4 | 249 | |
paul@3 | 250 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 251 | |
paul@3 | 252 | """ |
paul@3 | 253 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 254 | to name the entries. |
paul@3 | 255 | """ |
paul@3 | 256 | |
paul@3 | 257 | for key, value in fields.items(): |
paul@3 | 258 | |
paul@3 | 259 | # Serialise sections. |
paul@3 | 260 | |
paul@3 | 261 | if isinstance(value, dict): |
paul@3 | 262 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 263 | element_ref = "%s$%s" % (key, index) |
paul@3 | 264 | |
paul@3 | 265 | self.serialiseFields(element, form, |
paul@3 | 266 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 267 | ) |
paul@3 | 268 | |
paul@3 | 269 | # Serialise fields. |
paul@3 | 270 | |
paul@3 | 271 | else: |
paul@3 | 272 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 273 | |
paul@3 | 274 | def modifyFields(self, fields): |
paul@3 | 275 | |
paul@3 | 276 | "Modify the given 'fields', removing and adding items." |
paul@3 | 277 | |
paul@3 | 278 | # First, remove fields. |
paul@3 | 279 | |
paul@3 | 280 | for key in fields.keys(): |
paul@3 | 281 | if key.startswith("_remove="): |
paul@3 | 282 | self.removeField(key[8:], fields) |
paul@3 | 283 | |
paul@3 | 284 | # Then, add fields. |
paul@3 | 285 | |
paul@3 | 286 | for key in fields.keys(): |
paul@3 | 287 | if key.startswith("_add="): |
paul@3 | 288 | self.addField(key[5:], fields) |
paul@3 | 289 | |
paul@3 | 290 | def removeField(self, path, fields): |
paul@3 | 291 | |
paul@3 | 292 | """ |
paul@3 | 293 | Remove the section element indicated by the given 'path' from the |
paul@3 | 294 | 'fields'. |
paul@3 | 295 | """ |
paul@3 | 296 | |
paul@3 | 297 | section, (name, index) = getSectionForPath(path, fields) |
paul@17 | 298 | try: |
paul@17 | 299 | del section[name][index] |
paul@17 | 300 | except KeyError: |
paul@17 | 301 | pass |
paul@3 | 302 | |
paul@3 | 303 | def addField(self, path, fields): |
paul@3 | 304 | |
paul@3 | 305 | """ |
paul@3 | 306 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 307 | """ |
paul@3 | 308 | |
paul@3 | 309 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 310 | placeholder = {"_new" : ""} |
paul@3 | 311 | |
paul@3 | 312 | if section.has_key(name): |
paul@3 | 313 | indexes = section[name].keys() |
paul@3 | 314 | max_index = max(map(int, indexes)) |
paul@3 | 315 | section[name][max_index + 1] = placeholder |
paul@3 | 316 | else: |
paul@3 | 317 | max_index = -1 |
paul@3 | 318 | section[name] = {0 : placeholder} |
paul@3 | 319 | |
paul@23 | 320 | # Storage of form submissions. |
paul@23 | 321 | |
paul@23 | 322 | def storeFields(self, fields): |
paul@23 | 323 | |
paul@35 | 324 | """ |
paul@35 | 325 | Store the given 'fields' as a Python object representation with some |
paul@35 | 326 | metadata headers. |
paul@35 | 327 | """ |
paul@35 | 328 | |
paul@35 | 329 | headers = ["Form-Page: %s" % self.pagename] |
paul@35 | 330 | if self.attributes.has_key("fragment"): |
paul@35 | 331 | headers.append("Form-Fragment: %s" % self.attributes["fragment"]) |
paul@35 | 332 | |
paul@35 | 333 | item = "%s\n\n%s" % ("\n".join(headers), repr(fields)) |
paul@23 | 334 | |
paul@26 | 335 | store = FormStore(self.access_handler) |
paul@35 | 336 | store.append(item) |
paul@23 | 337 | |
paul@23 | 338 | def loadFields(self, number): |
paul@23 | 339 | |
paul@23 | 340 | "Load the fields associated with the given submission 'number'." |
paul@23 | 341 | |
paul@26 | 342 | store = FormStore(self.access_handler) |
paul@26 | 343 | return loadFields(store, number) |
paul@26 | 344 | |
paul@26 | 345 | def loadFields(store, number): |
paul@23 | 346 | |
paul@26 | 347 | """ |
paul@26 | 348 | Load the fields from the 'store' that are associated with the given |
paul@35 | 349 | submission 'number', returning the metadata headers and field structure. |
paul@26 | 350 | """ |
paul@23 | 351 | |
paul@49 | 352 | return loadFieldsFromString(store[number]) |
paul@49 | 353 | |
paul@49 | 354 | def loadFieldsFromString(s): |
paul@49 | 355 | |
paul@49 | 356 | """ |
paul@49 | 357 | Load the fields from the given string 's', returning the metadata headers |
paul@49 | 358 | and field structure. |
paul@49 | 359 | """ |
paul@49 | 360 | |
paul@49 | 361 | f = StringIO(s) |
paul@35 | 362 | |
paul@35 | 363 | headers = [] |
paul@35 | 364 | lines = [] |
paul@35 | 365 | |
paul@35 | 366 | # Find all lines before a blank line, marking the end of any headers. |
paul@35 | 367 | |
paul@35 | 368 | line = f.readline() |
paul@35 | 369 | while line.strip(): |
paul@35 | 370 | lines.append(line) |
paul@35 | 371 | line = f.readline() |
paul@35 | 372 | |
paul@35 | 373 | # Get the remaining text. |
paul@35 | 374 | |
paul@35 | 375 | text = f.read() |
paul@35 | 376 | |
paul@35 | 377 | # If there were headers, converted the recorded lines. |
paul@35 | 378 | |
paul@35 | 379 | if text: |
paul@35 | 380 | for line in lines: |
paul@49 | 381 | name, value = [s.strip() for s in line.split(":", 1)] |
paul@49 | 382 | headers.append((name, value)) |
paul@35 | 383 | |
paul@35 | 384 | # Otherwise, rewind to obtain the entire item text for field data. |
paul@35 | 385 | |
paul@35 | 386 | else: |
paul@35 | 387 | f.seek(0) |
paul@35 | 388 | text = f.read() |
paul@35 | 389 | |
paul@35 | 390 | # Check the text and evaluate it if it is well-formed. |
paul@35 | 391 | |
paul@31 | 392 | module = parse(text) |
paul@35 | 393 | |
paul@31 | 394 | if checkStoredFormData(module): |
paul@35 | 395 | return headers, eval(text) |
paul@31 | 396 | else: |
paul@31 | 397 | raise MoinFormDataError, text |
paul@23 | 398 | |
paul@23 | 399 | def checkStoredFormData(node): |
paul@23 | 400 | |
paul@23 | 401 | """ |
paul@23 | 402 | Check the syntax 'node' and its descendants for suitability as parts of |
paul@23 | 403 | a field definition. |
paul@23 | 404 | """ |
paul@23 | 405 | |
paul@35 | 406 | have_child = False |
paul@35 | 407 | |
paul@23 | 408 | for child in node.getChildNodes(): |
paul@35 | 409 | have_child = True |
paul@23 | 410 | if isinstance(child, Const): |
paul@23 | 411 | pass |
paul@23 | 412 | elif not isinstance(child, (Dict, Discard, List, Module, Stmt)) or not checkStoredFormData(child): |
paul@23 | 413 | return False |
paul@23 | 414 | |
paul@35 | 415 | return have_child |
paul@23 | 416 | |
paul@26 | 417 | class FormAccess: |
paul@26 | 418 | |
paul@26 | 419 | "A means of checking access to form data." |
paul@26 | 420 | |
paul@26 | 421 | def __init__(self, pagename, request, attributes): |
paul@26 | 422 | self.pagename = pagename |
paul@26 | 423 | self.request = request |
paul@26 | 424 | self.attributes = attributes |
paul@26 | 425 | |
paul@47 | 426 | def getACL(self): |
paul@47 | 427 | |
paul@47 | 428 | """ |
paul@47 | 429 | Return the access control list for the form. Where no form-specific |
paul@47 | 430 | policy is specified, the page's ACL will be returned. |
paul@47 | 431 | """ |
paul@47 | 432 | |
paul@47 | 433 | if self.attributes.has_key("access"): |
paul@47 | 434 | access = self.attributes["access"] |
paul@47 | 435 | return security.AccessControlList(self.request.cfg, [access]) |
paul@47 | 436 | else: |
paul@47 | 437 | return Page(self.request, self.pagename).getACL(self.request) |
paul@47 | 438 | |
paul@48 | 439 | def getSubpageACL(self): |
paul@48 | 440 | |
paul@48 | 441 | """ |
paul@48 | 442 | Return the access control list for the form for data that will be |
paul@48 | 443 | stored in subpages. Where no form-specific policy is specified, the |
paul@48 | 444 | page's ACL will be used as the basis of the subpage ACL. |
paul@48 | 445 | """ |
paul@48 | 446 | |
paul@48 | 447 | cfg = self.request.cfg |
paul@48 | 448 | |
paul@48 | 449 | acl = self.getACL() |
paul@48 | 450 | new_acl_lines = [] |
paul@48 | 451 | |
paul@48 | 452 | for acl_str in acl.acl_lines: |
paul@48 | 453 | new_acl_line = [] |
paul@48 | 454 | |
paul@48 | 455 | for op, users, rights in security.ACLStringIterator(cfg.acl_rights_valid, acl_str): |
paul@48 | 456 | |
paul@48 | 457 | # Remove "read" rights unless the "admin" right is also present. |
paul@48 | 458 | |
paul@48 | 459 | if op != "-" and "read" in rights and not "admin" in rights: |
paul@48 | 460 | rights.remove("read") |
paul@48 | 461 | |
paul@48 | 462 | # Add "read" rights if absent and "admin" is present. |
paul@48 | 463 | |
paul@48 | 464 | elif op != "-" and not "read" in rights and "admin" in rights: |
paul@48 | 465 | rights.append("read") |
paul@48 | 466 | |
paul@48 | 467 | new_acl_line.append((op, users, rights)) |
paul@48 | 468 | |
paul@48 | 469 | new_acl_lines.append(" ".join([ |
paul@48 | 470 | "%s%s:%s" % (op, ",".join(users), ",".join(rights)) for (op, users, rights) in new_acl_line |
paul@48 | 471 | ])) |
paul@48 | 472 | |
paul@48 | 473 | # Add an extra read-disable rule just to make sure. |
paul@48 | 474 | |
paul@48 | 475 | new_acl_lines.append("-All:read") |
paul@48 | 476 | |
paul@48 | 477 | return security.AccessControlList(cfg, new_acl_lines) |
paul@48 | 478 | |
paul@26 | 479 | def checkPermissions(self, action): |
paul@26 | 480 | |
paul@26 | 481 | """ |
paul@26 | 482 | Check the permissions of the user against any restrictions specified in |
paul@26 | 483 | the form's 'attributes'. |
paul@26 | 484 | """ |
paul@26 | 485 | |
paul@26 | 486 | user = self.request.user |
paul@26 | 487 | |
paul@26 | 488 | # Use the access definition if one is given. |
paul@26 | 489 | |
paul@26 | 490 | if self.attributes.has_key("access"): |
paul@26 | 491 | access = self.attributes["access"] |
paul@26 | 492 | acl = security.AccessControlList(self.request.cfg, [access]) |
paul@26 | 493 | policy = lambda request, pagename, username, action: acl.may(request, username, action) |
paul@26 | 494 | |
paul@26 | 495 | # Otherwise, use the page permissions. |
paul@26 | 496 | |
paul@26 | 497 | else: |
paul@26 | 498 | policy = security._check |
paul@26 | 499 | |
paul@26 | 500 | # The "read" action is only satisfied by the "admin" role. |
paul@26 | 501 | |
paul@26 | 502 | return user and ( |
paul@26 | 503 | action != "read" and policy(self.request, self.pagename, user.name, action) or |
paul@26 | 504 | action == "read" and policy(self.request, self.pagename, user.name, "admin") |
paul@26 | 505 | ) |
paul@26 | 506 | |
paul@46 | 507 | class FormStore(ItemStoreBase): |
paul@23 | 508 | |
paul@23 | 509 | "A form-specific storage mechanism." |
paul@23 | 510 | |
paul@23 | 511 | def __init__(self, handler): |
paul@23 | 512 | |
paul@23 | 513 | "Initialise the store with the form 'handler'." |
paul@23 | 514 | |
paul@23 | 515 | self.handler = handler |
paul@23 | 516 | page = Page(handler.request, handler.pagename) |
paul@32 | 517 | fragment = handler.attributes.get("fragment") |
paul@34 | 518 | suffix = fragment and ("_%s" % fragment) or "" |
paul@34 | 519 | formdir = wikiutil.quoteWikinameFS("form%s" % suffix) |
paul@34 | 520 | lockdir = wikiutil.quoteWikinameFS("lock%s" % suffix) |
paul@46 | 521 | |
paul@46 | 522 | # Use an alternative store type if indicated. |
paul@46 | 523 | |
paul@47 | 524 | self.storetype = handler.attributes.get("storetype") |
paul@47 | 525 | if self.storetype == "subpage": |
paul@46 | 526 | store = getSubpageItemStoreForPage(page, "form_locks/%s" % lockdir) |
paul@46 | 527 | else: |
paul@46 | 528 | store = getDirectoryItemStoreForPage(page, "forms/%s" % formdir, "form_locks/%s" % lockdir) |
paul@46 | 529 | ItemStoreBase.__init__(self, page, store) |
paul@23 | 530 | |
paul@23 | 531 | def can_write(self): |
paul@23 | 532 | |
paul@23 | 533 | """ |
paul@23 | 534 | Permit writing of form data using the form attributes or page |
paul@23 | 535 | permissions. |
paul@23 | 536 | """ |
paul@23 | 537 | |
paul@23 | 538 | return self.handler.checkPermissions("write") |
paul@23 | 539 | |
paul@24 | 540 | def can_read(self): |
paul@24 | 541 | |
paul@24 | 542 | """ |
paul@24 | 543 | Permit reading of form data using the form attributes or page |
paul@24 | 544 | permissions. |
paul@24 | 545 | """ |
paul@24 | 546 | |
paul@24 | 547 | return self.handler.checkPermissions("read") |
paul@24 | 548 | |
paul@47 | 549 | def append(self, item): |
paul@47 | 550 | |
paul@47 | 551 | "Append the given 'item' to the store." |
paul@47 | 552 | |
paul@47 | 553 | if self.storetype == "subpage": |
paul@47 | 554 | |
paul@47 | 555 | # Add an ACL to restrict direct access to subpages. |
paul@47 | 556 | |
paul@47 | 557 | request = self.page.request |
paul@48 | 558 | acl = self.handler.getSubpageACL() |
paul@47 | 559 | item = acl.getString() + item |
paul@47 | 560 | |
paul@49 | 561 | # Add a format header to the page for parsers to use. |
paul@49 | 562 | |
paul@49 | 563 | item = "#FORMAT formdata\n" + item |
paul@49 | 564 | |
paul@47 | 565 | ItemStoreBase.append(self, item) |
paul@47 | 566 | |
paul@47 | 567 | def __getitem__(self, number): |
paul@47 | 568 | |
paul@47 | 569 | "Return the item for the given 'number'." |
paul@47 | 570 | |
paul@47 | 571 | body = ItemStoreBase.__getitem__(self, number) |
paul@47 | 572 | |
paul@47 | 573 | if self.storetype == "subpage": |
paul@47 | 574 | |
paul@47 | 575 | # Remove any page directives. |
paul@47 | 576 | |
paul@47 | 577 | directives, body = wikiutil.get_processing_instructions(body) |
paul@47 | 578 | |
paul@47 | 579 | return body |
paul@47 | 580 | |
paul@5 | 581 | # Form and field information. |
paul@5 | 582 | |
paul@8 | 583 | def getFormStructure(text, request, path=None, structure=None): |
paul@8 | 584 | |
paul@8 | 585 | """ |
paul@8 | 586 | For the given form 'text' and using the 'request', return details of the |
paul@8 | 587 | form for the section at the given 'path' (or the entire form if 'path' is |
paul@8 | 588 | omitted), populating the given 'structure' (or populating a new structure if |
paul@8 | 589 | 'structure' is omitted). |
paul@8 | 590 | """ |
paul@8 | 591 | |
paul@8 | 592 | if structure is None: |
paul@8 | 593 | structure = {} |
paul@8 | 594 | |
paul@8 | 595 | for format, attributes, body in getFragments(text, True): |
paul@8 | 596 | |
paul@8 | 597 | # Get field details at the current level. |
paul@8 | 598 | |
paul@8 | 599 | if format is None: |
paul@8 | 600 | structure.update(getFormFields(body, path, request)) |
paul@8 | 601 | |
paul@8 | 602 | # Where a section is found, get details from within the section. |
paul@8 | 603 | |
paul@11 | 604 | elif format == "form": |
paul@11 | 605 | if attributes.has_key("section"): |
paul@11 | 606 | section_name = attributes["section"] |
paul@11 | 607 | section = structure[section_name] = {} |
paul@11 | 608 | getFormStructure(body, request, path and ("%s/%s" % (path, section_name)) or section_name, section) |
paul@11 | 609 | elif attributes.has_key("message"): |
paul@11 | 610 | getFormStructure(body, request, path, structure) |
paul@12 | 611 | elif attributes.has_key("not-message"): |
paul@12 | 612 | getFormStructure(body, request, path, structure) |
paul@8 | 613 | |
paul@8 | 614 | # Get field details from other kinds of region. |
paul@8 | 615 | |
paul@8 | 616 | elif format != "form": |
paul@8 | 617 | getFormStructure(body, request, path, structure) |
paul@8 | 618 | |
paul@8 | 619 | return structure |
paul@8 | 620 | |
paul@8 | 621 | def getFormForFragment(text, fragment=None): |
paul@8 | 622 | |
paul@8 | 623 | """ |
paul@8 | 624 | Return the form region from the given 'text' for the specified 'fragment'. |
paul@23 | 625 | If no fragment is specified, the first form region is returned. The form |
paul@23 | 626 | region is described using a tuple containing the attributes for the form |
paul@23 | 627 | and the body text of the form. |
paul@8 | 628 | """ |
paul@8 | 629 | |
paul@8 | 630 | for format, attributes, body in getFragments(text): |
paul@23 | 631 | if format == "form" and (not fragment or attributes.get("fragment") == fragment): |
paul@23 | 632 | return attributes, body |
paul@8 | 633 | |
paul@23 | 634 | return {}, None |
paul@8 | 635 | |
paul@5 | 636 | def getFieldArguments(field_definition): |
paul@5 | 637 | |
paul@5 | 638 | "Return the parsed arguments from the given 'field_definition' string." |
paul@5 | 639 | |
paul@5 | 640 | field_args = {} |
paul@5 | 641 | |
paul@5 | 642 | for field_arg in field_definition.split(): |
paul@8 | 643 | if field_arg == "required": |
paul@8 | 644 | field_args[field_arg] = True |
paul@8 | 645 | continue |
paul@5 | 646 | |
paul@5 | 647 | # Record the key-value details. |
paul@5 | 648 | |
paul@5 | 649 | try: |
paul@5 | 650 | argname, argvalue = field_arg.split("=", 1) |
paul@5 | 651 | field_args[argname] = argvalue |
paul@5 | 652 | |
paul@5 | 653 | # Single keywords are interpreted as type descriptions. |
paul@5 | 654 | |
paul@5 | 655 | except ValueError: |
paul@5 | 656 | if not field_args.has_key("type"): |
paul@5 | 657 | field_args["type"] = field_arg |
paul@5 | 658 | |
paul@5 | 659 | return field_args |
paul@5 | 660 | |
paul@0 | 661 | # Common formatting functions. |
paul@0 | 662 | |
paul@29 | 663 | def getFormOutput(text, fields, form_fragment=None, path=None, fragment=None, repeating=None, index=None): |
paul@0 | 664 | |
paul@0 | 665 | """ |
paul@0 | 666 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 667 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 668 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 669 | regions, and such sections may not define separate subregions but instead |
paul@0 | 670 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 671 | |
paul@0 | 672 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 673 | control whether sections are populated or not. |
paul@18 | 674 | |
paul@29 | 675 | The optional 'form_fragment' is used to indicate the form to which the |
paul@29 | 676 | fields belong. |
paul@29 | 677 | |
paul@18 | 678 | The optional 'path' is used to adjust form fields to refer to the correct |
paul@18 | 679 | part of the form hierarchy. |
paul@18 | 680 | |
paul@29 | 681 | The optional 'fragment' is used to indicate the form being output. If this |
paul@29 | 682 | value is different to 'form_fragment', the structure of the form should not |
paul@29 | 683 | be influenced by the 'fields'. |
paul@22 | 684 | |
paul@18 | 685 | The optional 'repeating' and 'index' is used to refer to individual values |
paul@18 | 686 | of a designated field. |
paul@0 | 687 | """ |
paul@0 | 688 | |
paul@29 | 689 | this_form = fragment and form_fragment == fragment or not fragment and not form_fragment |
paul@29 | 690 | |
paul@0 | 691 | output = [] |
paul@0 | 692 | section = fields |
paul@0 | 693 | |
paul@0 | 694 | for region in getRegions(text, True): |
paul@0 | 695 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 696 | |
paul@6 | 697 | # Adjust any FormField macros to use hierarchical names. |
paul@0 | 698 | |
paul@0 | 699 | if format is None: |
paul@22 | 700 | output.append((path or fragment or repeating) and |
paul@22 | 701 | adjustFormFields(body, path, fragment, repeating, index) or body) |
paul@0 | 702 | |
paul@0 | 703 | # Include form sections only if fields exist for those sections. |
paul@0 | 704 | |
paul@0 | 705 | elif format == "form": |
paul@0 | 706 | section_name = attributes.get("section") |
paul@10 | 707 | message_name = attributes.get("message") |
paul@12 | 708 | absent_message_name = attributes.get("not-message") |
paul@10 | 709 | |
paul@29 | 710 | # Ignore sections not related to the supplied field data. |
paul@29 | 711 | |
paul@29 | 712 | if not this_form: |
paul@29 | 713 | pass |
paul@29 | 714 | |
paul@10 | 715 | # Sections are groups of fields in their own namespace. |
paul@10 | 716 | |
paul@29 | 717 | elif section_name and section.has_key(section_name): |
paul@1 | 718 | |
paul@1 | 719 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 720 | |
paul@1 | 721 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@6 | 722 | element_ref = "%s$%s" % (section_name, index) |
paul@1 | 723 | |
paul@6 | 724 | # Get the output for the section. |
paul@6 | 725 | |
paul@29 | 726 | output.append(getFormOutput(body, element, form_fragment, |
paul@22 | 727 | path and ("%s/%s" % (path, element_ref)) or element_ref, fragment)) |
paul@0 | 728 | |
paul@10 | 729 | # Message regions are conditional on a particular field and |
paul@10 | 730 | # reference the current namespace. |
paul@10 | 731 | |
paul@10 | 732 | elif message_name and section.has_key(message_name): |
paul@18 | 733 | |
paul@18 | 734 | if attributes.get("repeating"): |
paul@18 | 735 | for index in range(0, len(section[message_name])): |
paul@29 | 736 | output.append(getFormOutput(body, section, form_fragment, path, fragment, message_name, index)) |
paul@18 | 737 | else: |
paul@29 | 738 | output.append(getFormOutput(body, section, form_fragment, path, fragment)) |
paul@10 | 739 | |
paul@12 | 740 | # Not-message regions are conditional on a particular field being |
paul@12 | 741 | # absent. They reference the current namespace. |
paul@12 | 742 | |
paul@12 | 743 | elif absent_message_name and not section.has_key(absent_message_name): |
paul@29 | 744 | output.append(getFormOutput(body, section, form_fragment, path, fragment)) |
paul@12 | 745 | |
paul@0 | 746 | # Inspect and include other regions. |
paul@0 | 747 | |
paul@0 | 748 | else: |
paul@0 | 749 | output.append(header) |
paul@29 | 750 | output.append(getFormOutput(body, section, form_fragment, path, fragment, repeating, index)) |
paul@0 | 751 | output.append(close) |
paul@0 | 752 | |
paul@0 | 753 | return "".join(output) |
paul@0 | 754 | |
paul@8 | 755 | def getFormFields(body, path, request): |
paul@8 | 756 | |
paul@8 | 757 | "Return a dictionary of fields from the given 'body' at the given 'path'." |
paul@8 | 758 | |
paul@8 | 759 | fields = {} |
paul@8 | 760 | cache = {} |
paul@8 | 761 | type = None |
paul@8 | 762 | |
paul@8 | 763 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@8 | 764 | state = i % 3 |
paul@8 | 765 | |
paul@8 | 766 | if state == 1: |
paul@8 | 767 | type = match |
paul@8 | 768 | elif state == 2 and type == "Field": |
paul@8 | 769 | args = {} |
paul@8 | 770 | |
paul@8 | 771 | # Obtain the macro arguments, adjusted to consider the path. |
paul@8 | 772 | |
paul@22 | 773 | name, path, dictpage, label, section, fragment = \ |
paul@8 | 774 | getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path)) |
paul@8 | 775 | |
paul@8 | 776 | # Obtain field information from the cache, if possible. |
paul@8 | 777 | |
paul@8 | 778 | cache_key = (name, dictpage) |
paul@8 | 779 | |
paul@8 | 780 | if cache.has_key(cache_key): |
paul@8 | 781 | field_args, allowed_values = cache[cache_key] |
paul@8 | 782 | |
paul@8 | 783 | # Otherwise, obtain field information from any WikiDict. |
paul@8 | 784 | |
paul@8 | 785 | else: |
paul@8 | 786 | field_args = {} |
paul@8 | 787 | allowed_values = None |
paul@8 | 788 | |
paul@8 | 789 | if dictpage: |
paul@8 | 790 | wikidict = getWikiDict(dictpage, request) |
paul@8 | 791 | if wikidict: |
paul@8 | 792 | field_definition = wikidict.get(name) |
paul@8 | 793 | if field_definition: |
paul@8 | 794 | field_args = getFieldArguments(field_definition) |
paul@8 | 795 | if field_args.has_key("source"): |
paul@8 | 796 | sourcedict = getWikiDict(field_args["source"], request) |
paul@8 | 797 | if sourcedict: |
paul@8 | 798 | allowed_values = sourcedict.keys() |
paul@8 | 799 | |
paul@8 | 800 | cache[cache_key] = field_args, allowed_values |
paul@8 | 801 | |
paul@8 | 802 | # Store the field information. |
paul@8 | 803 | |
paul@8 | 804 | fields[name] = path, dictpage, label, section, field_args, allowed_values |
paul@8 | 805 | |
paul@8 | 806 | return fields |
paul@8 | 807 | |
paul@22 | 808 | def adjustFormFields(body, path, fragment, repeating=None, index=None): |
paul@1 | 809 | |
paul@1 | 810 | """ |
paul@1 | 811 | Return a version of the 'body' with the names in FormField macros updated to |
paul@22 | 812 | incorporate the given 'path' and 'fragment'. If 'repeating' is specified, |
paul@22 | 813 | any field with such a name will be adjusted to reference the value with the |
paul@22 | 814 | given 'index'. |
paul@1 | 815 | """ |
paul@1 | 816 | |
paul@1 | 817 | result = [] |
paul@4 | 818 | type = None |
paul@1 | 819 | |
paul@7 | 820 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@7 | 821 | state = i % 3 |
paul@4 | 822 | |
paul@4 | 823 | # Reproduce normal text as is. |
paul@4 | 824 | |
paul@7 | 825 | if state == 0: |
paul@1 | 826 | result.append(match) |
paul@4 | 827 | |
paul@4 | 828 | # Capture the macro type. |
paul@4 | 829 | |
paul@7 | 830 | elif state == 1: |
paul@4 | 831 | type = match |
paul@4 | 832 | |
paul@4 | 833 | # Substitute the macro and modified arguments. |
paul@4 | 834 | |
paul@1 | 835 | else: |
paul@33 | 836 | result.append("<<Form%s(%s)>>" % (type, |
paul@33 | 837 | quoteMacroArguments( |
paul@33 | 838 | adjustMacroArguments( |
paul@33 | 839 | parseMacroArguments(match), path, fragment, repeating, index |
paul@33 | 840 | ) |
paul@33 | 841 | ) |
paul@33 | 842 | )) |
paul@1 | 843 | |
paul@1 | 844 | return "".join(result) |
paul@1 | 845 | |
paul@22 | 846 | def adjustMacroArguments(args, path, fragment=None, repeating=None, index=None): |
paul@0 | 847 | |
paul@0 | 848 | """ |
paul@1 | 849 | Adjust the given 'args' so that the path incorporates the given |
paul@22 | 850 | 'path' and 'fragment', returning a new list containing the revised path, |
paul@22 | 851 | fragment and remaining arguments. If 'repeating' is specified, any field |
paul@22 | 852 | with such a name will be adjusted to reference the value with the given |
paul@22 | 853 | 'index'. |
paul@1 | 854 | """ |
paul@1 | 855 | |
paul@22 | 856 | if not path and not fragment and not repeating: |
paul@8 | 857 | return args |
paul@8 | 858 | |
paul@1 | 859 | result = [] |
paul@6 | 860 | old_path = None |
paul@18 | 861 | found_name = None |
paul@1 | 862 | |
paul@33 | 863 | for name, value in args: |
paul@33 | 864 | if name == "path": |
paul@33 | 865 | old_path = value |
paul@33 | 866 | elif name == "fragment" and fragment: |
paul@22 | 867 | pass |
paul@1 | 868 | else: |
paul@33 | 869 | result.append((name, value)) |
paul@33 | 870 | |
paul@33 | 871 | # Remember any explicitly given name or where a keyword appears. |
paul@33 | 872 | |
paul@33 | 873 | if name == "name" or name is None and found_name is None: |
paul@33 | 874 | found_name = value |
paul@1 | 875 | |
paul@18 | 876 | if path: |
paul@18 | 877 | qualified = old_path and ("%s/%s" % (old_path, path)) or path |
paul@33 | 878 | result.append(("path", qualified)) |
paul@18 | 879 | |
paul@22 | 880 | if fragment: |
paul@33 | 881 | result.append(("fragment", fragment)) |
paul@22 | 882 | |
paul@18 | 883 | if repeating and repeating == found_name: |
paul@33 | 884 | result.append(("index", index)) |
paul@1 | 885 | |
paul@1 | 886 | return result |
paul@1 | 887 | |
paul@8 | 888 | def getMacroArguments(parsed_args): |
paul@8 | 889 | |
paul@8 | 890 | "Return the macro arguments decoded from 'parsed_args'." |
paul@8 | 891 | |
paul@33 | 892 | found_name = None |
paul@8 | 893 | path = None |
paul@8 | 894 | dictpage = None |
paul@8 | 895 | label = None |
paul@8 | 896 | section = None |
paul@22 | 897 | fragment = None |
paul@8 | 898 | |
paul@33 | 899 | for name, value in parsed_args: |
paul@33 | 900 | if name == "name": |
paul@33 | 901 | found_name = value |
paul@8 | 902 | |
paul@33 | 903 | elif name == "path": |
paul@33 | 904 | path = value |
paul@33 | 905 | |
paul@33 | 906 | elif name == "dict": |
paul@33 | 907 | dictpage = value |
paul@8 | 908 | |
paul@33 | 909 | elif name == "label": |
paul@33 | 910 | label = value |
paul@8 | 911 | |
paul@33 | 912 | elif name == "section": |
paul@33 | 913 | section = value |
paul@8 | 914 | |
paul@33 | 915 | elif name == "fragment": |
paul@33 | 916 | fragment = value |
paul@33 | 917 | |
paul@33 | 918 | # Keywords are interpreted as certain kinds of values. |
paul@22 | 919 | |
paul@8 | 920 | elif name is None: |
paul@33 | 921 | if found_name is None: |
paul@33 | 922 | found_name = value |
paul@8 | 923 | |
paul@33 | 924 | elif dictpage is None: |
paul@33 | 925 | dictpage = value |
paul@8 | 926 | |
paul@33 | 927 | return found_name, path, dictpage, label, section, fragment |
paul@8 | 928 | |
paul@1 | 929 | def getFields(d, remove=False): |
paul@1 | 930 | |
paul@1 | 931 | """ |
paul@1 | 932 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 933 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 934 | the fields from 'd'. |
paul@0 | 935 | """ |
paul@0 | 936 | |
paul@0 | 937 | fields = {} |
paul@0 | 938 | |
paul@0 | 939 | for key, value in d.items(): |
paul@0 | 940 | |
paul@1 | 941 | # Detect modifying fields. |
paul@1 | 942 | |
paul@1 | 943 | if key.find("=") != -1: |
paul@1 | 944 | fields[key] = value |
paul@1 | 945 | if remove: |
paul@1 | 946 | del d[key] |
paul@1 | 947 | continue |
paul@1 | 948 | |
paul@0 | 949 | # Reproduce the original hierarchy of the fields. |
paul@0 | 950 | |
paul@0 | 951 | section = fields |
paul@1 | 952 | parts = getPathDetails(key) |
paul@0 | 953 | |
paul@1 | 954 | for name, index in parts[:-1]: |
paul@1 | 955 | |
paul@1 | 956 | # Add an entry for instances of the section. |
paul@0 | 957 | |
paul@0 | 958 | if not section.has_key(name): |
paul@0 | 959 | section[name] = {} |
paul@0 | 960 | |
paul@1 | 961 | # Add an entry for the specific instance of the section. |
paul@1 | 962 | |
paul@0 | 963 | if not section[name].has_key(index): |
paul@0 | 964 | section[name][index] = {} |
paul@0 | 965 | |
paul@0 | 966 | section = section[name][index] |
paul@0 | 967 | |
paul@1 | 968 | section[parts[-1][0]] = value |
paul@1 | 969 | |
paul@1 | 970 | if remove: |
paul@1 | 971 | del d[key] |
paul@0 | 972 | |
paul@0 | 973 | return fields |
paul@0 | 974 | |
paul@1 | 975 | def getPathDetails(path): |
paul@4 | 976 | |
paul@4 | 977 | """ |
paul@4 | 978 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 979 | of section instances, with any specific field appearing as the last element |
paul@4 | 980 | and having the form (name, None). |
paul@4 | 981 | """ |
paul@4 | 982 | |
paul@1 | 983 | parts = [] |
paul@1 | 984 | |
paul@1 | 985 | for part in path.split("/"): |
paul@1 | 986 | try: |
paul@1 | 987 | name, index = part.split("$", 1) |
paul@1 | 988 | index = int(index) |
paul@1 | 989 | except ValueError: |
paul@1 | 990 | name, index = part, None |
paul@1 | 991 | |
paul@1 | 992 | parts.append((name, index)) |
paul@1 | 993 | |
paul@1 | 994 | return parts |
paul@1 | 995 | |
paul@1 | 996 | def getSectionForPath(path, fields): |
paul@1 | 997 | |
paul@1 | 998 | """ |
paul@1 | 999 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 1000 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 1001 | parent section contains the referenced section, where name is the name of |
paul@1 | 1002 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 1003 | specific section instance within the named section. |
paul@1 | 1004 | """ |
paul@1 | 1005 | |
paul@1 | 1006 | parts = getPathDetails(path) |
paul@1 | 1007 | section = fields |
paul@1 | 1008 | |
paul@1 | 1009 | for name, index in parts[:-1]: |
paul@1 | 1010 | section = fields[name][index] |
paul@1 | 1011 | |
paul@1 | 1012 | return section, parts[-1] |
paul@1 | 1013 | |
paul@1 | 1014 | def getSectionElements(section_elements): |
paul@1 | 1015 | |
paul@1 | 1016 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 1017 | |
paul@1 | 1018 | keys = map(int, section_elements.keys()) |
paul@1 | 1019 | keys.sort() |
paul@1 | 1020 | |
paul@1 | 1021 | elements = [] |
paul@1 | 1022 | |
paul@1 | 1023 | for key in keys: |
paul@1 | 1024 | elements.append(section_elements[key]) |
paul@1 | 1025 | |
paul@1 | 1026 | return elements |
paul@1 | 1027 | |
paul@9 | 1028 | # Parser-related formatting functions. |
paul@9 | 1029 | |
paul@9 | 1030 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@9 | 1031 | |
paul@9 | 1032 | """ |
paul@9 | 1033 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@9 | 1034 | The optional 'attrs' can be used to control the presentation of the form. |
paul@9 | 1035 | |
paul@9 | 1036 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@9 | 1037 | write output using the request. |
paul@9 | 1038 | """ |
paul@9 | 1039 | |
paul@9 | 1040 | write = write or request.write |
paul@9 | 1041 | page = request.page |
paul@9 | 1042 | |
paul@29 | 1043 | form = get_form(request) |
paul@29 | 1044 | form_fragment = form.get("fragment", [None])[0] |
paul@29 | 1045 | fields = getFields(form) |
paul@9 | 1046 | |
paul@26 | 1047 | # Prepare the query string for the form action URL. |
paul@26 | 1048 | |
paul@9 | 1049 | queryparams = [] |
paul@9 | 1050 | |
paul@16 | 1051 | for argname, default in [("fragment", None), ("action", "MoinFormHandler")]: |
paul@9 | 1052 | if attrs and attrs.has_key(argname): |
paul@9 | 1053 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@14 | 1054 | elif default: |
paul@14 | 1055 | queryparams.append("%s=%s" % (argname, default)) |
paul@9 | 1056 | |
paul@9 | 1057 | querystr = "&".join(queryparams) |
paul@17 | 1058 | fragment = attrs.get("fragment") |
paul@9 | 1059 | |
paul@17 | 1060 | write(fmt.rawHTML('<form method="post" action="%s%s"%s>' % ( |
paul@14 | 1061 | escattr(page.url(request, querystr)), |
paul@17 | 1062 | fragment and ("#%s" % escattr(fragment)) or "", |
paul@17 | 1063 | fragment and (' id="%s"' % escattr(fragment)) or "" |
paul@14 | 1064 | ))) |
paul@9 | 1065 | |
paul@9 | 1066 | # Obtain page text for the form, incorporating subregions and applicable |
paul@9 | 1067 | # sections. |
paul@9 | 1068 | |
paul@29 | 1069 | output = getFormOutput(text, fields, form_fragment=form_fragment, fragment=fragment) |
paul@9 | 1070 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@9 | 1071 | |
paul@9 | 1072 | write(fmt.rawHTML('</form>')) |
paul@9 | 1073 | |
paul@0 | 1074 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 1075 | |
paul@0 | 1076 | """ |
paul@0 | 1077 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 1078 | 'mimetype'. |
paul@0 | 1079 | |
paul@0 | 1080 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 1081 | |
paul@0 | 1082 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 1083 | write output using the request. |
paul@0 | 1084 | """ |
paul@0 | 1085 | |
paul@0 | 1086 | write = write or request.write |
paul@0 | 1087 | |
paul@0 | 1088 | if mimetype == "text/html": |
paul@0 | 1089 | write('<html>') |
paul@0 | 1090 | write('<body>') |
paul@0 | 1091 | fmt = request.html_formatter |
paul@0 | 1092 | fmt.setPage(request.page) |
paul@0 | 1093 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 1094 | write('</body>') |
paul@0 | 1095 | write('</html>') |
paul@0 | 1096 | |
paul@0 | 1097 | # vim: tabstop=4 expandtab shiftwidth=4 |