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