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