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
|
# -*- encoding: utf-8 -*-
import flask
from flask_wtf import FlaskForm
from wtforms.fields import BooleanField, StringField, HiddenField, PasswordField, DecimalField as WTFDecimalField, DateField as HTML4DateField
from wtforms.fields.html5 import DateField, IntegerField
from wtforms import validators, ValidationError, Form as WTForm
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from . import app
@app.template_test('hidden')
def is_hidden_field(f):
return isinstance(f, HiddenField)
class DecimalField(WTFDecimalField):
def __init__(self, *args, **kwargs):
render_kw = kwargs.setdefault('render_kw', dict())
render_kw.setdefault('inputmethod', 'decimal')
super().__init__(*args, **kwargs)
def process_formdata(self, valuelist):
if valuelist:
value = valuelist[0].replace(',','.')
super().process_formdata([value])
class MonthField(HTML4DateField):
def __init__(self, label, validators, format='%m.%Y', **kwargs):
super().__init__(label, validators, format, **kwargs)
req = [validators.input_required()]
def lenOf(col):
if hasattr(col.type, 'length') and col.type.length:
return [validators.length(max = col.type.length)]
class Form(FlaskForm):
class Meta:
locales = ['de_DE', 'de']
def __init__ (self, *args, **kwargs):
self._msg = kwargs.pop('flash', "Fehler im Formular!")
super().__init__(*args, **kwargs)
def flash(self):
flask.flash(self._msg, 'error')
def flash_validate (self):
if not self.validate():
self.flash()
return False
return True
def validate_on_submit (self):
return self.is_submitted() and self.flash_validate()
def _get_translations(self):
# use WTForms builtin translation support instead of the flask-babael
# stuff added by flask-wtf
# FIXME: remove this, if flask-babel is used in the app
return WTForm._get_translations(self)
|