annotate MoinMoin/util/forms.py @ 2657:080673c3e60a

Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
author Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
date Sun, 06 Jul 2014 09:25:31 +0530
parents ee404f81afe9
children
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
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
16
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 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
18 """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
19 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
20 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
21 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
22 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
23
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
24
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 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
26 """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
27 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
28 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
29 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
30 if ('value' not in attributes and
2589
ee404f81afe9 fix new pep8 errors generated after pep8 is upgraded to 1.5.1
RogerHaase <haaserd@gmail.com>
parents: 2426
diff changeset
31 attributes.get('type') in ['submit', 'reset', ]):
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 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
33 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
34 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
35 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
36 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
37
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
38
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
39 def required_filter(tagname, attributes, contents, context, bind):
1983
bb2f526d961c fix some stuff discovered by pycharm code inspection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
40 if bind is not None and not bind.optional:
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
41 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
42 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
43 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
44 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
45
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
46
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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
55
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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
64
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 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
66 """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
67
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 :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
69 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
70 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 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
72 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
73 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
74 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
75 else:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 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
77 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
78 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
79 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
80
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 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
82
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 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
85 """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
86 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
87 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
88 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
89 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
90 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
91
244
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
92
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
93 # other flatland stuff
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
94
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
95 from flatland import AdaptationError, Scalar
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
96 import werkzeug
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
97
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
98
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
99 class FileStorage(Scalar):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
100 """Schema element for Werkzeug FileStorage instances."""
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
101
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
102 def adapt(self, value):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
103 if not isinstance(value, (type(None), werkzeug.FileStorage)):
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
104 raise AdaptationError
a11656ba1eab modify views: use flatland also for the file upload form field
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 229
diff changeset
105 return value