/
__init__.py
157 lines (121 loc) · 4.77 KB
/
__init__.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
"""
:author: Grey Li (李辉)
:url: http://greyli.com
:copyright: © 2018 Grey Li <withlihui@gmail.com>
:license: MIT, see LICENSE for more details.
"""
import os
import click
from flask import Flask, render_template, jsonify, request
from flask_babel import _
from flask_login import current_user
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from apis.v1 import api_v1
from blueprints.auth import auth_bp
from blueprints.home import home_bp
#from blueprints.todo import todo_bp
from extensions import db, login_manager, csrf, babel
#from models import User, Item
from settings import config
def create_app(config_name=None):
if config_name is None:
config_name = os.getenv('FLASK_CONFIG', 'development')
app = Flask('untitled9')
app.config.from_object(config[config_name])
register_extensions(app)
register_blueprints(app)
register_commands(app)
print("ok")
register_errors(app)
register_template_context(app)
return app
def register_extensions(app):
db.init_app(app)
login_manager.init_app(app)
csrf.init_app(app)
csrf.exempt(api_v1)
babel.init_app(app)
def register_blueprints(app):
app.register_blueprint(auth_bp)
#app.register_blueprint(todo_bp)
app.register_blueprint(home_bp)
app.register_blueprint(api_v1, url_prefix='/api/v1')
# app.register_blueprint(api_v1, url_prefix='/v1', subdomain='api') # enable subdomain support
def register_template_context(app):
''' @app.context_processor
def make_template_context():
if current_user.is_authenticated:
#active_items = Item.query.with_parent(current_user).filter_by(done=False).count()
else:
active_items = None
return dict(active_items=active_items)'''
def register_errors(app):
@app.errorhandler(400)
def bad_request(e):
return render_template('errors.html', code=400, info=_('Bad Request')), 400
@app.errorhandler(403)
def forbidden(e):
return render_template('errors.html', code=403, info=_('Forbidden')), 403
@app.errorhandler(404)
def page_not_found(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html \
or request.path.startswith('/api'):
response = jsonify(code=404, message='The requested URL was not found on the server.')
response.status_code = 404
return response
return render_template('errors.html', code=404, info=_('Page Not Found')), 404
@app.errorhandler(405)
def method_not_allowed(e):
response = jsonify(code=405, message='The method is not allowed for the requested URL.')
response.status_code = 405
return response
@app.errorhandler(500)
def internal_server_error(e):
if request.accept_mimetypes.accept_json and \
not request.accept_mimetypes.accept_html \
or request.host.startswith('api'):
response = jsonify(code=500, message='An internal server error occurred.')
response.status_code = 500
return response
return render_template('errors.html', code=500, info=_('Server Error')), 500
def register_commands(app):
@app.cli.command()
@click.option('--drop', is_flag=True, help='Create after drop.')
def initdb(drop):
"""Initialize the database."""
if drop:
click.confirm('This operation will delete the database, do you want to continue?', abort=True)
db.drop_all()
click.echo('Drop tables.')
db.create_all()
click.echo('Initialized database.')
@app.cli.group()
def translate():
"""Translation and localization commands."""
pass
@translate.command()
@click.argument('locale')
def init(locale):
"""Initialize a new language."""
if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
raise RuntimeError('extract command failed')
if os.system(
'pybabel init -i messages.pot -d todoism/translations -l ' + locale):
raise RuntimeError('init command failed')
os.remove('messages.pot')
@translate.command()
def update():
"""Update all languages."""
if os.system('pybabel extract -F babel.cfg -k _l -o messages.pot .'):
raise RuntimeError('extract command failed')
if os.system('pybabel update -i messages.pot -d todoism/translations'):
raise RuntimeError('update command failed')
os.remove('messages.pot')
@translate.command()
def compile():
"""Compile all languages."""
if os.system('pybabel compile -d todoism/translations'):
raise RuntimeError('compile command failed')