-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_maker.py
139 lines (101 loc) · 3.58 KB
/
app_maker.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# -*- coding: utf-8 -*-
"""
App Factory
remark: copy from flaskr demo in flask/examples
"""
from flask import Flask, render_template
from werkzeug.utils import find_modules, import_string
from common.http import JsonResponse
from common.converters import ListConverter
from common.extensions import (cache, db, logger, migrate,
bcrypt, csrf_protect, login_manager,
debug_toolbar, webpack)
# from common.extensions import celery
from models.admin import admin
from common.utils import import_to_context
def create_app(name=__name__):
app = Flask(name, template_folder='templates', static_folder='static')
# load config file from settings.py first
app.config.from_object('settings')
# return json response
# app.response_class = JsonResponse
# register a new converter
app.url_map.converters.update(list=ListConverter)
# register context processor
register_context_processor(app, {
'test': 'test<script>alert(1)</script>'
})
register_blueprints(app)
register_commands(app)
register_extensions(app)
register_errorhandlers(app)
register_shellcontext(app)
return app
def register_extensions(app):
"""Register Flask extensions."""
logger.init_app(app, 'flask.log')
db.init_app(app)
migrate.init_app(app, db)
cache.init_app(app)
admin.init_app(app)
# KeyError: 'CELERY_BROKER_URL'
# celery.init_app(app)
bcrypt.init_app(app)
csrf_protect.init_app(app)
login_manager.init_app(app)
debug_toolbar.init_app(app)
webpack.init_app(app)
return None
def register_blueprints(app, pkg='blueprints'):
"""register all blueprint modules in bp_pkg package
"""
for name in find_modules(pkg):
try:
mod = import_string(name)
if hasattr(mod, 'bp'):
print '[{}] register blueprints: {}'.format(name, mod.bp.url_prefix)
app.register_blueprint(mod.bp)
except Exception as e:
print '[{}] register api/blueprints: {}'.format(name, e)
return None
def register_commands(app, pkg='commands'):
"""Register Click commands."""
for name in find_modules(pkg):
try:
mod = import_string(name)
if hasattr(mod, 'command'):
app.cli.add_command(mod.command, name=name.split('.')[1])
except Exception as e:
print '[{}] register cmd: {}'.format(name, e)
return None
def register_errorhandlers(app):
"""Register error handlers."""
def render_error(error):
"""Render error template."""
# If a HTTPException, pull the `code` attribute; default to 500
error_code = getattr(error, 'code', 500)
return render_template('{0}.html'.format(error_code)), error_code
for errcode in [401, 404, 500]:
app.register_error_handler(errcode, render_error)
# app.errorhandler(errcode)(render_error)
return None
def register_shellcontext(app):
"""Register shell context objects."""
def shell_context():
"""Shell context objects."""
extra_context = {
'db': db
}
# import models to shell_context
import_to_context('models', extra_context)
# print 'Shell Context: %s' % extra_context
return extra_context
app.shell_context_processor(shell_context)
def register_teardowns(app, func):
@app.teardown_appcontext
def command(error):
func()
def register_context_processor(app, extra_context):
@app.context_processor
def context():
return extra_context