paul@4 | 1 | # -*- coding: iso-8859-1 -*- |
paul@4 | 2 | """ |
paul@4 | 3 | MoinMoin - FormMessage Macro |
paul@4 | 4 | |
paul@22 | 5 | @copyright: 2012, 2013 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@22 | 26 | fragment=NAME The name of the form region or fragment in the page |
paul@18 | 27 | index=INDEX The index of the value to be displayed (instead of the first |
paul@18 | 28 | value) |
paul@4 | 29 | """ |
paul@4 | 30 | |
paul@4 | 31 | request = macro.request |
paul@4 | 32 | fmt = macro.formatter |
paul@4 | 33 | page = fmt.page |
paul@4 | 34 | _ = request.getText |
paul@4 | 35 | |
paul@4 | 36 | # Interpret the arguments. |
paul@4 | 37 | |
paul@4 | 38 | parsed_args = parseMacroArguments(args) |
paul@4 | 39 | |
paul@4 | 40 | # Get special arguments. |
paul@4 | 41 | |
paul@4 | 42 | name = None |
paul@4 | 43 | path = None |
paul@25 | 44 | fragment = None |
paul@18 | 45 | index = None |
paul@4 | 46 | |
paul@33 | 47 | for argname, argvalue in parsed_args: |
paul@33 | 48 | if argname == "name": |
paul@33 | 49 | name = argvalue |
paul@4 | 50 | |
paul@33 | 51 | elif argname == "path": |
paul@33 | 52 | path = argvalue |
paul@4 | 53 | |
paul@33 | 54 | if argname == "fragment": |
paul@33 | 55 | fragment = argvalue |
paul@25 | 56 | |
paul@33 | 57 | elif argname == "index": |
paul@33 | 58 | index = argvalue |
paul@18 | 59 | |
paul@33 | 60 | elif argname is None and name is None: |
paul@33 | 61 | name = argvalue |
paul@4 | 62 | |
paul@4 | 63 | if not name: |
paul@4 | 64 | return showError(_("No field name specified."), request) |
paul@4 | 65 | |
paul@4 | 66 | # The field name is a combination of the path and the name. |
paul@4 | 67 | |
paul@4 | 68 | ref = "%s%s" % (path and ("%s/" % path) or "", name) |
paul@4 | 69 | |
paul@4 | 70 | # Obtain any request parameters corresponding to the field. |
paul@4 | 71 | |
paul@4 | 72 | form = get_form(request) |
paul@22 | 73 | form_fragment = form.get("fragment", [None])[0] |
paul@22 | 74 | |
paul@22 | 75 | # Exclude values intended for other forms. |
paul@22 | 76 | |
paul@22 | 77 | if fragment and form_fragment != fragment or not fragment and form_fragment: |
paul@28 | 78 | return "" |
paul@4 | 79 | |
paul@4 | 80 | # Render the message. |
paul@4 | 81 | |
paul@28 | 82 | else: |
paul@28 | 83 | value = form.get(ref, [""])[index and int(index) or 0] |
paul@28 | 84 | return fmt.text(value) |
paul@4 | 85 | |
paul@4 | 86 | def showError(text, request): |
paul@4 | 87 | fmt = request.formatter |
paul@4 | 88 | |
paul@4 | 89 | output = [] |
paul@4 | 90 | append = output.append |
paul@4 | 91 | |
paul@4 | 92 | append(fmt.span(on=1, attrs={"class" : "form-field-error"})) |
paul@4 | 93 | append(fmt.text(text)) |
paul@4 | 94 | append(fmt.span(on=0)) |
paul@4 | 95 | |
paul@4 | 96 | return "".join(output) |
paul@4 | 97 | |
paul@4 | 98 | # vim: tabstop=4 expandtab shiftwidth=4 |