1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add nice looking errors

This commit is contained in:
Steffo 2019-07-08 03:03:13 +03:00
parent 0fa2cec5c2
commit 332e87120f
12 changed files with 94 additions and 42 deletions

View file

@ -2,7 +2,8 @@
import flask as f
import os
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from ....database.tables import Royal, Diario
@ -16,6 +17,8 @@ rp = Royalprint("diarioview", __name__, url_prefix="/diario", template_folder=tm
def diarioview_page(page):
alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
if page < 1:
return "Page should be >1", 404
return error(404, "Il numero di pagina deve essere maggiore di 0.")
entries = alchemy_session.query(alchemy.Diario).order_by(alchemy.Diario.diario_id.desc()).offset((page - 1) * 1000).limit(1000).all()
if len(entries) == 0:
return error(404, "Non ci sono righe di diario in questa pagina (e in tutte le successive).")
return f.render_template("diarioview_page.html", page=page, entries=entries)

View file

@ -1,7 +1,7 @@
"""Homepage :py:class:`royalnet.web.Royalprint` of the Royal Games website."""
import flask as f
import os
from ... import Royalprint
from ...royalprint import Royalprint
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')

View file

@ -2,7 +2,8 @@
import os
import flask as f
import socket
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from mcstatus import MinecraftServer
@ -22,7 +23,7 @@ def mcstatus_index(server_str: str):
except (socket.timeout, ConnectionRefusedError, OSError):
query = None
except socket.gaierror:
return "No such address", 404
return error(400, "L'indirizzo richiesto non è valido.")
except (socket.timeout, ConnectionRefusedError, OSError):
status = None
query = None

View file

@ -2,7 +2,8 @@
import flask as f
import os
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from ....database.tables import *
@ -25,5 +26,5 @@ def profile_by_username(username):
alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
royal = alchemy_session.query(alchemy.Royal).filter_by(username=username).one_or_none()
if royal is None:
return "No such user", 404
return error(404, "Non esiste nessun utente con l'username richiesto.")
return f.render_template("profile_page.html", royal=royal)

View file

@ -4,7 +4,8 @@ import hashlib
import hmac
import datetime
import os
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from ....database.tables import Royal, Telegram
@ -15,6 +16,8 @@ rp = Royalprint("tglogin", __name__, url_prefix="/login/telegram", required_tabl
@rp.route("/")
def tglogin_index():
if f.request.url_root != "https://ryg.steffo.eu/":
return error(404, "Il login tramite Telegram non è possibile su questo dominio.")
f.session.pop("royal", None)
return f.render_template("tglogin_index.html")
@ -32,10 +35,10 @@ def tglogin_done():
secret_key = hashlib.sha256(bytes(f.current_app.config["TG_AK"], encoding="ascii")).digest()
hex_data = hmac.new(key=secret_key, msg=data_check, digestmod="sha256").hexdigest()
if hex_data != f.request.args["hash"]:
return "Invalid authentication", 403
return error(400, "L'autenticazione è fallita: l'hash ricevuto non coincide con quello calcolato.")
tg_user = alchemy_session.query(alchemy.Telegram).filter(alchemy.Telegram.tg_id == f.request.args["id"]).one_or_none()
if tg_user is None:
return "No such telegram", 404
return error(404, "L'account Telegram con cui hai fatto il login non è connesso a nessun account Royal Games. Se sei un membro Royal Games, assicurati di aver syncato con il bot il tuo account di Telegram!")
royal_user = tg_user.royal
f.session["royal"] = {
"uid": royal_user.uid,

View file

@ -12,7 +12,6 @@
</span>
</div>
<div class="bot">
{% if request.url_root == "https://ryg.steffo.eu/" %}
<p>
Facendo il login su questo sito, acconsenti a ricevere due <abbr title="cookie">biscottini</abbr> che memorizzino l'account con cui hai fatto il login.<br>
</p>
@ -20,10 +19,10 @@
Essi avranno il seguente formato:
</p>
<pre><code>session["royal"] = {
"uid": [il tuo id Royalnet]
"username": [il tuo username Royalnet],
"avatar": [il tuo avatar Royalnet],
"role": [il tuo ruolo Royalnet]
"uid": [il tuo id Royalnet]
"username": [il tuo username Royalnet],
"avatar": [il tuo avatar Royalnet],
"role": [il tuo ruolo Royalnet]
}
session["login_date"] = [la data e l'ora di adesso]</code></pre>
@ -33,11 +32,6 @@ session["login_date"] = [la data e l'ora di adesso]</code></pre>
<p>
Nota: per fare il login, devi aver <code>sync</code>ato il tuo account sul gruppo Telegram Royal Games!
</p>
{% else %}
<p>
Il login con Telegram non è attivo su questo dominio.
</p>
{% endif %}
</div>
</div>
{% endblock %}

View file

@ -4,7 +4,8 @@ import uuid
import os
import datetime
import difflib
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from ....database.tables import Royal, WikiPage, WikiRevision
@ -16,7 +17,7 @@ rp = Royalprint("wikiedit", __name__, url_prefix="/wiki/edit", template_folder=t
@rp.route("/newpage", methods=["GET", "POST"])
def wikiedit_newpage():
if "royal" not in f.session:
return "Please login to edit wiki pages", 403
return error(403, "Devi aver effettuato il login per creare pagine wiki.")
if f.request.method == "GET":
return f.render_template("wikiedit_page.html", page=None)
@ -24,7 +25,7 @@ def wikiedit_newpage():
elif f.request.method == "POST":
fd = f.request.form
if not ("title" in fd and "content" in fd and "css" in fd):
return "Missing field", 400
return error(400, "Uno dei campi obbligatori non è stato compilato. Controlla e riprova!")
alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
page = alchemy.WikiPage(page_id=uuid.uuid4(),
title=fd["title"],
@ -47,12 +48,12 @@ def wikiedit_newpage():
@rp.route("/<uuid:page_id>/<title>", methods=["GET", "POST"])
def wikiedit_by_id(page_id: uuid.UUID, title: str):
if "royal" not in f.session:
return "Please login to edit wiki pages", 403
return error(403, "Devi aver effettuato il login per modificare pagine wiki.")
alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none()
if page is None:
return "No such page", 404
return error(404, "La pagina che stai cercando di modificare non esiste.")
if f.request.method == "GET":
return f.render_template("wikiedit_page.html", page=page)
@ -60,7 +61,7 @@ def wikiedit_by_id(page_id: uuid.UUID, title: str):
elif f.request.method == "POST":
fd = f.request.form
if not ("title" in fd and "content" in fd and "css" in fd):
return "Missing field", 400
return error(400, "Uno dei campi obbligatori non è stato compilato. Controlla e riprova!")
# Create new revision
revision = alchemy.WikiRevision(revision_id=uuid.uuid4(),
page=page,

View file

@ -5,7 +5,8 @@ import markdown2
import re
import uuid
import os
from ... import Royalprint
from ...royalprint import Royalprint
from ...shortcuts import error
from ....database.tables import Royal, WikiPage, WikiRevision
@ -47,7 +48,7 @@ def prepare_page(page):
parsed_content=f.Markup(page.content),
css=page.css)
else:
return "Format not available", 500
return error(500, f"Non esiste nessun handler in grado di preparare pagine con il formato {page.format}.")
@rp.route("/")
@ -63,5 +64,5 @@ def wikiview_by_id(page_id: uuid.UUID, title: str):
alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"]
page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none()
if page is None:
return "No such page", 404
return error(404, f"La pagina richiesta non esiste.")
return prepare_page(page)

View file

@ -0,0 +1,5 @@
import flask as f
def error(code, reason):
return f.render_template("error.html", title=f"Errore {code}", reason=reason), code

View file

@ -353,6 +353,14 @@ table tbody tr:last-child td:last-child {
padding: 8px;
border-radius: 0 0 4px 4px;
}
.doublebox.doublebox-red .top {
background-color: rgba(255, 125, 125, 0.2);
color: #ff7d7d;
}
.doublebox.doublebox-red .bot {
background-color: rgba(255, 125, 125, 0.1);
color: #ff7d7d;
}
.multirow {
list-style-type: none;
margin-top: 0;

View file

@ -433,6 +433,18 @@ table {
padding: 8px;
border-radius: 0 0 4px 4px;
}
&.doublebox-red {
.top {
background-color: fade(@pastel-red, 20%);
color: @pastel-red;
}
.bot {
background-color: fade(@pastel-red, 10%);
color: @pastel-red;
}
}
}
//This is something about the sidebar... better leave it alone.

View file

@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block title %}
Errore
{% endblock %}
{% block content %}
<div class="error">
<div class="doublebox doublebox-red">
<div class="top">
<span class="left">Errore!</span>
</div>
<div class="bot">
<h1>
{{ title }}
</h1>
<p>
{{ reason }}
</p>
</div>
</div>
</div>
{% endblock %}