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