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 """ 27 28 request = macro.request 29 fmt = macro.formatter 30 page = fmt.page 31 _ = request.getText 32 33 # Interpret the arguments. 34 35 parsed_args = parseMacroArguments(args) 36 37 # Get special arguments. 38 39 name = None 40 path = None 41 42 for arg in parsed_args: 43 if arg.startswith("name="): 44 name = arg[5:] 45 46 elif arg.startswith("path="): 47 path = arg[5:] 48 49 elif name is None: 50 name = arg 51 52 if not name: 53 return showError(_("No field name specified."), request) 54 55 # The field name is a combination of the path and the name. 56 57 ref = "%s%s" % (path and ("%s/" % path) or "", name) 58 59 # Obtain any request parameters corresponding to the field. 60 61 form = get_form(request) 62 value = form.get(ref, [""])[0] 63 64 # Render the message. 65 66 return fmt.text(value) 67 68 def showError(text, request): 69 fmt = request.formatter 70 71 output = [] 72 append = output.append 73 74 append(fmt.span(on=1, attrs={"class" : "form-field-error"})) 75 append(fmt.text(text)) 76 append(fmt.span(on=0)) 77 78 return "".join(output) 79 80 # vim: tabstop=4 expandtab shiftwidth=4