-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapp.py
137 lines (101 loc) · 3.67 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
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
import os
import traceback
from datetime import datetime
import connexion
import sys
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_cors import CORS
from flask_caching import Cache
from connexion.exceptions import ProblemException
import json
from sqlalchemy import MetaData
from util import convert_image_to_data_uri
db = SQLAlchemy()
ma = Marshmallow()
convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
db.Model.metadata = MetaData(naming_convention=convention)
cache = Cache(config={
"DEBUG": True, # some Flask specific configs
"CACHE_TYPE": "simple", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 900 # 15 minutes
})
basedir = os.path.abspath(os.path.dirname(__file__))
# Create the Connexion application instance
connex_app = connexion.App(__name__, specification_dir=basedir)
# For html reports to get a data url to a static file
connex_app.app.jinja_env.globals.update(convert_image_to_data_uri=convert_image_to_data_uri)
def render_exception(exception):
error_string = traceback.format_exc()
print(error_string)
db.session.rollback()
return json.dumps({
"detail": "",
"status": 500,
"title": "Internal Server Error"
}, indent=2), 500
def render_connexion_problem_exception(connexion_exception):
error_string = traceback.format_exc()
print(error_string)
db.session.rollback()
return json.dumps({
"detail": connexion_exception.detail,
"status": connexion_exception.status,
"title": connexion_exception.title,
"code": connexion_exception.args[4]
}, indent=2), connexion_exception.status
def create_app():
connex_app.add_error_handler(Exception, render_exception)
connex_app.add_error_handler(
ProblemException, render_connexion_problem_exception)
# Get the underlying Flask app instance
app = connex_app.app
app.config.from_envvar('ENV_CONFIG')
# Configure the SQLAlchemy part of the app instance
app.config['SQLALCHEMY_ECHO'] = app.config['DEBUG']
if app.config['DATABASE_PLUGIN'] == "sqlite":
# this is for unit tests
app.config['SQLALCHEMY_DATABASE_URI'] = "%s://" % app.config['DATABASE_PLUGIN']
else:
app.config['SQLALCHEMY_DATABASE_URI'] = '%s://%s:%s@%s:%s/%s' % (
app.config['DATABASE_PLUGIN'],
app.config['DATABASE_USERNAME'],
app.config['DATABASE_PASSWORD'],
app.config['DATABASE_HOST'],
app.config['DATABASE_PORT'],
app.config['DATABASE_NAME']
)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Create the SQLAlchemy db instance
db.init_app(app)
# Initialize Marshmallow
ma.init_app(app)
# add CORS support
CORS(app)
# Read the swagger.yml file to configure the endpoints
connex_app.add_api("swagger.yml", strict_validation=True,
validate_responses=False)
@app.context_processor
def inject_to_template():
is_prod_env = False
if app.config.get('PROD_ENV'):
is_prod_env = app.config['PROD_ENV']
from auth import get_user_name
current_user_name = ''
try:
current_user_name = get_user_name()
except:
pass
return dict(
isProdEnv=is_prod_env,
current_user=current_user_name,
current_timestamp=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
)
cache.init_app(app)
return connex_app