/
form.py
102 lines (85 loc) · 3.34 KB
/
form.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import cherrypy
from markupsafe import Markup
from wtforms.form import Form
SUBMIT_METHODS = {'POST', 'PUT', 'PATCH', 'DELETE'}
class _ProxyFormdata:
"""
Custom class to proxy default form data into WTForm from cherrypy variables.
"""
def __contains__(self, key):
return key in cherrypy.request.params
def getlist(self, key):
# Default to use cherrypy params.
params = cherrypy.request.params
if key in params:
if isinstance(params[key], list):
return params[key]
else:
return [params[key]]
# Return default empty list.
return []
_AUTO = _ProxyFormdata()
class CherryForm(Form):
"""
Custom implementation of WTForm for cherrypy to support kwargs parms.
If ``formdata`` is not specified, this will use cherrypy.request.params
Explicitly pass ``formdata=None`` to prevent this.
"""
def __init__(self, **kwargs):
if 'formdata' in kwargs:
formdata = kwargs.pop('formdata')
else:
formdata = _AUTO if CherryForm.is_submitted(self) else None
super().__init__(formdata=formdata, **kwargs)
def is_submitted(self):
"""
Consider the form submitted if there is an active request and
the method is ``POST``, ``PUT``, ``PATCH``, or ``DELETE``.
"""
return cherrypy.request.method in SUBMIT_METHODS
def validate_on_submit(self):
"""
Call `validate` only if the form is submitted.
This is a shortcut for ``form.is_submitted() and form.validate()``.
"""
return self.is_submitted() and self.validate()
@property
def error_message(self):
if self.errors:
msg = Markup("")
for field, messages in self.errors.items():
if msg:
msg += Markup('<br/>')
# Field name
if field in self:
msg += "%s: " % self[field].label.text
else:
msg += "%s: " % field
for m in messages:
msg += m
return msg
def __html__(self):
"""
Return a HTML representation of the form. For more powerful rendering, see the __call__() method.
"""
return self()
def __call__(self, **kwargs):
env = cherrypy.tree.apps[''].templates.jinja_env
tmpl = env.get_template('components/form.html')
return Markup(tmpl.render(form=self, **kwargs))