annotate MoinMoin/util/forms.py @ 229:c956811fba9a

flatland filters: fix copy&paste bug, only use "required" attr in input fields
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 14 May 2011 23:06:29 +0200
parents 029cb888d253
children a11656ba1eab
rev   line source
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
1 # Copyright: 2010 Thomas Waldmann, Jason Kirtland, Scott Wilson
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
2 # License: see flatland license
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
3
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 MoinMoin - form helpers for flatland / jinja2
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 21
diff changeset
6 """
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 from jinja2 import Markup
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 from flatland.out.markup import Generator
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 from flatland.schema.util import find_i18n_function
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 from MoinMoin.i18n import _, L_, N_
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 def label_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 """Provide a translated, generated fallback for field labels."""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 if bind is not None and not contents:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 contents = _(bind.label)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 label_filter.tags = set(['label'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 def button_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 """Show translated text in clickable buttons and submits."""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 if bind is None:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 if tagname == 'input':
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 if ('value' not in attributes and
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 attributes.get('type') in ['submit', 'reset', ]):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
31 attributes['value'] = _(bind.default_value)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 elif tagname == 'button' and not contents:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 contents = _(bind.default_value)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 button_filter.tags = set(['input', 'button'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
37 def required_filter(tagname, attributes, contents, context, bind):
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
38 if (bind is not None and not bind.optional):
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
39 attributes[u'class'] = u'required'
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
40 if tagname == 'input':
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
41 attributes[u'required'] = u'required'
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
42 return contents
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
43 required_filter.tags = set(['input', 'label'])
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
45 def autofocus_filter(tagname, attributes, contents, context, bind):
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
46 if bind is not None:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
47 autofocus = bind.properties.get('autofocus')
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
48 if autofocus:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
49 attributes[u'autofocus'] = u'autofocus'
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
50 return contents
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
51 autofocus_filter.tags = set(['input', 'textarea', ])
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
52
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
53 def placeholder_filter(tagname, attributes, contents, context, bind):
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
54 if bind is not None:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
55 placeholder = bind.properties.get('placeholder')
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
56 if placeholder:
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
57 attributes[u'placeholder'] = placeholder
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
58 return contents
229
c956811fba9a flatland filters: fix copy&paste bug, only use "required" attr in input fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 226
diff changeset
59 placeholder_filter.tags = set(['input', 'textarea', ])
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
60
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 def error_filter_factory(class_='moin-error'):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 """Returns an HTML generation filter annotating field CSS class on error.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 :param class: The css class to apply in case of validation error on a
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 field. Default: 'error'
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 def error_filter(tagname, attributes, contents, context, bind):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 if bind is not None and bind.errors:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 if 'class' in attributes:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 attributes['class'] = ' '.join([attributes['class'], class_])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 else:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 attributes['class'] = class_
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 return contents
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 error_filter.tags = set(['input'])
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 return error_filter
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 error_filter = error_filter_factory()
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 def make_generator():
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 """make an html generator"""
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 return Generator(auto_domid=True, auto_for=True, auto_filter=True,
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 markup_wrapper=Markup,
218
fd36d76dd0e3 util.forms: new filter for required attributes added, thanks to Jason Kirtland
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 26
diff changeset
84 filters=[label_filter, button_filter,
226
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
85 error_filter,
029cb888d253 use flatland filters for autofocus/placeholder html5 attrs, use html5 input field types email/url/search
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 218
diff changeset
86 required_filter, placeholder_filter, autofocus_filter])
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87