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 fragment = None 45 index = None 46 47 for argname, argvalue in parsed_args: 48 if argname == "name": 49 name = argvalue 50 51 elif argname == "path": 52 path = argvalue 53 54 if argname == "fragment": 55 fragment = argvalue 56 57 elif argname == "index": 58 index = argvalue 59 60 elif argname is None and name is None: 61 name = argvalue 62 63 if not name: 64 return showError(_("No field name specified."), request) 65 66 # The field name is a combination of the path and the name. 67 68 ref = "%s%s" % (path and ("%s/" % path) or "", name) 69 70 # Obtain any request parameters corresponding to the field. 71 72 form = get_form(request) 73 form_fragment = form.get("fragment", [None])[0] 74 75 # Exclude values intended for other forms. 76 77 if fragment and form_fragment != fragment or not fragment and form_fragment: 78 return "" 79 80 # Render the message. 81 82 else: 83 value = form.get(ref, [""])[index and int(index) or 0] 84 return fmt.text(value) 85 86 def showError(text, request): 87 fmt = request.formatter 88 89 output = [] 90 append = output.append 91 92 append(fmt.span(on=1, attrs={"class" : "form-field-error"})) 93 append(fmt.text(text)) 94 append(fmt.span(on=0)) 95 96 return "".join(output) 97 98 # vim: tabstop=4 expandtab shiftwidth=4