paul@4 | 1 | # -*- coding: iso-8859-1 -*- |
paul@4 | 2 | """ |
paul@4 | 3 | MoinMoin - FormMessage Macro |
paul@4 | 4 | |
paul@4 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@4 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@4 | 7 | """ |
paul@4 | 8 | |
paul@4 | 9 | from MoinSupport import * |
paul@4 | 10 | from MoinForms import parseMacroArguments |
paul@4 | 11 | |
paul@4 | 12 | Dependencies = ['pages'] |
paul@4 | 13 | |
paul@4 | 14 | # Macro functions. |
paul@4 | 15 | |
paul@4 | 16 | def execute(macro, args): |
paul@4 | 17 | |
paul@4 | 18 | """ |
paul@4 | 19 | Execute the 'macro' with the given 'args' to produce a form field element: |
paul@4 | 20 | |
paul@4 | 21 | * A field name |
paul@4 | 22 | |
paul@4 | 23 | The following optional named arguments are also supported: |
paul@4 | 24 | |
paul@4 | 25 | path=PATH The location of the field in the form section hierarchy |
paul@4 | 26 | """ |
paul@4 | 27 | |
paul@4 | 28 | request = macro.request |
paul@4 | 29 | fmt = macro.formatter |
paul@4 | 30 | page = fmt.page |
paul@4 | 31 | _ = request.getText |
paul@4 | 32 | |
paul@4 | 33 | # Interpret the arguments. |
paul@4 | 34 | |
paul@4 | 35 | parsed_args = parseMacroArguments(args) |
paul@4 | 36 | |
paul@4 | 37 | # Get special arguments. |
paul@4 | 38 | |
paul@4 | 39 | name = None |
paul@4 | 40 | path = None |
paul@4 | 41 | |
paul@4 | 42 | for arg in parsed_args: |
paul@4 | 43 | if arg.startswith("name="): |
paul@4 | 44 | name = arg[5:] |
paul@4 | 45 | |
paul@4 | 46 | elif arg.startswith("path="): |
paul@4 | 47 | path = arg[5:] |
paul@4 | 48 | |
paul@4 | 49 | elif name is None: |
paul@4 | 50 | name = arg |
paul@4 | 51 | |
paul@4 | 52 | if not name: |
paul@4 | 53 | return showError(_("No field name specified."), request) |
paul@4 | 54 | |
paul@4 | 55 | # The field name is a combination of the path and the name. |
paul@4 | 56 | |
paul@4 | 57 | ref = "%s%s" % (path and ("%s/" % path) or "", name) |
paul@4 | 58 | |
paul@4 | 59 | # Obtain any request parameters corresponding to the field. |
paul@4 | 60 | |
paul@4 | 61 | form = get_form(request) |
paul@4 | 62 | value = form.get(ref, [""])[0] |
paul@4 | 63 | |
paul@4 | 64 | # Render the message. |
paul@4 | 65 | |
paul@4 | 66 | return fmt.text(value) |
paul@4 | 67 | |
paul@4 | 68 | def showError(text, request): |
paul@4 | 69 | fmt = request.formatter |
paul@4 | 70 | |
paul@4 | 71 | output = [] |
paul@4 | 72 | append = output.append |
paul@4 | 73 | |
paul@4 | 74 | append(fmt.span(on=1, attrs={"class" : "form-field-error"})) |
paul@4 | 75 | append(fmt.text(text)) |
paul@4 | 76 | append(fmt.span(on=0)) |
paul@4 | 77 | |
paul@4 | 78 | return "".join(output) |
paul@4 | 79 | |
paul@4 | 80 | # vim: tabstop=4 expandtab shiftwidth=4 |