-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
127 lines (99 loc) · 3.42 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
from flask import Flask, jsonify, request, url_for
from flask_admin import Admin
#from flask_admin.contrib.peewee import ModelView
from flask_admin import helpers as admin_helpers
from flask_admin.menu import MenuLink
from flask_security import Security, PeeweeUserDatastore, \
UserMixin, RoleMixin, login_required, user_registered
from db import Sign, URL, SignURL, User, Role, UserRoles, db
from adminViews import SignView, AuthModelView, AuthAdminIndexView, UserView
from config import *
from datetime import datetime
app = Flask(__name__)
app.secret_key = SECRET_KEY
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_PASSWORD_HASH'] = 'sha512_crypt'
app.config['SECURITY_PASSWORD_SALT'] = SECRET_KEY
user_datastore = PeeweeUserDatastore(db, User, Role, UserRoles)
security = Security(app, user_datastore)
def shutdown_flask():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
admin = Admin(app, name='SignMan', template_mode='bootstrap3', index_view=AuthAdminIndexView())
admin.add_view(SignView(Sign))
admin.add_view(AuthModelView(URL))
admin.add_view(AuthModelView(SignURL))
admin.add_view(UserView(User))
admin.add_link(MenuLink("Logout", endpoint='security.logout'))
@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
)
@user_registered.connect_via(app)
def activateFirstUser(sender, user, confirm_token, **extra):
if User.select().count() == 1:
user.active = True
else:
user.active = False
user.save()
@app.route('/')
def index():
return '''<html><body>
Welcome to Sign Man<p>
<a href=./admin>Admin interface </a>
</html></body>
'''
@app.route('/api/v1/config/<string:token>', methods=['GET'])
def get_config(token):
lines = []
try:
sign = Sign.get(token=token)
url = sign.getCurrentURL()
sign.last_ip=request.remote_addr
sign.save()
except Sign.DoesNotExist:
lines.append("# Sign not found")
url = "%s/nonregistered/%s" % (SIGNMAN_BASE_URL, token)
if auto_add_signs:
lines.append("... added to database")
newSign = Sign(token=token, name="new-%s" % datetime.now().strftime("%Y%m%d-%H%M%S"), last_ip=request.remote_addr)
newSign.save()
except URL.DoesNotExist:
lines.append("# No URL for sign found")
url = DEFAULT_URL
lines.append("URL %s" % url)
lines.append("NODE %s" % token)
lines.append("")
return "\n".join(lines)
@app.route('/api/v1/sign/<string:token>', methods=['GET'])
def get_urls(token):
try:
sign = Sign.get(token=token)
except:
return "sign not found"
return jsonify(sign.activeURLs())
@app.route('/nonregistered/<string:token>', methods=['GET'])
def non_registered_screen(token):
return '''<html><body>
Connected to SignMan<p>
Sign Token: %s
</html></body>''' % token
@app.route('/health', methods=['GET'])
def health_check():
return "OK"
@app.route('/quitquitquit', methods=['POST'])
@app.route('/abortabortabort', methods=['POST'])
def shutdown():
if shutdown_endpoints_enabled:
shutdown_flask()
return "Goodbye"
else:
return "Nope"
if __name__ == '__main__':
app.run(debug=debug)