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 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 = None 48 path = None 49 dictpage = None 50 label = None 51 section = None 52 53 for arg in parsed_args: 54 if arg.startswith("name="): 55 name = arg[5:] 56 57 elif arg.startswith("path="): 58 path = arg[5:] 59 60 elif arg.startswith("dict="): 61 dictpage = arg[5:] 62 63 elif arg.startswith("label="): 64 label = arg[6:] 65 66 elif arg.startswith("section="): 67 section = arg[8:] 68 69 elif name is None: 70 name = arg 71 72 elif dictpage is None: 73 dictpage = arg 74 75 if not name: 76 return showError(_("No field name specified."), request) 77 78 # Detect special modification fields. 79 80 if name in ("_add", "_remove"): 81 field_args = {"type" : "submit"} 82 83 # The field name is a combination of the name, path and section. 84 85 ref = "%s=%s%s%s" % (name, path or "", path and section and "/" or "", section or "") 86 87 # Get the WikiDict and the field's definition. 88 89 elif dictpage: 90 wikidict = getWikiDict(dictpage, request) 91 92 if not wikidict: 93 return showError(_("WikiDict %s cannot be loaded for %s.") % (dictpage, name), request) 94 95 try: 96 field_definition = wikidict[name] 97 except KeyError: 98 return showError(_("No entry for %s in %s.") % (name, dictpage), request) 99 100 field_args = getFieldArguments(field_definition) 101 102 # The field name is a combination of the path and the name. 103 104 ref = "%s%s" % (path and ("%s/" % path) or "", name) 105 106 else: 107 return showError(_("No WikiDict specified for %s.") % name, request) 108 109 # Obtain any request parameters corresponding to the field. 110 111 form = get_form(request) 112 values = form.get(ref, []) 113 value = form.get(ref, [""])[0] 114 115 # Render the field. 116 117 type = field_args.get("type", "text") 118 119 if type == "text": 120 return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % ( 121 escattr(ref), escattr(type), escattr(field_args.get("size", "10")), escattr(value) 122 )) 123 124 elif type == "textarea": 125 return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % ( 126 escattr(ref), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value) 127 )) 128 129 elif type == "submit": 130 return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % ( 131 escattr(ref), escattr(_(label or name)) 132 )) 133 134 elif type == "select": 135 136 if not field_args.has_key("source"): 137 return showError(_("No source dictionary given for %s.") % name, request) 138 139 sourcedict = getWikiDict(field_args["source"], request) 140 141 if not sourcedict: 142 return showError(_("WikiDict %s cannot be loaded for %s.") % (sourcedict, name), request) 143 144 size = field_args.get("size", [None])[0] 145 size = size and int(size) or size 146 147 output = [] 148 output.append(fmt.rawHTML('<select name="%s" %s>' % (escattr(ref), size != 1 and "multiple" or ""))) 149 150 for option, label in sourcedict.items(): 151 is_selected = size == 1 and option == value or size != 1 and option in values 152 153 output.append(fmt.rawHTML('<option value="%s" %s>%s</option>' % ( 154 escattr(option), is_selected and "selected" or "", escape(label)) 155 )) 156 157 output.append(fmt.rawHTML('</select>')) 158 return u''.join(output) 159 160 else: 161 return u'' 162 163 def showError(text, request): 164 fmt = request.formatter 165 166 output = [] 167 append = output.append 168 169 append(fmt.span(on=1, attrs={"class" : "form-field-error"})) 170 append(fmt.text(text)) 171 append(fmt.span(on=0)) 172 173 return "".join(output) 174 175 # vim: tabstop=4 expandtab shiftwidth=4