2018-07-15 12:41:42 +00:00
|
|
|
|
from flask import Flask, render_template, request, abort, redirect, url_for, Markup, escape
|
2018-05-07 10:51:24 +00:00
|
|
|
|
from flask import session as fl_session
|
|
|
|
|
import db
|
|
|
|
|
import bcrypt
|
|
|
|
|
import configparser
|
2018-07-21 17:32:05 +00:00
|
|
|
|
import markdown2
|
2018-07-15 12:41:42 +00:00
|
|
|
|
import datetime
|
|
|
|
|
import telegram
|
2018-07-28 17:58:23 +00:00
|
|
|
|
import query_discord_music
|
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-07-15 12:41:42 +00:00
|
|
|
|
telegram_bot = telegram.Bot(config["Telegram"]["bot_token"])
|
2018-03-12 12:29:12 +00:00
|
|
|
|
|
2018-07-21 20:46:02 +00:00
|
|
|
|
|
|
|
|
|
@app.errorhandler(400)
|
2018-07-21 20:47:52 +00:00
|
|
|
|
def error_400(_=None):
|
2018-07-21 20:50:49 +00:00
|
|
|
|
return render_template("400.html", config=config)
|
2018-07-21 20:46:02 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-21 20:47:52 +00:00
|
|
|
|
@app.route("/400")
|
|
|
|
|
def page_400():
|
|
|
|
|
return error_400()
|
|
|
|
|
|
|
|
|
|
|
2018-07-21 20:46:02 +00:00
|
|
|
|
@app.errorhandler(403)
|
2018-07-21 20:47:52 +00:00
|
|
|
|
def error_403(_=None):
|
2018-07-21 20:50:49 +00:00
|
|
|
|
return render_template("403.html", config=config)
|
2018-07-21 20:46:02 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-21 20:47:52 +00:00
|
|
|
|
@app.route("/403")
|
|
|
|
|
def page_403():
|
|
|
|
|
return error_403()
|
|
|
|
|
|
|
|
|
|
|
2018-07-21 20:46:02 +00:00
|
|
|
|
@app.errorhandler(500)
|
2018-07-21 20:47:52 +00:00
|
|
|
|
def error_500(_=None):
|
2018-07-21 20:50:49 +00:00
|
|
|
|
return render_template("500.html", config=config)
|
2018-07-21 20:46:02 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-21 20:47:52 +00:00
|
|
|
|
@app.route("/500")
|
|
|
|
|
def page_500():
|
|
|
|
|
return error_500()
|
|
|
|
|
|
|
|
|
|
|
2018-01-25 14:24:17 +00:00
|
|
|
|
@app.route("/")
|
2018-06-01 11:45:45 +00:00
|
|
|
|
def page_main():
|
2018-07-24 17:23:19 +00:00
|
|
|
|
if not fl_session.get("user_id"):
|
|
|
|
|
return redirect(url_for("page_login"))
|
|
|
|
|
db_session = db.Session()
|
|
|
|
|
royals = db_session.query(db.Royal).order_by(db.Royal.username).all()
|
|
|
|
|
wiki_pages = db_session.query(db.WikiEntry).order_by(db.WikiEntry.key).all()
|
|
|
|
|
random_diario = db_session.query(db.Diario).order_by(db.func.random()).first()
|
2018-07-26 17:26:03 +00:00
|
|
|
|
next_events = db_session.query(db.Event).filter(db.Event.time > datetime.datetime.now()).order_by(
|
|
|
|
|
db.Event.time).all()
|
2018-07-24 17:23:19 +00:00
|
|
|
|
db_session.close()
|
2018-07-24 17:45:12 +00:00
|
|
|
|
return render_template("main.html", royals=royals, wiki_pages=wiki_pages, entry=random_diario,
|
2018-07-24 17:40:57 +00:00
|
|
|
|
next_events=next_events, config=config, escape=escape)
|
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()
|
2018-06-07 09:47:04 +00:00
|
|
|
|
ow = db_session.query(db.Overwatch).filter_by(royal=user).one_or_none()
|
2018-07-25 19:10:43 +00:00
|
|
|
|
tg = db_session.query(db.Telegram).filter_by(royal=user).one_or_none()
|
2018-07-28 17:58:23 +00:00
|
|
|
|
discord = [dict(row) for row in db_session.execute(query_discord_music.one_query, user.id)]
|
2018-06-04 20:54:12 +00:00
|
|
|
|
db_session.close()
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return render_template("profile.html", ryg=user, css=css, osu=osu, rl=rl, dota=dota, lol=lol, steam=steam, ow=ow,
|
2018-07-25 21:52:38 +00:00
|
|
|
|
tg=tg, discord=discord, config=config)
|
2018-06-04 09:58:27 +00:00
|
|
|
|
|
|
|
|
|
|
2018-05-07 10:51:24 +00:00
|
|
|
|
@app.route("/login")
|
|
|
|
|
def page_login():
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return render_template("login.html", config=config)
|
2018-05-07 10:51:24 +00:00
|
|
|
|
|
|
|
|
|
|
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:
|
2018-07-21 20:46:02 +00:00
|
|
|
|
abort(403)
|
2018-06-01 11:45:45 +00:00
|
|
|
|
return
|
|
|
|
|
if user.password is None:
|
2018-06-05 10:31:11 +00:00
|
|
|
|
fl_session["user_id"] = user.id
|
2018-07-15 16:40:25 +00:00
|
|
|
|
fl_session["username"] = username
|
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-07-15 16:40:25 +00:00
|
|
|
|
fl_session["username"] = username
|
2018-06-01 11:45:45 +00:00
|
|
|
|
return redirect(url_for("page_main"))
|
|
|
|
|
else:
|
2018-07-21 20:46:02 +00:00
|
|
|
|
abort(403)
|
2018-06-01 11:45:45 +00:00
|
|
|
|
return
|
2018-05-07 10:51:24 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-15 16:40:25 +00:00
|
|
|
|
@app.route("/logout")
|
|
|
|
|
def page_logout():
|
|
|
|
|
if "user_id" in fl_session:
|
|
|
|
|
del fl_session["user_id"]
|
|
|
|
|
del fl_session["username"]
|
|
|
|
|
return redirect(url_for("page_main"))
|
|
|
|
|
|
|
|
|
|
|
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-07-21 20:46:02 +00:00
|
|
|
|
return redirect(url_for("page_login"))
|
|
|
|
|
return render_template("password.html", config=config)
|
2018-05-07 10:51:24 +00:00
|
|
|
|
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()
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return redirect(url_for("page_login"))
|
2018-06-01 11:45:45 +00:00
|
|
|
|
|
|
|
|
|
|
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:
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return redirect(url_for("page_login"))
|
|
|
|
|
return render_template("setcss.html", css=ccss.css, config=config)
|
2018-06-05 10:31:11 +00:00
|
|
|
|
elif request.method == "POST":
|
|
|
|
|
if user_id is None:
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return redirect(url_for("page_login"))
|
2018-06-05 13:11:58 +00:00
|
|
|
|
css = request.form.get("css", "")
|
2018-06-07 09:59:15 +00:00
|
|
|
|
if "</style" in css:
|
2018-06-05 13:11:58 +00:00
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
2018-06-13 21:32:26 +00:00
|
|
|
|
@app.route("/game/<name>")
|
|
|
|
|
def page_game(name: str):
|
|
|
|
|
db_session = db.Session()
|
|
|
|
|
if name == "rl":
|
|
|
|
|
game_name = "Rocket League"
|
|
|
|
|
query = db_session.query(db.RocketLeague).join(db.Steam).all()
|
|
|
|
|
elif name == "dota":
|
|
|
|
|
game_name = "Dota 2"
|
|
|
|
|
query = db_session.query(db.Dota).join(db.Steam).all()
|
|
|
|
|
elif name == "lol":
|
|
|
|
|
game_name = "League of Legends"
|
|
|
|
|
query = db_session.query(db.LeagueOfLegends).all()
|
|
|
|
|
elif name == "osu":
|
|
|
|
|
game_name = "osu!"
|
|
|
|
|
query = db_session.query(db.Osu).all()
|
|
|
|
|
elif name == "ow":
|
|
|
|
|
game_name = "Overwatch"
|
|
|
|
|
query = db_session.query(db.Overwatch).all()
|
|
|
|
|
elif name == "steam":
|
|
|
|
|
game_name = "Steam"
|
|
|
|
|
query = db_session.query(db.Steam).all()
|
|
|
|
|
elif name == "ryg":
|
|
|
|
|
game_name = "Royalnet"
|
|
|
|
|
query = db_session.query(db.Royal).all()
|
2018-07-25 21:52:38 +00:00
|
|
|
|
elif name == "tg":
|
|
|
|
|
game_name = "Telegram"
|
|
|
|
|
query = db_session.query(db.Telegram).all()
|
|
|
|
|
elif name == "discord":
|
|
|
|
|
game_name = "Discord"
|
2018-07-28 17:58:23 +00:00
|
|
|
|
query = [dict(row) for row in db_session.execute(query_discord_music.all_query)]
|
2018-07-25 21:52:38 +00:00
|
|
|
|
else:
|
|
|
|
|
abort(404)
|
|
|
|
|
return
|
2018-06-13 21:32:26 +00:00
|
|
|
|
db_session.close()
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return render_template("game.html", minis=query, game_name=game_name, game_short_name=name, config=config)
|
2018-06-13 21:32:26 +00:00
|
|
|
|
|
2018-07-15 12:41:42 +00:00
|
|
|
|
|
|
|
|
|
@app.route("/wiki/<key>", methods=["GET", "POST"])
|
|
|
|
|
def page_wiki(key: str):
|
|
|
|
|
db_session = db.Session()
|
|
|
|
|
wiki_page = db_session.query(db.WikiEntry).filter_by(key=key).one_or_none()
|
|
|
|
|
if request.method == "GET":
|
|
|
|
|
wiki_latest_edit = db_session.query(db.WikiLog).filter_by(edited_key=key) \
|
2018-07-26 17:26:03 +00:00
|
|
|
|
.order_by(db.WikiLog.timestamp.desc()).first()
|
2018-07-15 12:41:42 +00:00
|
|
|
|
db_session.close()
|
|
|
|
|
if wiki_page is None:
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return render_template("wiki.html", key=key, config=config)
|
2018-07-21 17:47:16 +00:00
|
|
|
|
converted_md = Markup(markdown2.markdown(wiki_page.content.replace("<", "<"),
|
2018-07-26 17:26:03 +00:00
|
|
|
|
extras=["spoiler", "tables"]))
|
2018-07-15 12:41:42 +00:00
|
|
|
|
return render_template("wiki.html", key=key, wiki_page=wiki_page, converted_md=converted_md,
|
2018-07-21 20:46:02 +00:00
|
|
|
|
wiki_log=wiki_latest_edit, config=config)
|
2018-07-15 12:41:42 +00:00
|
|
|
|
elif request.method == "POST":
|
|
|
|
|
user_id = fl_session.get('user_id')
|
|
|
|
|
user = db_session.query(db.Royal).filter_by(id=user_id).one()
|
|
|
|
|
if user_id is None:
|
|
|
|
|
db_session.close()
|
2018-07-21 20:46:02 +00:00
|
|
|
|
return redirect(url_for("page_login"))
|
2018-07-15 12:41:42 +00:00
|
|
|
|
if wiki_page is None:
|
|
|
|
|
wiki_page = db.WikiEntry(key=key, content=request.form.get("content"))
|
|
|
|
|
db_session.add(wiki_page)
|
|
|
|
|
db_session.flush()
|
|
|
|
|
else:
|
|
|
|
|
wiki_page.content = request.form.get("content")
|
|
|
|
|
edit_reason = request.form.get("reason")
|
|
|
|
|
new_log = db.WikiLog(editor=user, edited_key=key, timestamp=datetime.datetime.now(), reason=edit_reason)
|
|
|
|
|
db_session.add(new_log)
|
|
|
|
|
db_session.commit()
|
2018-07-15 16:14:39 +00:00
|
|
|
|
try:
|
|
|
|
|
telegram_bot.send_message(config["Telegram"]["main_group"],
|
|
|
|
|
f'ℹ️ La pagina wiki <a href="https://ryg.steffo.eu/wiki/{key}">{key}</a> è stata'
|
|
|
|
|
f' modificata da'
|
|
|
|
|
f' <a href="https://ryg.steffo.eu/profile/{user.username}">{user.username}</a>:'
|
|
|
|
|
f' {"<i>Nessun motivo specificato.</i>" if not edit_reason else edit_reason}\n',
|
|
|
|
|
parse_mode="HTML")
|
2018-07-21 20:46:02 +00:00
|
|
|
|
except Exception:
|
2018-07-15 16:14:39 +00:00
|
|
|
|
pass
|
2018-07-15 12:41:42 +00:00
|
|
|
|
return redirect(url_for("page_wiki", key=key))
|
|
|
|
|
|
|
|
|
|
|
2018-07-22 15:02:42 +00:00
|
|
|
|
@app.route("/diario")
|
|
|
|
|
def page_diario():
|
|
|
|
|
db_session = db.Session()
|
|
|
|
|
diario_entries = db_session.query(db.Diario).all()
|
|
|
|
|
db_session.close()
|
|
|
|
|
return render_template("diario.html", config=config, entries=diario_entries)
|
|
|
|
|
|
|
|
|
|
|
2017-10-25 09:09:06 +00:00
|
|
|
|
if __name__ == "__main__":
|
2017-10-27 09:53:05 +00:00
|
|
|
|
try:
|
2018-07-15 12:41:42 +00:00
|
|
|
|
app.run(host="0.0.0.0", port=1235, debug=__debug__)
|
2017-10-27 09:53:05 +00:00
|
|
|
|
except KeyboardInterrupt:
|
2018-01-25 14:29:38 +00:00
|
|
|
|
pass
|