forked from zap-me/premio_stage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·147 lines (129 loc) · 4.46 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
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
import sys
import logging
import signal
import traceback
import gevent
import gevent.pool
from flask_security.utils import encrypt_password
import web
import utils
import log_utils
from app_core import MISSING_VITAL_SETTING, app, db
from models import user_datastore, User, Role, Category, Permission, Topic
logger = logging.getLogger(__name__)
def teardown_logging():
# fix this bug: https://bugs.python.org/issue21149
logger.handlers.clear()
web.logger_clear()
def add_user(email, password):
email = email.lower()
with app.app_context():
user = User.from_email(db.session, email)
if user:
#logger.error("user already exists")
#return
user.password = encrypt_password(password)
else:
user = user_datastore.create_user(email=email, password=encrypt_password(password))
db.session.commit()
def create_role(name, desc):
role = Role.from_name(db.session, name)
if not role:
role = Role(name=name, description=desc)
else:
role.description = desc
db.session.add(role)
return role
def create_permission(name, desc):
permission = Permission.from_name(db.session, name)
if not permission:
permission = Permission(name=name, description=desc)
else:
permission.description = desc
db.session.add(permission)
return permission
def create_category(name, desc):
category = Category.from_name(db.session, name)
if not category:
category = Category(name=name, description=desc)
else:
category.description = desc
db.session.add(category)
return category
def create_topic(name):
topic = Topic.from_name(db.session, name)
if not topic:
topic = Topic(topic=name)
db.session.add(topic)
return topic
def add_role(email, role_name):
with app.app_context():
user = User.from_email(db.session, email)
if not user:
logger.error("user does not exist")
return
role = create_role(role_name, None)
if role not in user.roles:
user.roles.append(role)
else:
logger.info("user already has role")
db.session.commit()
def sigint_handler(signum, frame):
# pylint: disable=global-statement
global KEEP_RUNNING
logger.warning("SIGINT caught, attempting to exit gracefully")
KEEP_RUNNING = False
def g_exception(greenlet):
# pylint: disable=broad-except
try:
greenlet.get()
except Exception as e:
stack_trace = traceback.format_exc()
msg = f"{e}\n---\n{stack_trace}"
utils.email_exception(logger, msg)
KEEP_RUNNING = True
if __name__ == "__main__":
ch = log_utils.setup_logging(logger, logging.DEBUG)
web.logger_setup(logging.DEBUG, ch)
log_utils.log_socketio_version(logger)
# create tables
db.create_all()
create_role(Role.ROLE_ADMIN, "super user")
create_role(Role.ROLE_PROPOSER, "Can propose payments")
create_role(Role.ROLE_AUTHORIZER, "Can authorize payments")
create_role(Role.ROLE_REFERRAL_CLAIMER, "Can claim referrals")
create_permission(Permission.PERMISSION_RECIEVE, "view account name")
create_permission(Permission.PERMISSION_BALANCE, "view account balance")
create_permission(Permission.PERMISSION_HISTORY, "view account history")
create_permission(Permission.PERMISSION_TRANSFER, "transfer funds")
create_permission(Permission.PERMISSION_ISSUE, "issue funds")
create_category(Category.CATEGORY_MARKETING, "")
create_category(Category.CATEGORY_MISC, "")
create_category(Category.CATEGORY_TESTING, "")
create_category(Category.CATEGORY_REFERRAL, "")
create_topic("test")
create_topic("general")
db.session.commit()
# process commands
if len(sys.argv) > 1:
if sys.argv[1] == "add_user":
add_user(sys.argv[2], sys.argv[3])
if sys.argv[1] == "add_role":
add_role(sys.argv[2], sys.argv[3])
else:
if MISSING_VITAL_SETTING:
logger.error('missing vital setting')
sys.exit(1)
else:
logger.info('got all vital settings')
signal.signal(signal.SIGINT, sigint_handler)
logger.info("starting greenlets")
web_greenlet = web.WebGreenlet(g_exception)
web_greenlet.start()
while KEEP_RUNNING:
gevent.sleep(1)
logger.info("stopping greenlets")
web_greenlet.stop()
logger.info("teardown logging")
teardown_logging()