1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - FormField 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 MoinMoin import wikiutil 10 from MoinSupport import * 11 from MoinForms import getFieldArguments, parseMacroArguments, getMacroArguments 12 13 Dependencies = ['pages'] 14 15 escape = wikiutil.escape 16 17 # Macro functions. 18 19 def execute(macro, args): 20 21 """ 22 Execute the 'macro' with the given 'args' to produce a form field element: 23 24 * A field name 25 * The WikiDict describing form fields 26 27 The following optional named arguments are also supported: 28 29 label=TEXT The label employed by button-like fields 30 path=PATH The location of the field in the form section hierarchy 31 32 The nature of each field is described by a WikiDict entry for the given 33 field name. 34 """ 35 36 request = macro.request 37 fmt = macro.formatter 38 page = fmt.page 39 _ = request.getText 40 41 # Interpret the arguments. 42 43 parsed_args = parseMacroArguments(args) 44 45 # Get special arguments. 46 47 name, path, dictpage, label, section = getMacroArguments(parsed_args) 48 49 if not name: 50 return showError(_("No field name specified."), request) 51 52 # Detect special modification fields. 53 54 if name in ("_add", "_remove"): 55 field_args = {"type" : "submit"} 56 57 # The field name is a combination of the name, path and section. 58 59 ref = "%s=%s%s%s" % (name, path or "", path and section and "/" or "", section or "") 60 61 # Get the WikiDict and the field's definition. 62 63 elif dictpage: 64 wikidict = getWikiDict(dictpage, request) 65 66 if not wikidict: 67 return showError(_("WikiDict %s cannot be loaded for %s.") % (dictpage, name), request) 68 69 try: 70 field_definition = wikidict[name] 71 except KeyError: 72 return showError(_("No entry for %s in %s.") % (name, dictpage), request) 73 74 field_args = getFieldArguments(field_definition) 75 76 # The field name is a combination of the path and the name. 77 78 ref = "%s%s" % (path and ("%s/" % path) or "", name) 79 80 else: 81 return showError(_("No WikiDict specified for %s.") % name, request) 82 83 # Obtain any request parameters corresponding to the field. 84 85 form = get_form(request) 86 values = form.get(ref, []) 87 value = form.get(ref, [""])[0] 88 89 # Render the field. 90 91 type = field_args.get("type", "text") 92 93 if type == "text": 94 return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % ( 95 escattr(ref), escattr(type), escattr(field_args.get("size", "10")), escattr(value) 96 )) 97 98 elif type == "textarea": 99 return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % ( 100 escattr(ref), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value) 101 )) 102 103 elif type == "submit": 104 return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % ( 105 escattr(ref), escattr(_(label or name)) 106 )) 107 108 elif type == "select": 109 110 if not field_args.has_key("source"): 111 return showError(_("No source dictionary given for %s.") % name, request) 112 113 sourcedict = getWikiDict(field_args["source"], request) 114 115 if not sourcedict: 116 return showError(_("WikiDict %s cannot be loaded for %s.") % (sourcedict, name), request) 117 118 maxselected = field_args.get("maxselected", [None])[0] 119 maxselected = maxselected and int(maxselected) or maxselected 120 121 output = [] 122 output.append(fmt.rawHTML('<select name="%s" %s>' % (escattr(ref), maxselected != 1 and "multiple" or ""))) 123 124 for option, label in sourcedict.items(): 125 is_selected = maxselected == 1 and option == value or maxselected != 1 and option in values 126 127 output.append(fmt.rawHTML('<option value="%s" %s>%s</option>' % ( 128 escattr(option), is_selected and "selected" or "", escape(label)) 129 )) 130 131 output.append(fmt.rawHTML('</select>')) 132 return u''.join(output) 133 134 else: 135 return u'' 136 137 def showError(text, request): 138 fmt = request.formatter 139 140 output = [] 141 append = output.append 142 143 append(fmt.span(on=1, attrs={"class" : "form-field-error"})) 144 append(fmt.text(text)) 145 append(fmt.span(on=0)) 146 147 return "".join(output) 148 149 # vim: tabstop=4 expandtab shiftwidth=4