-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.py
122 lines (90 loc) · 3 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
from flask import Flask, render_template, session, redirect, url_for, request, g, flash, jsonify
import redis
import socket
import logging
import config
from utils import misc, select
from data.database import db
import data
# Blueprints
from routes import api, admin, teams, users, challenges, tickets, scoreboard, shell
if config.production:
logging.basicConfig(level=logging.INFO)
else:
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
app.secret_key = config.secret.key
app.register_blueprint(api.api)
app.register_blueprint(admin.admin)
app.register_blueprint(teams.teams)
app.register_blueprint(users.users)
app.register_blueprint(challenges.challenges)
app.register_blueprint(tickets.tickets)
app.register_blueprint(scoreboard.scoreboard)
app.register_blueprint(shell.shell)
@app.before_request
def make_info_available():
if "user_id" in session:
g.logged_in = True
current_user = data.user.get_user(id=session["user_id"])
if current_user is not None:
g.user = current_user
g.user_restricts = g.user.restricts.split(",")
g.team = g.user.team
g.team_restricts = g.team.restricts.split(",")
else:
g.logged_in = False
session.pop("user_id")
return render_template("login.html")
else:
g.logged_in = False
@app.context_processor
def scoreboard_variables():
var = dict(config=config, select=select)
if "user_id" in session:
var["logged_in"] = True
var["user"] = g.user
var["team"] = g.team
var["notifications"] = data.notification.get_notifications(team=g.team)
else:
var["logged_in"] = False
var["notifications"] = []
return var
@app.route('/')
def root():
if g.logged_in:
return redirect(url_for('teams.dashboard'))
return redirect(url_for('users.register'))
@app.route('/chat/')
def chat():
return render_template("chat.html")
# Debug
@app.route('/debug/')
def debug_app():
return jsonify(hostname=socket.gethostname())
# Manage Peewee database sessions and Redis
@app.before_request
def before_request():
db.connect()
g.redis = redis.StrictRedis(host=config.redis.host, port=config.redis.port, db=config.redis.db)
g.connected = True
@app.teardown_request
def teardown_request(exc):
if getattr(g, 'connected', False):
db.close()
g.redis.connection_pool.disconnect()
# CSRF things
@app.before_request
def csrf_protect():
csrf_exempt = ['/teamconfirm/']
if request.method == "POST":
token = session.get('_csrf_token', None)
if (not token or token != request.form["_csrf_token"]) and request.path not in csrf_exempt:
return "Invalid CSRF token!"
def generate_csrf_token():
if '_csrf_token' not in session:
session['_csrf_token'] = misc.generate_random_string(64)
return session['_csrf_token']
app.jinja_env.globals['csrf_token'] = generate_csrf_token
if __name__ == '__main__':
app.run(debug=True, port=8001)