SomeForm2   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 10
wmc 1
1
"""A Test app to demonstrate various aspects of flask_extras."""
2
3
from collections import OrderedDict
4
from datetime import datetime as dt
5
6
from flask import (
7
    Flask,
8
    render_template,
9
    flash,
10
    request,
11
)
12
13
from flask_wtf import FlaskForm
14
15
from flask_extras import FlaskExtras
16
from wtforms import (
17
    BooleanField,
18
    IntegerField,
19
    RadioField,
20
    HiddenField,
21
    PasswordField,
22
    SelectField,
23
    SelectMultipleField,
24
    StringField,
25
    SubmitField,
26
    TextAreaField,
27
    validators,
28
)
29
30
from flask_extras.views import statuses
31
32
app = Flask('flask_extras_test')
33
app.secret_key = 'abc1234'
34
35
FlaskExtras(app)
36
37
38
class SomeForm(FlaskForm):
39
    """Form."""
40
41
    hideme = HiddenField()
42
    favorite_food = RadioField(
43
        choices=[('pizza', 'Pizza'), ('ice-cream', 'Ice Cream')]
44
    )
45
    age = IntegerField(validators=[validators.DataRequired()])
46
    name = StringField(
47
        description='enter your name',
48
        validators=[validators.DataRequired()],
49
    )
50
    nickname = StringField('What do people call you?')
51
52
53
class SomeForm2(FlaskForm):
54
    """Form."""
55
56
    hideme = HiddenField()
57
    frobnicate = BooleanField()
58
    baz = StringField()
59
    quux = SelectMultipleField(
60
        choices=[(v, v) for v in ['quux', 'baz', 'foo']])
61
62
63
@app.context_processor
64
def ctx():
65
    """Add global ctx."""
66
    return dict(
67
        ghub_url='https://github.com/christabor/flask_extras/blob/master/flask_extras/macros/',
68
        name=str(request.url_rule),
69
        links=sorted(get_rulesmap()),
70
    )
71
72
73
def get_rulesmap():
74
    """Get all rules so we ensure they're dynamic and always updated."""
75
    bad = ['static', 'index']
76
    return [r.endpoint for r in app.url_map._rules if r.endpoint not in bad]
77
78
79
@app.route('/')
80
def index():
81
    """Demo page links."""
82
    return render_template('pages/index.html', **dict(home=True))
83
84
85
@app.route('/extras_msg.html')
86
def extras_msg():
87
    """Demo page."""
88
    flash('I am a success message!', 'success')
89
    flash('I am warning message!', 'warning')
90
    flash('I am an error (danger) message!', 'error')
91
    flash('I am an info message!', 'info')
92
    return render_template('pages/extras_msg.html')
93
94
95
@app.route('/content_blocks.html')
96
def content_blocks():
97
    """Demo page."""
98
    return render_template('pages/content_blocks.html')
99
100
101
@app.route('/extras_code.html')
102
def extras_code():
103
    """Demo page."""
104
    return render_template('pages/extras_code.html')
105
106
107
@app.route('/dates.html')
108
def dates():
109
    """Demo page."""
110
    kwargs = dict(somedate=dt.now())
111
    return render_template('pages/dates.html', **kwargs)
112
113
114
@app.route('/utils.html')
115
def utils():
116
    """Demo page."""
117
    kwargs = dict()
118
    return render_template('pages/utils.html', **kwargs)
119
120
121
@app.route('/macros.html')
122
def macros():
123
    """Demo page."""
124
    kwargs = dict(
125
        dicttest=dict(
126
            foo='Some bar',
127
            bar='Some foo',
128
        ),
129
        dictlist=[
130
            dict(
131
                foo='Some bar',
132
                bar='Some foo',
133
            )
134
        ],
135
        dictlist2=[
136
            dict(name='foo', age=10, dob='01/01/1900', gender='M'),
137
            dict(name='bar', age=22, dob='01/01/1901', gender='F'),
138
            dict(name='quux', age=120, dob='01/01/1830', gender='X'),
139
        ],
140
        form=SomeForm(),
141
        recursedict=vars(request),
142
    )
143
    return render_template('pages/macros.html', **kwargs)
144
145
146
@app.route('/bootstrap.html')
147
def bootstrap():
148
    """Demo page."""
149
    dicttest = dict(
150
        foo='Some bar',
151
        bar='Some foo',
152
    )
153
    dictlist = [
154
        dict(name='zomb', age=999, dob='03/01/2030', gender='Z102'),
155
        dict(name='foo', age=10, dob='01/01/1900', gender='M'),
156
        dict(name='bar', age=22, dob='01/01/1901', gender='F'),
157
        dict(name='quux', age=120, dob='01/01/1830', gender='X'),
158
    ]
159
    kwargs = dict(
160
        dicttest=dicttest,
161
        dictlist=dictlist,
162
        form=SomeForm(),
163
        form2=SomeForm2(),
164
        pagination=OrderedDict(
165
            zip(['/somelink/{}'.format(i) for i in range(10)], range(10))
166
        ),
167
    )
168
    return render_template('pages/bootstrap.html', **kwargs)
169
170
171
if __name__ == '__main__':
172
    app.run(debug=True, host='0.0.0.0', port=5014)
173