diff options
author | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 11:20:27 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2021-10-27 11:20:27 +0800 |
commit | 3fdc1d0b91cf4be7768e940cf930e74005768177 (patch) | |
tree | 31eec01b95a200f8f32e44eb7e6fd566c538fde4 /jimbrella/admin.py | |
parent | 2a5870f529b5defe9154834c252b0d30cccc9c8d (diff) |
Provide exception messages
UmbrellaNotFoundError: serial
UmbrellaValueError: field
Diffstat (limited to 'jimbrella/admin.py')
-rw-r--r-- | jimbrella/admin.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py index df751ee..4c604be 100644 --- a/jimbrella/admin.py +++ b/jimbrella/admin.py @@ -1,6 +1,7 @@ from flask import Blueprint, request, render_template, redirect, url_for from user_agents import parse as user_agent from .database import Database +from .exceptions import * from .config import * bp = Blueprint("admin", __name__, url_prefix="/admin") @@ -25,11 +26,17 @@ def index(): def umbrellas(): umbrellas = db.read() edit = request.args.get("edit") + error = request.args.get("error") + template = ( + "admin/umbrellas_mobile.html" + if user_agent(request.user_agent.string).is_mobile + else "admin/umbrellas_desktop.html" + ) return render_template( - "admin/umbrellas.html", + template, umbrellas=umbrellas, edit=int(edit) if edit else None, - mobile=user_agent(request.user_agent.string).is_mobile, + error=error, ) @@ -48,5 +55,17 @@ def umbrellas_edit(): ]: data[key] = request.form.get(key) - db.update(data) + error = None + try: + db.update(data) + except UmbrellaValueError as e: + # invalid field is in `e.message`. + return redirect( + "{0}?edit={1}&error={2}".format( + url_for("admin.umbrellas"), request.form.get("serial"), e.message + ) + ) + except UmbrellaNotFoundError: + pass # impossible on web console + return redirect(url_for("admin.umbrellas")) |