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