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@0 | 10 | from MoinMoin import wikiutil |
paul@0 | 11 | from MoinSupport import * |
paul@0 | 12 | import re |
paul@0 | 13 | |
paul@0 | 14 | __version__ = "0.1" |
paul@0 | 15 | |
paul@4 | 16 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 17 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 18 | |
paul@3 | 19 | # Common action functionality. |
paul@3 | 20 | |
paul@3 | 21 | class MoinFormHandlerAction: |
paul@3 | 22 | |
paul@3 | 23 | "A handler action that can be specialised for individual forms." |
paul@3 | 24 | |
paul@3 | 25 | def __init__(self, pagename, request): |
paul@3 | 26 | self.pagename = pagename |
paul@3 | 27 | self.request = request |
paul@3 | 28 | |
paul@3 | 29 | def processForm(self): |
paul@3 | 30 | |
paul@3 | 31 | """ |
paul@3 | 32 | Interpret the request details and modify them according to the structure |
paul@3 | 33 | of the interpreted information. |
paul@3 | 34 | """ |
paul@3 | 35 | |
paul@3 | 36 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 37 | |
paul@3 | 38 | form = get_form(self.request) |
paul@3 | 39 | fields = getFields(form, remove=True) |
paul@3 | 40 | |
paul@3 | 41 | # Modify, serialise and show the form. |
paul@3 | 42 | |
paul@3 | 43 | self.modifyFields(fields) |
paul@4 | 44 | self.validateFields(fields) |
paul@3 | 45 | self.serialiseFields(fields, form) |
paul@3 | 46 | do_show(self.pagename, self.request) |
paul@3 | 47 | |
paul@4 | 48 | def validateFields(self, fields): |
paul@4 | 49 | pass |
paul@4 | 50 | |
paul@3 | 51 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 52 | |
paul@3 | 53 | """ |
paul@3 | 54 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 55 | to name the entries. |
paul@3 | 56 | """ |
paul@3 | 57 | |
paul@3 | 58 | for key, value in fields.items(): |
paul@3 | 59 | |
paul@3 | 60 | # Serialise sections. |
paul@3 | 61 | |
paul@3 | 62 | if isinstance(value, dict): |
paul@3 | 63 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 64 | element_ref = "%s$%s" % (key, index) |
paul@3 | 65 | |
paul@3 | 66 | self.serialiseFields(element, form, |
paul@3 | 67 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 68 | ) |
paul@3 | 69 | |
paul@3 | 70 | # Serialise fields. |
paul@3 | 71 | |
paul@3 | 72 | else: |
paul@3 | 73 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 74 | |
paul@3 | 75 | def modifyFields(self, fields): |
paul@3 | 76 | |
paul@3 | 77 | "Modify the given 'fields', removing and adding items." |
paul@3 | 78 | |
paul@3 | 79 | # First, remove fields. |
paul@3 | 80 | |
paul@3 | 81 | for key in fields.keys(): |
paul@3 | 82 | if key.startswith("_remove="): |
paul@3 | 83 | self.removeField(key[8:], fields) |
paul@3 | 84 | |
paul@3 | 85 | # Then, add fields. |
paul@3 | 86 | |
paul@3 | 87 | for key in fields.keys(): |
paul@3 | 88 | if key.startswith("_add="): |
paul@3 | 89 | self.addField(key[5:], fields) |
paul@3 | 90 | |
paul@3 | 91 | def removeField(self, path, fields): |
paul@3 | 92 | |
paul@3 | 93 | """ |
paul@3 | 94 | Remove the section element indicated by the given 'path' from the |
paul@3 | 95 | 'fields'. |
paul@3 | 96 | """ |
paul@3 | 97 | |
paul@3 | 98 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 99 | del section[name][index] |
paul@3 | 100 | |
paul@3 | 101 | def addField(self, path, fields): |
paul@3 | 102 | |
paul@3 | 103 | """ |
paul@3 | 104 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 105 | """ |
paul@3 | 106 | |
paul@3 | 107 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 108 | placeholder = {"_new" : ""} |
paul@3 | 109 | |
paul@3 | 110 | if section.has_key(name): |
paul@3 | 111 | indexes = section[name].keys() |
paul@3 | 112 | max_index = max(map(int, indexes)) |
paul@3 | 113 | section[name][max_index + 1] = placeholder |
paul@3 | 114 | else: |
paul@3 | 115 | max_index = -1 |
paul@3 | 116 | section[name] = {0 : placeholder} |
paul@3 | 117 | |
paul@5 | 118 | # Form and field information. |
paul@5 | 119 | |
paul@5 | 120 | def getFieldArguments(field_definition): |
paul@5 | 121 | |
paul@5 | 122 | "Return the parsed arguments from the given 'field_definition' string." |
paul@5 | 123 | |
paul@5 | 124 | field_args = {} |
paul@5 | 125 | |
paul@5 | 126 | for field_arg in field_definition.split(): |
paul@5 | 127 | |
paul@5 | 128 | # Record the key-value details. |
paul@5 | 129 | |
paul@5 | 130 | try: |
paul@5 | 131 | argname, argvalue = field_arg.split("=", 1) |
paul@5 | 132 | field_args[argname] = argvalue |
paul@5 | 133 | |
paul@5 | 134 | # Single keywords are interpreted as type descriptions. |
paul@5 | 135 | |
paul@5 | 136 | except ValueError: |
paul@5 | 137 | if not field_args.has_key("type"): |
paul@5 | 138 | field_args["type"] = field_arg |
paul@5 | 139 | |
paul@5 | 140 | return field_args |
paul@5 | 141 | |
paul@0 | 142 | # Common formatting functions. |
paul@0 | 143 | |
paul@0 | 144 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@0 | 145 | |
paul@0 | 146 | """ |
paul@0 | 147 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@0 | 148 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 149 | |
paul@0 | 150 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 151 | write output using the request. |
paul@0 | 152 | """ |
paul@0 | 153 | |
paul@0 | 154 | write = write or request.write |
paul@0 | 155 | page = request.page |
paul@0 | 156 | |
paul@0 | 157 | fields = getFields(get_form(request)) |
paul@0 | 158 | |
paul@1 | 159 | queryparams = [] |
paul@1 | 160 | |
paul@1 | 161 | for argname in ["fragment", "action"]: |
paul@1 | 162 | if attrs and attrs.has_key(argname): |
paul@1 | 163 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@1 | 164 | |
paul@1 | 165 | querystr = "&".join(queryparams) |
paul@0 | 166 | |
paul@0 | 167 | write(fmt.rawHTML('<form method="post" action="%s">' % |
paul@0 | 168 | escattr(page.url(request, querystr)) |
paul@0 | 169 | )) |
paul@0 | 170 | |
paul@6 | 171 | # Obtain page text for the form, incorporating subregions and applicable |
paul@6 | 172 | # sections. |
paul@6 | 173 | |
paul@0 | 174 | output = getFormOutput(text, fields) |
paul@2 | 175 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@0 | 176 | |
paul@0 | 177 | write(fmt.rawHTML('</form>')) |
paul@0 | 178 | |
paul@6 | 179 | def getFormOutput(text, fields, path=None): |
paul@0 | 180 | |
paul@0 | 181 | """ |
paul@0 | 182 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 183 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 184 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 185 | regions, and such sections may not define separate subregions but instead |
paul@0 | 186 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 187 | |
paul@0 | 188 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 189 | control whether sections are populated or not. |
paul@0 | 190 | """ |
paul@0 | 191 | |
paul@0 | 192 | output = [] |
paul@0 | 193 | section = fields |
paul@0 | 194 | |
paul@0 | 195 | for region in getRegions(text, True): |
paul@0 | 196 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 197 | |
paul@6 | 198 | # Adjust any FormField macros to use hierarchical names. |
paul@0 | 199 | |
paul@0 | 200 | if format is None: |
paul@6 | 201 | if path: |
paul@6 | 202 | adjusted_body = adjustFormFields(body, path) |
paul@6 | 203 | output.append(adjusted_body) |
paul@6 | 204 | else: |
paul@6 | 205 | output.append(body) |
paul@0 | 206 | |
paul@0 | 207 | # Include form sections only if fields exist for those sections. |
paul@0 | 208 | |
paul@0 | 209 | elif format == "form": |
paul@0 | 210 | section_name = attributes.get("section") |
paul@0 | 211 | if section_name and section.has_key(section_name): |
paul@1 | 212 | |
paul@1 | 213 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 214 | |
paul@1 | 215 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@6 | 216 | element_ref = "%s$%s" % (section_name, index) |
paul@1 | 217 | |
paul@6 | 218 | # Get the output for the section. |
paul@6 | 219 | |
paul@6 | 220 | output.append(getFormOutput(body, element, |
paul@6 | 221 | path and ("%s/%s" % (path, element_ref)) or element_ref)) |
paul@0 | 222 | |
paul@0 | 223 | # Inspect and include other regions. |
paul@0 | 224 | |
paul@0 | 225 | else: |
paul@0 | 226 | output.append(header) |
paul@6 | 227 | output.append(getFormOutput(body, section, path)) |
paul@0 | 228 | output.append(close) |
paul@0 | 229 | |
paul@0 | 230 | return "".join(output) |
paul@0 | 231 | |
paul@6 | 232 | def adjustFormFields(body, path): |
paul@1 | 233 | |
paul@1 | 234 | """ |
paul@1 | 235 | Return a version of the 'body' with the names in FormField macros updated to |
paul@6 | 236 | incorporate the given 'path'. |
paul@1 | 237 | """ |
paul@1 | 238 | |
paul@1 | 239 | result = [] |
paul@4 | 240 | type = None |
paul@1 | 241 | |
paul@7 | 242 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@7 | 243 | state = i % 3 |
paul@4 | 244 | |
paul@4 | 245 | # Reproduce normal text as is. |
paul@4 | 246 | |
paul@7 | 247 | if state == 0: |
paul@1 | 248 | result.append(match) |
paul@4 | 249 | |
paul@4 | 250 | # Capture the macro type. |
paul@4 | 251 | |
paul@7 | 252 | elif state == 1: |
paul@4 | 253 | type = match |
paul@4 | 254 | |
paul@4 | 255 | # Substitute the macro and modified arguments. |
paul@4 | 256 | |
paul@1 | 257 | else: |
paul@4 | 258 | result.append("<<Form%s(%s)>>" % (type, ",".join( |
paul@6 | 259 | adjustMacroArguments(parseMacroArguments(match), path) |
paul@4 | 260 | ))) |
paul@1 | 261 | |
paul@1 | 262 | return "".join(result) |
paul@1 | 263 | |
paul@6 | 264 | def adjustMacroArguments(args, path): |
paul@0 | 265 | |
paul@0 | 266 | """ |
paul@1 | 267 | Adjust the given 'args' so that the path incorporates the given |
paul@6 | 268 | 'path', returning a new list containing the revised path and remaining |
paul@6 | 269 | arguments. |
paul@1 | 270 | """ |
paul@1 | 271 | |
paul@1 | 272 | result = [] |
paul@6 | 273 | old_path = None |
paul@1 | 274 | |
paul@1 | 275 | for arg in args: |
paul@1 | 276 | if arg.startswith("path="): |
paul@6 | 277 | old_path = arg[5:] |
paul@1 | 278 | else: |
paul@1 | 279 | result.append(arg) |
paul@1 | 280 | |
paul@6 | 281 | qualified = old_path and ("%s/%s" % (old_path, path)) or path |
paul@1 | 282 | result.append("path=%s" % qualified) |
paul@1 | 283 | |
paul@1 | 284 | return result |
paul@1 | 285 | |
paul@1 | 286 | def parseMacroArguments(args): |
paul@1 | 287 | |
paul@1 | 288 | """ |
paul@1 | 289 | Interpret the arguments. |
paul@1 | 290 | NOTE: The argument parsing should really be more powerful in order to |
paul@1 | 291 | NOTE: support labels. |
paul@1 | 292 | """ |
paul@1 | 293 | |
paul@1 | 294 | try: |
paul@1 | 295 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 296 | except AttributeError: |
paul@1 | 297 | parsed_args = args.split(",") |
paul@1 | 298 | |
paul@1 | 299 | return [arg for arg in parsed_args if arg] |
paul@1 | 300 | |
paul@1 | 301 | def getFields(d, remove=False): |
paul@1 | 302 | |
paul@1 | 303 | """ |
paul@1 | 304 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 305 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 306 | the fields from 'd'. |
paul@0 | 307 | """ |
paul@0 | 308 | |
paul@0 | 309 | fields = {} |
paul@0 | 310 | |
paul@0 | 311 | for key, value in d.items(): |
paul@0 | 312 | |
paul@1 | 313 | # Detect modifying fields. |
paul@1 | 314 | |
paul@1 | 315 | if key.find("=") != -1: |
paul@1 | 316 | fields[key] = value |
paul@1 | 317 | if remove: |
paul@1 | 318 | del d[key] |
paul@1 | 319 | continue |
paul@1 | 320 | |
paul@0 | 321 | # Reproduce the original hierarchy of the fields. |
paul@0 | 322 | |
paul@0 | 323 | section = fields |
paul@1 | 324 | parts = getPathDetails(key) |
paul@0 | 325 | |
paul@1 | 326 | for name, index in parts[:-1]: |
paul@1 | 327 | |
paul@1 | 328 | # Add an entry for instances of the section. |
paul@0 | 329 | |
paul@0 | 330 | if not section.has_key(name): |
paul@0 | 331 | section[name] = {} |
paul@0 | 332 | |
paul@1 | 333 | # Add an entry for the specific instance of the section. |
paul@1 | 334 | |
paul@0 | 335 | if not section[name].has_key(index): |
paul@0 | 336 | section[name][index] = {} |
paul@0 | 337 | |
paul@0 | 338 | section = section[name][index] |
paul@0 | 339 | |
paul@1 | 340 | section[parts[-1][0]] = value |
paul@1 | 341 | |
paul@1 | 342 | if remove: |
paul@1 | 343 | del d[key] |
paul@0 | 344 | |
paul@0 | 345 | return fields |
paul@0 | 346 | |
paul@1 | 347 | def getPathDetails(path): |
paul@4 | 348 | |
paul@4 | 349 | """ |
paul@4 | 350 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 351 | of section instances, with any specific field appearing as the last element |
paul@4 | 352 | and having the form (name, None). |
paul@4 | 353 | """ |
paul@4 | 354 | |
paul@1 | 355 | parts = [] |
paul@1 | 356 | |
paul@1 | 357 | for part in path.split("/"): |
paul@1 | 358 | try: |
paul@1 | 359 | name, index = part.split("$", 1) |
paul@1 | 360 | index = int(index) |
paul@1 | 361 | except ValueError: |
paul@1 | 362 | name, index = part, None |
paul@1 | 363 | |
paul@1 | 364 | parts.append((name, index)) |
paul@1 | 365 | |
paul@1 | 366 | return parts |
paul@1 | 367 | |
paul@1 | 368 | def getSectionForPath(path, fields): |
paul@1 | 369 | |
paul@1 | 370 | """ |
paul@1 | 371 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 372 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 373 | parent section contains the referenced section, where name is the name of |
paul@1 | 374 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 375 | specific section instance within the named section. |
paul@1 | 376 | """ |
paul@1 | 377 | |
paul@1 | 378 | parts = getPathDetails(path) |
paul@1 | 379 | section = fields |
paul@1 | 380 | |
paul@1 | 381 | for name, index in parts[:-1]: |
paul@1 | 382 | section = fields[name][index] |
paul@1 | 383 | |
paul@1 | 384 | return section, parts[-1] |
paul@1 | 385 | |
paul@1 | 386 | def getSectionElements(section_elements): |
paul@1 | 387 | |
paul@1 | 388 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 389 | |
paul@1 | 390 | keys = map(int, section_elements.keys()) |
paul@1 | 391 | keys.sort() |
paul@1 | 392 | |
paul@1 | 393 | elements = [] |
paul@1 | 394 | |
paul@1 | 395 | for key in keys: |
paul@1 | 396 | elements.append(section_elements[key]) |
paul@1 | 397 | |
paul@1 | 398 | return elements |
paul@1 | 399 | |
paul@0 | 400 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 401 | |
paul@0 | 402 | """ |
paul@0 | 403 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 404 | 'mimetype'. |
paul@0 | 405 | |
paul@0 | 406 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 407 | |
paul@0 | 408 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 409 | write output using the request. |
paul@0 | 410 | """ |
paul@0 | 411 | |
paul@0 | 412 | write = write or request.write |
paul@0 | 413 | |
paul@0 | 414 | if mimetype == "text/html": |
paul@0 | 415 | write('<html>') |
paul@0 | 416 | write('<body>') |
paul@0 | 417 | fmt = request.html_formatter |
paul@0 | 418 | fmt.setPage(request.page) |
paul@0 | 419 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 420 | write('</body>') |
paul@0 | 421 | write('</html>') |
paul@0 | 422 | |
paul@0 | 423 | # vim: tabstop=4 expandtab shiftwidth=4 |