-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathrun.py
73 lines (54 loc) · 2.2 KB
/
run.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
from app import app, cli
from app.admin import run_flask_admin
from app.models import Category, Language, Resource, db, Role, User
import os
from flask_security import Security, SQLAlchemyUserDatastore, utils
from flask import url_for
from flask_admin import helpers as admin_helpers
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from prometheus_client import make_wsgi_app
from sqlalchemy import event
admin = run_flask_admin(app)
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
if __name__ == "__main__":
app.run()
cli.register(app, db)
# Add prometheus wsgi middleware to route /metrics requests
app_dispatch = DispatcherMiddleware(app, {
'/metrics': make_wsgi_app()
})
# @event.listens_for(User.password, 'set', retval=True)
# def hash_user_password(target, value, oldvalue, initiator):
# """Encrypts password when new admin created in User View"""
# if value != oldvalue:
# return utils.encrypt_password(value)
# return value
@security.context_processor
def security_context_processor():
return dict(
admin_base_template=admin.base_template,
admin_view=admin.index_view,
h=admin_helpers,
get_url=url_for
)
@app.shell_context_processor
def make_shell_context():
return {'db': db, 'Resource': Resource, 'Category': Category, 'Language': Language,
'User': User, 'Role': Role}
@app.before_first_request
def before_first_request():
""" Adds admin/user roles and default admin account and password if none exists"""
db.create_all()
user_datastore.find_or_create_role(name='admin', description='Administrator')
user_datastore.find_or_create_role(name='user', description='End User')
admin_email = os.environ.get('ADMIN_EMAIL', "[email protected]")
admin_password = os.environ.get('ADMIN_PASSWORD', 'password')
encrypted_password = utils.encrypt_password(admin_password)
if not user_datastore.get_user(admin_email):
user_datastore.create_user(email=admin_email, password=encrypted_password)
db.session.commit()
user_datastore.add_role_to_user(admin_email, 'admin')
db.session.commit()
if __name__ == "__main__":
app.run()