From 332e87120f8497d5a2b32c6097096df2bf1df9a7 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 8 Jul 2019 03:03:13 +0300 Subject: [PATCH] Add nice looking errors --- .../web/royalprints/diarioview/__init__.py | 7 +++- royalnet/web/royalprints/home/__init__.py | 2 +- royalnet/web/royalprints/mcstatus/__init__.py | 5 ++- royalnet/web/royalprints/profile/__init__.py | 5 ++- royalnet/web/royalprints/tglogin/__init__.py | 9 +++-- .../tglogin/templates/tglogin_index.html | 40 ++++++++----------- royalnet/web/royalprints/wikiedit/__init__.py | 13 +++--- royalnet/web/royalprints/wikiview/__init__.py | 7 ++-- royalnet/web/shortcuts.py | 5 +++ royalnet/web/static/ryg.css | 8 ++++ royalnet/web/static/ryg.less | 12 ++++++ royalnet/web/templates/error.html | 23 +++++++++++ 12 files changed, 94 insertions(+), 42 deletions(-) create mode 100644 royalnet/web/shortcuts.py create mode 100644 royalnet/web/templates/error.html diff --git a/royalnet/web/royalprints/diarioview/__init__.py b/royalnet/web/royalprints/diarioview/__init__.py index b3c81030..2cb5521b 100644 --- a/royalnet/web/royalprints/diarioview/__init__.py +++ b/royalnet/web/royalprints/diarioview/__init__.py @@ -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) diff --git a/royalnet/web/royalprints/home/__init__.py b/royalnet/web/royalprints/home/__init__.py index e3dcb59d..b5739c11 100644 --- a/royalnet/web/royalprints/home/__init__.py +++ b/royalnet/web/royalprints/home/__init__.py @@ -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') diff --git a/royalnet/web/royalprints/mcstatus/__init__.py b/royalnet/web/royalprints/mcstatus/__init__.py index 50c723a5..06b39912 100644 --- a/royalnet/web/royalprints/mcstatus/__init__.py +++ b/royalnet/web/royalprints/mcstatus/__init__.py @@ -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 diff --git a/royalnet/web/royalprints/profile/__init__.py b/royalnet/web/royalprints/profile/__init__.py index 6c146e5d..46d54dd0 100644 --- a/royalnet/web/royalprints/profile/__init__.py +++ b/royalnet/web/royalprints/profile/__init__.py @@ -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) diff --git a/royalnet/web/royalprints/tglogin/__init__.py b/royalnet/web/royalprints/tglogin/__init__.py index 1fac857f..64fab63a 100644 --- a/royalnet/web/royalprints/tglogin/__init__.py +++ b/royalnet/web/royalprints/tglogin/__init__.py @@ -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, diff --git a/royalnet/web/royalprints/tglogin/templates/tglogin_index.html b/royalnet/web/royalprints/tglogin/templates/tglogin_index.html index 5752ffc8..373d0ffc 100644 --- a/royalnet/web/royalprints/tglogin/templates/tglogin_index.html +++ b/royalnet/web/royalprints/tglogin/templates/tglogin_index.html @@ -12,32 +12,26 @@
- {% if request.url_root == "https://ryg.steffo.eu/" %} -

- Facendo il login su questo sito, acconsenti a ricevere due biscottini che memorizzino l'account con cui hai fatto il login.
-

-

- Essi avranno il seguente formato: -

-
session["royal"] = {
-    "uid": [il tuo id Royalnet]
-    "username": [il tuo username Royalnet],
-    "avatar": [il tuo avatar Royalnet],
-    "role": [il tuo ruolo Royalnet]
+            

+ Facendo il login su questo sito, acconsenti a ricevere due biscottini che memorizzino l'account con cui hai fatto il login.
+

+

+ Essi avranno il seguente formato: +

+
session["royal"] = {
+"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]
-

- -

-

- Nota: per fare il login, devi aver syncato il tuo account sul gruppo Telegram Royal Games! -

- {% else %} -

- Il login con Telegram non è attivo su questo dominio. -

- {% endif %} +

+ +

+

+ Nota: per fare il login, devi aver syncato il tuo account sul gruppo Telegram Royal Games! +

{% endblock %} diff --git a/royalnet/web/royalprints/wikiedit/__init__.py b/royalnet/web/royalprints/wikiedit/__init__.py index d903c8ee..acaeea5c 100644 --- a/royalnet/web/royalprints/wikiedit/__init__.py +++ b/royalnet/web/royalprints/wikiedit/__init__.py @@ -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("//", 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, diff --git a/royalnet/web/royalprints/wikiview/__init__.py b/royalnet/web/royalprints/wikiview/__init__.py index 70a77e97..e924221a 100644 --- a/royalnet/web/royalprints/wikiview/__init__.py +++ b/royalnet/web/royalprints/wikiview/__init__.py @@ -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) diff --git a/royalnet/web/shortcuts.py b/royalnet/web/shortcuts.py new file mode 100644 index 00000000..fdb9c466 --- /dev/null +++ b/royalnet/web/shortcuts.py @@ -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 diff --git a/royalnet/web/static/ryg.css b/royalnet/web/static/ryg.css index 2ae3e5f8..8c163969 100644 --- a/royalnet/web/static/ryg.css +++ b/royalnet/web/static/ryg.css @@ -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; diff --git a/royalnet/web/static/ryg.less b/royalnet/web/static/ryg.less index b910d3a7..60672126 100644 --- a/royalnet/web/static/ryg.less +++ b/royalnet/web/static/ryg.less @@ -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. diff --git a/royalnet/web/templates/error.html b/royalnet/web/templates/error.html new file mode 100644 index 00000000..05025081 --- /dev/null +++ b/royalnet/web/templates/error.html @@ -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 %} \ No newline at end of file