1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 21:44:21 +00:00
royalnet/webserver.py

130 lines
4.1 KiB
Python
Raw Normal View History

2018-05-07 10:51:24 +00:00
from flask import Flask, render_template, request, abort, redirect, url_for
from flask import session as fl_session
import db
import bcrypt
import configparser
2017-10-25 09:09:06 +00:00
app = Flask(__name__)
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
2018-05-07 10:51:24 +00:00
config = configparser.ConfigParser()
config.read("config.ini")
app.secret_key = config["Flask"]["secret_key"]
2018-03-12 12:29:12 +00:00
2018-01-25 14:24:17 +00:00
@app.route("/")
2018-06-01 11:45:45 +00:00
def page_main():
2018-06-05 10:31:11 +00:00
if fl_session.get("user_id"):
db_session = db.Session()
royals = db_session.query(db.Royal).all()
db_session.close()
return render_template("main.html", royals=royals)
2018-06-01 11:45:45 +00:00
return redirect(url_for("page_login"))
2018-02-02 10:46:27 +00:00
2017-11-07 17:44:00 +00:00
2018-06-04 09:58:27 +00:00
@app.route("/profile/<name>")
def page_profile(name: str):
db_session = db.Session()
user = db_session.query(db.Royal).filter_by(username=name).one_or_none()
if user is None:
db_session.close()
abort(404)
return
2018-06-05 10:31:11 +00:00
css = db_session.query(db.CustomCSS).filter_by(royal=user).one_or_none()
2018-06-05 08:34:59 +00:00
steam = db_session.query(db.Steam).filter_by(royal=user).one_or_none()
2018-06-04 09:58:27 +00:00
osu = db_session.query(db.Osu).filter_by(royal=user).one_or_none()
2018-06-04 11:13:59 +00:00
rl = db_session.query(db.RocketLeague).join(db.Steam).filter_by(royal=user).one_or_none()
2018-06-04 16:08:18 +00:00
dota = db_session.query(db.Dota).join(db.Steam).filter_by(royal=user).one_or_none()
2018-06-04 20:54:12 +00:00
lol = db_session.query(db.LeagueOfLegends).filter_by(royal=user).one_or_none()
db_session.close()
2018-06-05 10:31:11 +00:00
return render_template("profile.html", royal=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam)
2018-06-04 09:58:27 +00:00
2018-05-07 10:51:24 +00:00
@app.route("/login")
def page_login():
return render_template("login.html")
2018-06-01 11:45:45 +00:00
@app.route("/loggedin", methods=["POST"])
2018-05-07 10:51:24 +00:00
def page_loggedin():
2018-06-01 11:45:45 +00:00
username = request.form.get("username", "")
password = request.form.get("password", "")
db_session = db.Session()
user = db_session.query(db.Royal).filter_by(username=username).one_or_none()
db_session.close()
if user is None:
abort(403)
return
if user.password is None:
2018-06-05 10:31:11 +00:00
fl_session["user_id"] = user.id
2018-06-01 11:45:45 +00:00
return redirect(url_for("page_password"))
if bcrypt.checkpw(bytes(password, encoding="utf8"), user.password):
2018-06-05 10:31:11 +00:00
fl_session["user_id"] = user.id
2018-06-01 11:45:45 +00:00
return redirect(url_for("page_main"))
else:
abort(403)
return
2018-05-07 10:51:24 +00:00
@app.route("/password", methods=["GET", "POST"])
def page_password():
2018-06-05 10:31:11 +00:00
user_id = fl_session.get("user_id")
2018-05-07 10:51:24 +00:00
if request.method == "GET":
2018-06-05 10:31:11 +00:00
if user_id is None:
2018-05-07 10:51:24 +00:00
abort(403)
return
return render_template("password.html")
elif request.method == "POST":
2018-06-01 11:45:45 +00:00
new_password = request.form.get("new", "")
2018-05-07 10:51:24 +00:00
db_session = db.Session()
2018-06-05 10:31:11 +00:00
user = db_session.query(db.Royal).filter_by(id=user_id).one()
2018-06-01 11:45:45 +00:00
if user.password is None:
2018-05-07 10:51:24 +00:00
user.password = bcrypt.hashpw(bytes(new_password, encoding="utf8"), bcrypt.gensalt())
db_session.commit()
db_session.close()
2018-06-01 11:45:45 +00:00
return redirect(url_for("page_main"))
2018-05-07 10:51:24 +00:00
else:
db_session.close()
abort(403)
2018-06-01 11:45:45 +00:00
return
2018-06-05 10:31:11 +00:00
@app.route("/setcss", methods=["GET", "POST"])
def page_setcss():
user_id = fl_session.get("user_id")
db_session = db.Session()
ccss = db_session.query(db.CustomCSS).filter_by(royal_id=user_id).one_or_none()
if request.method == "GET":
db_session.close()
if user_id is None:
abort(403)
return
return render_template("setcss.html", css=ccss.css)
elif request.method == "POST":
if user_id is None:
abort(403)
return
2018-06-05 13:11:58 +00:00
css = request.form.get("css", "")
if "<style" in css:
abort(400)
return
2018-06-05 10:31:11 +00:00
if ccss is None:
2018-06-05 13:11:58 +00:00
ccss = db.CustomCSS(royal_id=user_id, css=css)
2018-06-05 10:31:11 +00:00
db_session.add(ccss)
else:
ccss.css = request.form.get("css", "")
db_session.commit()
royal = db_session.query(db.Royal).filter_by(id=user_id).one()
db_session.close()
return redirect(url_for("page_profile", name=royal.username))
2017-10-25 09:09:06 +00:00
if __name__ == "__main__":
2017-10-27 09:53:05 +00:00
try:
2018-05-26 08:50:54 +00:00
app.run(host="0.0.0.0", port=1234, debug=__debug__)
2017-10-27 09:53:05 +00:00
except KeyboardInterrupt:
2018-01-25 14:29:38 +00:00
pass