paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinForms library |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 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@3 | 9 | from MoinMoin.action import do_show |
paul@8 | 10 | from MoinMoin.Page import Page |
paul@0 | 11 | from MoinMoin import wikiutil |
paul@0 | 12 | from MoinSupport import * |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@0 | 15 | __version__ = "0.1" |
paul@0 | 16 | |
paul@4 | 17 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 18 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 19 | |
paul@3 | 20 | # Common action functionality. |
paul@3 | 21 | |
paul@3 | 22 | class MoinFormHandlerAction: |
paul@3 | 23 | |
paul@3 | 24 | "A handler action that can be specialised for individual forms." |
paul@3 | 25 | |
paul@3 | 26 | def __init__(self, pagename, request): |
paul@3 | 27 | self.pagename = pagename |
paul@3 | 28 | self.request = request |
paul@3 | 29 | |
paul@3 | 30 | def processForm(self): |
paul@3 | 31 | |
paul@3 | 32 | """ |
paul@3 | 33 | Interpret the request details and modify them according to the structure |
paul@3 | 34 | of the interpreted information. |
paul@3 | 35 | """ |
paul@3 | 36 | |
paul@13 | 37 | _ = self.request.getText |
paul@13 | 38 | |
paul@3 | 39 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 40 | |
paul@3 | 41 | form = get_form(self.request) |
paul@3 | 42 | fields = getFields(form, remove=True) |
paul@3 | 43 | |
paul@8 | 44 | # Modify and validate the form. |
paul@3 | 45 | |
paul@3 | 46 | self.modifyFields(fields) |
paul@8 | 47 | |
paul@13 | 48 | # Get the form definition. |
paul@13 | 49 | |
paul@17 | 50 | structure = self.getFormStructure(fields) |
paul@13 | 51 | |
paul@13 | 52 | # Without any form definition, the page is probably the wrong one. |
paul@13 | 53 | |
paul@13 | 54 | if not structure: |
paul@13 | 55 | self.request.theme.add_msg(_("This page does not provide a form."), "error") |
paul@13 | 56 | do_show(self.pagename, self.request) |
paul@13 | 57 | return |
paul@13 | 58 | |
paul@13 | 59 | # With a form definition, attempt to validate the fields. |
paul@13 | 60 | |
paul@13 | 61 | if self.validateFields(fields, structure): |
paul@8 | 62 | self.finished(fields, form) |
paul@8 | 63 | else: |
paul@8 | 64 | self.unfinished(fields, form) |
paul@8 | 65 | |
paul@8 | 66 | def finished(self, fields, form): |
paul@8 | 67 | |
paul@8 | 68 | "Handle the finished 'fields' and 'form'." |
paul@8 | 69 | |
paul@8 | 70 | self.unfinished(fields, form) |
paul@8 | 71 | |
paul@8 | 72 | def unfinished(self, fields, form): |
paul@8 | 73 | |
paul@8 | 74 | "Handle the unfinished 'fields' and 'form'." |
paul@8 | 75 | |
paul@8 | 76 | # Serialise and show the form. |
paul@8 | 77 | |
paul@3 | 78 | self.serialiseFields(fields, form) |
paul@3 | 79 | do_show(self.pagename, self.request) |
paul@3 | 80 | |
paul@17 | 81 | def getFormStructure(self, fields): |
paul@8 | 82 | |
paul@13 | 83 | "Return the structure of the form being handled." |
paul@8 | 84 | |
paul@8 | 85 | text = Page(self.request, self.pagename).get_raw_body() |
paul@17 | 86 | text = getFormForFragment(text, fields.get("fragment", [None])[0]) |
paul@13 | 87 | return getFormStructure(text, self.request) |
paul@13 | 88 | |
paul@13 | 89 | def validateFields(self, fields, structure): |
paul@13 | 90 | |
paul@13 | 91 | """ |
paul@13 | 92 | Validate the given 'fields' using the given form 'structure', |
paul@13 | 93 | introducing error fields where the individual fields do not conform to |
paul@13 | 94 | their descriptions. |
paul@13 | 95 | """ |
paul@8 | 96 | |
paul@8 | 97 | return self.validateFieldsUsingStructure(fields, structure) |
paul@8 | 98 | |
paul@8 | 99 | def validateFieldsUsingStructure(self, fields, structure): |
paul@8 | 100 | |
paul@8 | 101 | "Validate the given 'fields' using the given 'structure'." |
paul@8 | 102 | |
paul@8 | 103 | _ = self.request.getText |
paul@8 | 104 | valid = True |
paul@8 | 105 | |
paul@8 | 106 | for key, definition in structure.items(): |
paul@8 | 107 | value = fields.get(key) |
paul@8 | 108 | |
paul@8 | 109 | # Enter form sections and validate them. |
paul@8 | 110 | |
paul@8 | 111 | if isinstance(definition, dict): |
paul@8 | 112 | if value: |
paul@8 | 113 | for element in getSectionElements(value): |
paul@11 | 114 | valid = self.validateFieldsUsingStructure(element, structure[key]) and valid |
paul@8 | 115 | |
paul@8 | 116 | # Validate individual fields. |
paul@8 | 117 | |
paul@8 | 118 | elif structure.has_key(key): |
paul@8 | 119 | path, dictpage, label, section, field_args, allowed_values = definition |
paul@8 | 120 | errors = [] |
paul@8 | 121 | |
paul@8 | 122 | # Test for obligatory values. |
paul@8 | 123 | |
paul@10 | 124 | if not value or not value[0]: |
paul@8 | 125 | if field_args.get("required"): |
paul@11 | 126 | |
paul@11 | 127 | # Detect new parts of the structure and avoid producing |
paul@11 | 128 | # premature error messages. |
paul@11 | 129 | |
paul@11 | 130 | if not fields.has_key("_new"): |
paul@11 | 131 | errors.append(_("This field must be filled out.")) |
paul@11 | 132 | else: |
paul@11 | 133 | valid = False |
paul@8 | 134 | else: |
paul@8 | 135 | # Test for unacceptable values. |
paul@8 | 136 | |
paul@8 | 137 | if allowed_values and set(value).difference(allowed_values): |
paul@8 | 138 | errors.append(_("At least one of the choices is not acceptable.")) |
paul@8 | 139 | |
paul@8 | 140 | # Test the number of values. |
paul@8 | 141 | |
paul@8 | 142 | if field_args.get("type") == "select": |
paul@8 | 143 | if field_args.has_key("maxselected"): |
paul@8 | 144 | if len(value) > int(field_args["maxselected"]): |
paul@8 | 145 | errors.append(_("Incorrect number of choices given: need %s.") % field_args["maxselected"]) |
paul@8 | 146 | |
paul@8 | 147 | if errors: |
paul@8 | 148 | fields["%s-error" % key] = errors |
paul@8 | 149 | valid = False |
paul@8 | 150 | |
paul@8 | 151 | return valid |
paul@4 | 152 | |
paul@3 | 153 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 154 | |
paul@3 | 155 | """ |
paul@3 | 156 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 157 | to name the entries. |
paul@3 | 158 | """ |
paul@3 | 159 | |
paul@3 | 160 | for key, value in fields.items(): |
paul@3 | 161 | |
paul@3 | 162 | # Serialise sections. |
paul@3 | 163 | |
paul@3 | 164 | if isinstance(value, dict): |
paul@3 | 165 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 166 | element_ref = "%s$%s" % (key, index) |
paul@3 | 167 | |
paul@3 | 168 | self.serialiseFields(element, form, |
paul@3 | 169 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 170 | ) |
paul@3 | 171 | |
paul@3 | 172 | # Serialise fields. |
paul@3 | 173 | |
paul@3 | 174 | else: |
paul@3 | 175 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 176 | |
paul@3 | 177 | def modifyFields(self, fields): |
paul@3 | 178 | |
paul@3 | 179 | "Modify the given 'fields', removing and adding items." |
paul@3 | 180 | |
paul@3 | 181 | # First, remove fields. |
paul@3 | 182 | |
paul@3 | 183 | for key in fields.keys(): |
paul@3 | 184 | if key.startswith("_remove="): |
paul@3 | 185 | self.removeField(key[8:], fields) |
paul@3 | 186 | |
paul@3 | 187 | # Then, add fields. |
paul@3 | 188 | |
paul@3 | 189 | for key in fields.keys(): |
paul@3 | 190 | if key.startswith("_add="): |
paul@3 | 191 | self.addField(key[5:], fields) |
paul@3 | 192 | |
paul@3 | 193 | def removeField(self, path, fields): |
paul@3 | 194 | |
paul@3 | 195 | """ |
paul@3 | 196 | Remove the section element indicated by the given 'path' from the |
paul@3 | 197 | 'fields'. |
paul@3 | 198 | """ |
paul@3 | 199 | |
paul@3 | 200 | section, (name, index) = getSectionForPath(path, fields) |
paul@17 | 201 | try: |
paul@17 | 202 | del section[name][index] |
paul@17 | 203 | except KeyError: |
paul@17 | 204 | pass |
paul@3 | 205 | |
paul@3 | 206 | def addField(self, path, fields): |
paul@3 | 207 | |
paul@3 | 208 | """ |
paul@3 | 209 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 210 | """ |
paul@3 | 211 | |
paul@3 | 212 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 213 | placeholder = {"_new" : ""} |
paul@3 | 214 | |
paul@3 | 215 | if section.has_key(name): |
paul@3 | 216 | indexes = section[name].keys() |
paul@3 | 217 | max_index = max(map(int, indexes)) |
paul@3 | 218 | section[name][max_index + 1] = placeholder |
paul@3 | 219 | else: |
paul@3 | 220 | max_index = -1 |
paul@3 | 221 | section[name] = {0 : placeholder} |
paul@3 | 222 | |
paul@5 | 223 | # Form and field information. |
paul@5 | 224 | |
paul@8 | 225 | def getFormStructure(text, request, path=None, structure=None): |
paul@8 | 226 | |
paul@8 | 227 | """ |
paul@8 | 228 | For the given form 'text' and using the 'request', return details of the |
paul@8 | 229 | form for the section at the given 'path' (or the entire form if 'path' is |
paul@8 | 230 | omitted), populating the given 'structure' (or populating a new structure if |
paul@8 | 231 | 'structure' is omitted). |
paul@8 | 232 | """ |
paul@8 | 233 | |
paul@8 | 234 | if structure is None: |
paul@8 | 235 | structure = {} |
paul@8 | 236 | |
paul@8 | 237 | for format, attributes, body in getFragments(text, True): |
paul@8 | 238 | |
paul@8 | 239 | # Get field details at the current level. |
paul@8 | 240 | |
paul@8 | 241 | if format is None: |
paul@8 | 242 | structure.update(getFormFields(body, path, request)) |
paul@8 | 243 | |
paul@8 | 244 | # Where a section is found, get details from within the section. |
paul@8 | 245 | |
paul@11 | 246 | elif format == "form": |
paul@11 | 247 | if attributes.has_key("section"): |
paul@11 | 248 | section_name = attributes["section"] |
paul@11 | 249 | section = structure[section_name] = {} |
paul@11 | 250 | getFormStructure(body, request, path and ("%s/%s" % (path, section_name)) or section_name, section) |
paul@11 | 251 | elif attributes.has_key("message"): |
paul@11 | 252 | getFormStructure(body, request, path, structure) |
paul@12 | 253 | elif attributes.has_key("not-message"): |
paul@12 | 254 | getFormStructure(body, request, path, structure) |
paul@8 | 255 | |
paul@8 | 256 | # Get field details from other kinds of region. |
paul@8 | 257 | |
paul@8 | 258 | elif format != "form": |
paul@8 | 259 | getFormStructure(body, request, path, structure) |
paul@8 | 260 | |
paul@8 | 261 | return structure |
paul@8 | 262 | |
paul@8 | 263 | def getFormForFragment(text, fragment=None): |
paul@8 | 264 | |
paul@8 | 265 | """ |
paul@8 | 266 | Return the form region from the given 'text' for the specified 'fragment'. |
paul@8 | 267 | If no fragment is specified, the first form region is returned. |
paul@8 | 268 | """ |
paul@8 | 269 | |
paul@8 | 270 | for format, attributes, body in getFragments(text): |
paul@8 | 271 | if not fragment or attributes.get("fragment") == fragment: |
paul@8 | 272 | return body |
paul@8 | 273 | |
paul@8 | 274 | return None |
paul@8 | 275 | |
paul@5 | 276 | def getFieldArguments(field_definition): |
paul@5 | 277 | |
paul@5 | 278 | "Return the parsed arguments from the given 'field_definition' string." |
paul@5 | 279 | |
paul@5 | 280 | field_args = {} |
paul@5 | 281 | |
paul@5 | 282 | for field_arg in field_definition.split(): |
paul@8 | 283 | if field_arg == "required": |
paul@8 | 284 | field_args[field_arg] = True |
paul@8 | 285 | continue |
paul@5 | 286 | |
paul@5 | 287 | # Record the key-value details. |
paul@5 | 288 | |
paul@5 | 289 | try: |
paul@5 | 290 | argname, argvalue = field_arg.split("=", 1) |
paul@5 | 291 | field_args[argname] = argvalue |
paul@5 | 292 | |
paul@5 | 293 | # Single keywords are interpreted as type descriptions. |
paul@5 | 294 | |
paul@5 | 295 | except ValueError: |
paul@5 | 296 | if not field_args.has_key("type"): |
paul@5 | 297 | field_args["type"] = field_arg |
paul@5 | 298 | |
paul@5 | 299 | return field_args |
paul@5 | 300 | |
paul@0 | 301 | # Common formatting functions. |
paul@0 | 302 | |
paul@6 | 303 | def getFormOutput(text, fields, path=None): |
paul@0 | 304 | |
paul@0 | 305 | """ |
paul@0 | 306 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 307 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 308 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 309 | regions, and such sections may not define separate subregions but instead |
paul@0 | 310 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 311 | |
paul@0 | 312 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 313 | control whether sections are populated or not. |
paul@0 | 314 | """ |
paul@0 | 315 | |
paul@0 | 316 | output = [] |
paul@0 | 317 | section = fields |
paul@0 | 318 | |
paul@0 | 319 | for region in getRegions(text, True): |
paul@0 | 320 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 321 | |
paul@6 | 322 | # Adjust any FormField macros to use hierarchical names. |
paul@0 | 323 | |
paul@0 | 324 | if format is None: |
paul@8 | 325 | output.append(path and adjustFormFields(body, path) or body) |
paul@0 | 326 | |
paul@0 | 327 | # Include form sections only if fields exist for those sections. |
paul@0 | 328 | |
paul@0 | 329 | elif format == "form": |
paul@0 | 330 | section_name = attributes.get("section") |
paul@10 | 331 | message_name = attributes.get("message") |
paul@12 | 332 | absent_message_name = attributes.get("not-message") |
paul@10 | 333 | |
paul@10 | 334 | # Sections are groups of fields in their own namespace. |
paul@10 | 335 | |
paul@0 | 336 | if section_name and section.has_key(section_name): |
paul@1 | 337 | |
paul@1 | 338 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 339 | |
paul@1 | 340 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@6 | 341 | element_ref = "%s$%s" % (section_name, index) |
paul@1 | 342 | |
paul@6 | 343 | # Get the output for the section. |
paul@6 | 344 | |
paul@6 | 345 | output.append(getFormOutput(body, element, |
paul@6 | 346 | path and ("%s/%s" % (path, element_ref)) or element_ref)) |
paul@0 | 347 | |
paul@10 | 348 | # Message regions are conditional on a particular field and |
paul@10 | 349 | # reference the current namespace. |
paul@10 | 350 | |
paul@10 | 351 | elif message_name and section.has_key(message_name): |
paul@10 | 352 | output.append(getFormOutput(body, section, path)) |
paul@10 | 353 | |
paul@12 | 354 | # Not-message regions are conditional on a particular field being |
paul@12 | 355 | # absent. They reference the current namespace. |
paul@12 | 356 | |
paul@12 | 357 | elif absent_message_name and not section.has_key(absent_message_name): |
paul@12 | 358 | output.append(getFormOutput(body, section, path)) |
paul@12 | 359 | |
paul@0 | 360 | # Inspect and include other regions. |
paul@0 | 361 | |
paul@0 | 362 | else: |
paul@0 | 363 | output.append(header) |
paul@6 | 364 | output.append(getFormOutput(body, section, path)) |
paul@0 | 365 | output.append(close) |
paul@0 | 366 | |
paul@0 | 367 | return "".join(output) |
paul@0 | 368 | |
paul@8 | 369 | def getFormFields(body, path, request): |
paul@8 | 370 | |
paul@8 | 371 | "Return a dictionary of fields from the given 'body' at the given 'path'." |
paul@8 | 372 | |
paul@8 | 373 | fields = {} |
paul@8 | 374 | cache = {} |
paul@8 | 375 | type = None |
paul@8 | 376 | |
paul@8 | 377 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@8 | 378 | state = i % 3 |
paul@8 | 379 | |
paul@8 | 380 | if state == 1: |
paul@8 | 381 | type = match |
paul@8 | 382 | elif state == 2 and type == "Field": |
paul@8 | 383 | args = {} |
paul@8 | 384 | |
paul@8 | 385 | # Obtain the macro arguments, adjusted to consider the path. |
paul@8 | 386 | |
paul@8 | 387 | name, path, dictpage, label, section = \ |
paul@8 | 388 | getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path)) |
paul@8 | 389 | |
paul@8 | 390 | # Obtain field information from the cache, if possible. |
paul@8 | 391 | |
paul@8 | 392 | cache_key = (name, dictpage) |
paul@8 | 393 | |
paul@8 | 394 | if cache.has_key(cache_key): |
paul@8 | 395 | field_args, allowed_values = cache[cache_key] |
paul@8 | 396 | |
paul@8 | 397 | # Otherwise, obtain field information from any WikiDict. |
paul@8 | 398 | |
paul@8 | 399 | else: |
paul@8 | 400 | field_args = {} |
paul@8 | 401 | allowed_values = None |
paul@8 | 402 | |
paul@8 | 403 | if dictpage: |
paul@8 | 404 | wikidict = getWikiDict(dictpage, request) |
paul@8 | 405 | if wikidict: |
paul@8 | 406 | field_definition = wikidict.get(name) |
paul@8 | 407 | if field_definition: |
paul@8 | 408 | field_args = getFieldArguments(field_definition) |
paul@8 | 409 | if field_args.has_key("source"): |
paul@8 | 410 | sourcedict = getWikiDict(field_args["source"], request) |
paul@8 | 411 | if sourcedict: |
paul@8 | 412 | allowed_values = sourcedict.keys() |
paul@8 | 413 | |
paul@8 | 414 | cache[cache_key] = field_args, allowed_values |
paul@8 | 415 | |
paul@8 | 416 | # Store the field information. |
paul@8 | 417 | |
paul@8 | 418 | fields[name] = path, dictpage, label, section, field_args, allowed_values |
paul@8 | 419 | |
paul@8 | 420 | return fields |
paul@8 | 421 | |
paul@6 | 422 | def adjustFormFields(body, path): |
paul@1 | 423 | |
paul@1 | 424 | """ |
paul@1 | 425 | Return a version of the 'body' with the names in FormField macros updated to |
paul@6 | 426 | incorporate the given 'path'. |
paul@1 | 427 | """ |
paul@1 | 428 | |
paul@1 | 429 | result = [] |
paul@4 | 430 | type = None |
paul@1 | 431 | |
paul@7 | 432 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@7 | 433 | state = i % 3 |
paul@4 | 434 | |
paul@4 | 435 | # Reproduce normal text as is. |
paul@4 | 436 | |
paul@7 | 437 | if state == 0: |
paul@1 | 438 | result.append(match) |
paul@4 | 439 | |
paul@4 | 440 | # Capture the macro type. |
paul@4 | 441 | |
paul@7 | 442 | elif state == 1: |
paul@4 | 443 | type = match |
paul@4 | 444 | |
paul@4 | 445 | # Substitute the macro and modified arguments. |
paul@4 | 446 | |
paul@1 | 447 | else: |
paul@4 | 448 | result.append("<<Form%s(%s)>>" % (type, ",".join( |
paul@6 | 449 | adjustMacroArguments(parseMacroArguments(match), path) |
paul@4 | 450 | ))) |
paul@1 | 451 | |
paul@1 | 452 | return "".join(result) |
paul@1 | 453 | |
paul@6 | 454 | def adjustMacroArguments(args, path): |
paul@0 | 455 | |
paul@0 | 456 | """ |
paul@1 | 457 | Adjust the given 'args' so that the path incorporates the given |
paul@6 | 458 | 'path', returning a new list containing the revised path and remaining |
paul@6 | 459 | arguments. |
paul@1 | 460 | """ |
paul@1 | 461 | |
paul@8 | 462 | if not path: |
paul@8 | 463 | return args |
paul@8 | 464 | |
paul@1 | 465 | result = [] |
paul@6 | 466 | old_path = None |
paul@1 | 467 | |
paul@1 | 468 | for arg in args: |
paul@1 | 469 | if arg.startswith("path="): |
paul@6 | 470 | old_path = arg[5:] |
paul@1 | 471 | else: |
paul@1 | 472 | result.append(arg) |
paul@1 | 473 | |
paul@6 | 474 | qualified = old_path and ("%s/%s" % (old_path, path)) or path |
paul@1 | 475 | result.append("path=%s" % qualified) |
paul@1 | 476 | |
paul@1 | 477 | return result |
paul@1 | 478 | |
paul@1 | 479 | def parseMacroArguments(args): |
paul@1 | 480 | |
paul@1 | 481 | """ |
paul@1 | 482 | Interpret the arguments. |
paul@1 | 483 | NOTE: The argument parsing should really be more powerful in order to |
paul@1 | 484 | NOTE: support labels. |
paul@1 | 485 | """ |
paul@1 | 486 | |
paul@1 | 487 | try: |
paul@1 | 488 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 489 | except AttributeError: |
paul@1 | 490 | parsed_args = args.split(",") |
paul@1 | 491 | |
paul@1 | 492 | return [arg for arg in parsed_args if arg] |
paul@1 | 493 | |
paul@8 | 494 | def getMacroArguments(parsed_args): |
paul@8 | 495 | |
paul@8 | 496 | "Return the macro arguments decoded from 'parsed_args'." |
paul@8 | 497 | |
paul@8 | 498 | name = None |
paul@8 | 499 | path = None |
paul@8 | 500 | dictpage = None |
paul@8 | 501 | label = None |
paul@8 | 502 | section = None |
paul@8 | 503 | |
paul@8 | 504 | for arg in parsed_args: |
paul@8 | 505 | if arg.startswith("name="): |
paul@8 | 506 | name = arg[5:] |
paul@8 | 507 | |
paul@8 | 508 | elif arg.startswith("path="): |
paul@8 | 509 | path = arg[5:] |
paul@8 | 510 | |
paul@8 | 511 | elif arg.startswith("dict="): |
paul@8 | 512 | dictpage = arg[5:] |
paul@8 | 513 | |
paul@8 | 514 | elif arg.startswith("label="): |
paul@8 | 515 | label = arg[6:] |
paul@8 | 516 | |
paul@8 | 517 | elif arg.startswith("section="): |
paul@8 | 518 | section = arg[8:] |
paul@8 | 519 | |
paul@8 | 520 | elif name is None: |
paul@8 | 521 | name = arg |
paul@8 | 522 | |
paul@8 | 523 | elif dictpage is None: |
paul@8 | 524 | dictpage = arg |
paul@8 | 525 | |
paul@8 | 526 | return name, path, dictpage, label, section |
paul@8 | 527 | |
paul@1 | 528 | def getFields(d, remove=False): |
paul@1 | 529 | |
paul@1 | 530 | """ |
paul@1 | 531 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 532 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 533 | the fields from 'd'. |
paul@0 | 534 | """ |
paul@0 | 535 | |
paul@0 | 536 | fields = {} |
paul@0 | 537 | |
paul@0 | 538 | for key, value in d.items(): |
paul@0 | 539 | |
paul@1 | 540 | # Detect modifying fields. |
paul@1 | 541 | |
paul@1 | 542 | if key.find("=") != -1: |
paul@1 | 543 | fields[key] = value |
paul@1 | 544 | if remove: |
paul@1 | 545 | del d[key] |
paul@1 | 546 | continue |
paul@1 | 547 | |
paul@0 | 548 | # Reproduce the original hierarchy of the fields. |
paul@0 | 549 | |
paul@0 | 550 | section = fields |
paul@1 | 551 | parts = getPathDetails(key) |
paul@0 | 552 | |
paul@1 | 553 | for name, index in parts[:-1]: |
paul@1 | 554 | |
paul@1 | 555 | # Add an entry for instances of the section. |
paul@0 | 556 | |
paul@0 | 557 | if not section.has_key(name): |
paul@0 | 558 | section[name] = {} |
paul@0 | 559 | |
paul@1 | 560 | # Add an entry for the specific instance of the section. |
paul@1 | 561 | |
paul@0 | 562 | if not section[name].has_key(index): |
paul@0 | 563 | section[name][index] = {} |
paul@0 | 564 | |
paul@0 | 565 | section = section[name][index] |
paul@0 | 566 | |
paul@1 | 567 | section[parts[-1][0]] = value |
paul@1 | 568 | |
paul@1 | 569 | if remove: |
paul@1 | 570 | del d[key] |
paul@0 | 571 | |
paul@0 | 572 | return fields |
paul@0 | 573 | |
paul@1 | 574 | def getPathDetails(path): |
paul@4 | 575 | |
paul@4 | 576 | """ |
paul@4 | 577 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 578 | of section instances, with any specific field appearing as the last element |
paul@4 | 579 | and having the form (name, None). |
paul@4 | 580 | """ |
paul@4 | 581 | |
paul@1 | 582 | parts = [] |
paul@1 | 583 | |
paul@1 | 584 | for part in path.split("/"): |
paul@1 | 585 | try: |
paul@1 | 586 | name, index = part.split("$", 1) |
paul@1 | 587 | index = int(index) |
paul@1 | 588 | except ValueError: |
paul@1 | 589 | name, index = part, None |
paul@1 | 590 | |
paul@1 | 591 | parts.append((name, index)) |
paul@1 | 592 | |
paul@1 | 593 | return parts |
paul@1 | 594 | |
paul@1 | 595 | def getSectionForPath(path, fields): |
paul@1 | 596 | |
paul@1 | 597 | """ |
paul@1 | 598 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 599 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 600 | parent section contains the referenced section, where name is the name of |
paul@1 | 601 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 602 | specific section instance within the named section. |
paul@1 | 603 | """ |
paul@1 | 604 | |
paul@1 | 605 | parts = getPathDetails(path) |
paul@1 | 606 | section = fields |
paul@1 | 607 | |
paul@1 | 608 | for name, index in parts[:-1]: |
paul@1 | 609 | section = fields[name][index] |
paul@1 | 610 | |
paul@1 | 611 | return section, parts[-1] |
paul@1 | 612 | |
paul@1 | 613 | def getSectionElements(section_elements): |
paul@1 | 614 | |
paul@1 | 615 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 616 | |
paul@1 | 617 | keys = map(int, section_elements.keys()) |
paul@1 | 618 | keys.sort() |
paul@1 | 619 | |
paul@1 | 620 | elements = [] |
paul@1 | 621 | |
paul@1 | 622 | for key in keys: |
paul@1 | 623 | elements.append(section_elements[key]) |
paul@1 | 624 | |
paul@1 | 625 | return elements |
paul@1 | 626 | |
paul@9 | 627 | # Parser-related formatting functions. |
paul@9 | 628 | |
paul@9 | 629 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@9 | 630 | |
paul@9 | 631 | """ |
paul@9 | 632 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@9 | 633 | The optional 'attrs' can be used to control the presentation of the form. |
paul@9 | 634 | |
paul@9 | 635 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@9 | 636 | write output using the request. |
paul@9 | 637 | """ |
paul@9 | 638 | |
paul@9 | 639 | write = write or request.write |
paul@9 | 640 | page = request.page |
paul@9 | 641 | |
paul@9 | 642 | fields = getFields(get_form(request)) |
paul@9 | 643 | |
paul@9 | 644 | queryparams = [] |
paul@9 | 645 | |
paul@16 | 646 | for argname, default in [("fragment", None), ("action", "MoinFormHandler")]: |
paul@9 | 647 | if attrs and attrs.has_key(argname): |
paul@9 | 648 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@14 | 649 | elif default: |
paul@14 | 650 | queryparams.append("%s=%s" % (argname, default)) |
paul@9 | 651 | |
paul@9 | 652 | querystr = "&".join(queryparams) |
paul@17 | 653 | fragment = attrs.get("fragment") |
paul@9 | 654 | |
paul@17 | 655 | write(fmt.rawHTML('<form method="post" action="%s%s"%s>' % ( |
paul@14 | 656 | escattr(page.url(request, querystr)), |
paul@17 | 657 | fragment and ("#%s" % escattr(fragment)) or "", |
paul@17 | 658 | fragment and (' id="%s"' % escattr(fragment)) or "" |
paul@14 | 659 | ))) |
paul@9 | 660 | |
paul@9 | 661 | # Obtain page text for the form, incorporating subregions and applicable |
paul@9 | 662 | # sections. |
paul@9 | 663 | |
paul@9 | 664 | output = getFormOutput(text, fields) |
paul@9 | 665 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@9 | 666 | |
paul@9 | 667 | write(fmt.rawHTML('</form>')) |
paul@9 | 668 | |
paul@0 | 669 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 670 | |
paul@0 | 671 | """ |
paul@0 | 672 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 673 | 'mimetype'. |
paul@0 | 674 | |
paul@0 | 675 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 676 | |
paul@0 | 677 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 678 | write output using the request. |
paul@0 | 679 | """ |
paul@0 | 680 | |
paul@0 | 681 | write = write or request.write |
paul@0 | 682 | |
paul@0 | 683 | if mimetype == "text/html": |
paul@0 | 684 | write('<html>') |
paul@0 | 685 | write('<body>') |
paul@0 | 686 | fmt = request.html_formatter |
paul@0 | 687 | fmt.setPage(request.page) |
paul@0 | 688 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 689 | write('</body>') |
paul@0 | 690 | write('</html>') |
paul@0 | 691 | |
paul@0 | 692 | # vim: tabstop=4 expandtab shiftwidth=4 |