-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (51 loc) · 1.5 KB
/
app.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
import os
import pytest
import logging
from flask import jsonify
from flask.cli import FlaskGroup
from flask_migrate import Migrate
from marshmallow.exceptions import ValidationError
from apps.api import blueprint
from apps.api.utils import response_with, responses as resp, AuthError, NotFound
from apps import create_app, db
app = create_app(os.getenv('FLASK_ENV', 'default'))
app.register_blueprint(blueprint)
migrate = Migrate(app, db)
cli = FlaskGroup(app)
@cli.command('test')
def test():
"""Runs the unit tests."""
pytest.main(args=['-v', 'tests'])
@app.after_request
def add_header(response):
return response
@app.errorhandler(400)
def bad_request(e):
logging.error(e)
return response_with(resp.BAD_REQUEST_400)
@app.errorhandler(500)
def server_error(e):
db.session.rollback()
logging.error(e)
return response_with(resp.SERVER_ERROR_500)
@app.errorhandler(404)
def not_found(e):
logging.error(e)
return response_with(resp.SERVER_ERROR_404)
@app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
@app.errorhandler(ValidationError)
def handle_validation_error(e):
logging.error(e)
response = jsonify(dict(code='validation_error', message=e.messages))
response.status_code = 400
return response
@app.errorhandler(NotFound)
def handle_not_found(e):
logging.error(e)
return response_with(resp.SERVER_ERROR_404)
if __name__ == '__main__':
cli()