paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - FormField Macro |
paul@0 | 4 | |
paul@22 | 5 | @copyright: 2012, 2013 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@22 | 31 | fragment=NAME The name of the form region or fragment in the page |
paul@0 | 32 | |
paul@0 | 33 | The nature of each field is described by a WikiDict entry for the given |
paul@0 | 34 | field name. |
paul@0 | 35 | """ |
paul@0 | 36 | |
paul@0 | 37 | request = macro.request |
paul@0 | 38 | fmt = macro.formatter |
paul@0 | 39 | page = fmt.page |
paul@0 | 40 | _ = request.getText |
paul@0 | 41 | |
paul@0 | 42 | # Interpret the arguments. |
paul@0 | 43 | |
paul@1 | 44 | parsed_args = parseMacroArguments(args) |
paul@0 | 45 | |
paul@0 | 46 | # Get special arguments. |
paul@0 | 47 | |
paul@22 | 48 | name, path, dictpage, label, section, fragment = getMacroArguments(parsed_args) |
paul@0 | 49 | |
paul@0 | 50 | if not name: |
paul@0 | 51 | return showError(_("No field name specified."), request) |
paul@0 | 52 | |
paul@1 | 53 | # Detect special modification fields. |
paul@1 | 54 | |
paul@1 | 55 | if name in ("_add", "_remove"): |
paul@1 | 56 | field_args = {"type" : "submit"} |
paul@1 | 57 | |
paul@1 | 58 | # The field name is a combination of the name, path and section. |
paul@1 | 59 | |
paul@1 | 60 | ref = "%s=%s%s%s" % (name, path or "", path and section and "/" or "", section or "") |
paul@0 | 61 | |
paul@0 | 62 | # Get the WikiDict and the field's definition. |
paul@0 | 63 | |
paul@1 | 64 | elif dictpage: |
paul@1 | 65 | wikidict = getWikiDict(dictpage, request) |
paul@0 | 66 | |
paul@1 | 67 | if not wikidict: |
paul@1 | 68 | return showError(_("WikiDict %s cannot be loaded for %s.") % (dictpage, name), request) |
paul@0 | 69 | |
paul@0 | 70 | try: |
paul@1 | 71 | field_definition = wikidict[name] |
paul@1 | 72 | except KeyError: |
paul@1 | 73 | return showError(_("No entry for %s in %s.") % (name, dictpage), request) |
paul@1 | 74 | |
paul@5 | 75 | field_args = getFieldArguments(field_definition) |
paul@1 | 76 | |
paul@1 | 77 | # The field name is a combination of the path and the name. |
paul@1 | 78 | |
paul@1 | 79 | ref = "%s%s" % (path and ("%s/" % path) or "", name) |
paul@1 | 80 | |
paul@1 | 81 | else: |
paul@1 | 82 | return showError(_("No WikiDict specified for %s.") % name, request) |
paul@0 | 83 | |
paul@0 | 84 | # Obtain any request parameters corresponding to the field. |
paul@0 | 85 | |
paul@0 | 86 | form = get_form(request) |
paul@22 | 87 | form_fragment = form.get("fragment", [None])[0] |
paul@22 | 88 | |
paul@22 | 89 | # Exclude values intended for other forms. |
paul@22 | 90 | |
paul@22 | 91 | if fragment and form_fragment != fragment or not fragment and form_fragment: |
paul@22 | 92 | values = [] |
paul@22 | 93 | value = "" |
paul@22 | 94 | else: |
paul@22 | 95 | values = form.get(ref, []) |
paul@22 | 96 | value = form.get(ref, [""])[0] |
paul@0 | 97 | |
paul@0 | 98 | # Render the field. |
paul@0 | 99 | |
paul@0 | 100 | type = field_args.get("type", "text") |
paul@0 | 101 | |
paul@0 | 102 | if type == "text": |
paul@0 | 103 | return fmt.rawHTML('<input name="%s" type="%s" size="%s" value="%s" />' % ( |
paul@1 | 104 | escattr(ref), escattr(type), escattr(field_args.get("size", "10")), escattr(value) |
paul@0 | 105 | )) |
paul@0 | 106 | |
paul@0 | 107 | elif type == "textarea": |
paul@0 | 108 | return fmt.rawHTML('<textarea name="%s" cols="%s" rows="%s">%s</textarea>' % ( |
paul@1 | 109 | escattr(ref), escattr(field_args.get("cols", "60")), escattr(field_args.get("rows", "5")), escape(value) |
paul@0 | 110 | )) |
paul@0 | 111 | |
paul@0 | 112 | elif type == "submit": |
paul@0 | 113 | return fmt.rawHTML('<input name="%s" type="submit" value="%s" />' % ( |
paul@1 | 114 | escattr(ref), escattr(_(label or name)) |
paul@0 | 115 | )) |
paul@0 | 116 | |
paul@1 | 117 | elif type == "select": |
paul@1 | 118 | |
paul@1 | 119 | if not field_args.has_key("source"): |
paul@1 | 120 | return showError(_("No source dictionary given for %s.") % name, request) |
paul@1 | 121 | |
paul@1 | 122 | sourcedict = getWikiDict(field_args["source"], request) |
paul@1 | 123 | |
paul@1 | 124 | if not sourcedict: |
paul@1 | 125 | return showError(_("WikiDict %s cannot be loaded for %s.") % (sourcedict, name), request) |
paul@1 | 126 | |
paul@8 | 127 | maxselected = field_args.get("maxselected", [None])[0] |
paul@8 | 128 | maxselected = maxselected and int(maxselected) or maxselected |
paul@2 | 129 | |
paul@1 | 130 | output = [] |
paul@8 | 131 | output.append(fmt.rawHTML('<select name="%s" %s>' % (escattr(ref), maxselected != 1 and "multiple" or ""))) |
paul@1 | 132 | |
paul@1 | 133 | for option, label in sourcedict.items(): |
paul@8 | 134 | is_selected = maxselected == 1 and option == value or maxselected != 1 and option in values |
paul@2 | 135 | |
paul@1 | 136 | output.append(fmt.rawHTML('<option value="%s" %s>%s</option>' % ( |
paul@28 | 137 | escattr(option), is_selected and 'selected="selected"' or "", escape(label)) |
paul@1 | 138 | )) |
paul@1 | 139 | |
paul@1 | 140 | output.append(fmt.rawHTML('</select>')) |
paul@1 | 141 | return u''.join(output) |
paul@0 | 142 | |
paul@0 | 143 | else: |
paul@1 | 144 | return u'' |
paul@0 | 145 | |
paul@0 | 146 | def showError(text, request): |
paul@0 | 147 | fmt = request.formatter |
paul@0 | 148 | |
paul@0 | 149 | output = [] |
paul@0 | 150 | append = output.append |
paul@0 | 151 | |
paul@0 | 152 | append(fmt.span(on=1, attrs={"class" : "form-field-error"})) |
paul@0 | 153 | append(fmt.text(text)) |
paul@0 | 154 | append(fmt.span(on=0)) |
paul@0 | 155 | |
paul@0 | 156 | return "".join(output) |
paul@0 | 157 | |
paul@0 | 158 | # vim: tabstop=4 expandtab shiftwidth=4 |