catch flash

This commit is contained in:
Daniel Tsvetkov 2020-04-28 19:42:24 +02:00
parent 53aa2b6527
commit a026ccf4b9
3 changed files with 31 additions and 5 deletions

View File

@ -5,13 +5,12 @@ import re
from json import JSONEncoder from json import JSONEncoder
from uuid import uuid4 from uuid import uuid4
from flask_sqlalchemy import SQLAlchemy from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE
from flask_security import RoleMixin, UserMixin
from flask_security import Security, SQLAlchemyUserDatastore from flask_security import Security, SQLAlchemyUserDatastore
from flask_sqlalchemy import SQLAlchemy
from markupsafe import escape, Markup from markupsafe import escape, Markup
from sqlalchemy.ext.declarative import declared_attr, DeclarativeMeta from sqlalchemy.ext.declarative import declared_attr, DeclarativeMeta
from flask_security import RoleMixin, UserMixin
from config import SQLALCHEMY_DATABASE_URI, MAKEDIRS, DATABASE_FILE
from sqlalchemy.orm.collections import InstrumentedList from sqlalchemy.orm.collections import InstrumentedList
from sqlalchemy_utils import Choice from sqlalchemy_utils import Choice
from tww.tww import solve_query, resolve_timezone, dt_tz_translation, time_ago from tww.tww import solve_query, resolve_timezone, dt_tz_translation, time_ago
@ -64,7 +63,7 @@ class ModelController(ModelJsonEncoder):
__mapper_args__ = {'always_refresh': True} __mapper_args__ = {'always_refresh': True}
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
uuid = db.Column(db.Unicode, default=str(uuid4())) uuid = db.Column(db.Unicode, index=True, default=lambda: str(uuid4()))
_excluded_serialization = [] _excluded_serialization = []

View File

@ -10,6 +10,7 @@ from flask_table import Table, LinkCol, Col
from oshipka.persistance import db from oshipka.persistance import db
from oshipka.webapp import test_bp, oshipka_bp from oshipka.webapp import test_bp, oshipka_bp
from oshipka.webapp.views import catch_flash
TASKS = {} TASKS = {}
@ -120,3 +121,9 @@ def stream(task_uuid):
return jsonify({"error": "no task with uuid {}".format(task_uuid)}), 404 return jsonify({"error": "no task with uuid {}".format(task_uuid)}), 404
return Response(tail(os.path.join(TASKS_BUF_DIR, task_uuid)), content_type='text/event-stream') return Response(tail(os.path.join(TASKS_BUF_DIR, task_uuid)), content_type='text/event-stream')
return jsonify({"error": "Request has to contain 'Accept: text/event-stream' header"}), 400 return jsonify({"error": "Request has to contain 'Accept: text/event-stream' header"}), 400
@test_bp.route("/div_error")
@catch_flash
def test_error():
return 1 / 0

View File

@ -1,3 +1,4 @@
from functools import wraps
from uuid import uuid4 from uuid import uuid4
import inflect import inflect
@ -138,3 +139,22 @@ class ModelView(object):
self.app.add_url_rule(url, methods=["GET", "POST"], self.app.add_url_rule(url, methods=["GET", "POST"],
endpoint='delete_{}'.format(self.model_name), endpoint='delete_{}'.format(self.model_name),
view_func=delete_view(self)) view_func=delete_view(self))
def catch_flash(f):
@wraps(f)
def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as e:
flash(str(e), "error")
serialized_form = serialize_form()
if '_next' in serialized_form:
_next = serialized_form.pop('_next')
elif request.referrer and request.referrer != request.path:
_next = request.referrer
else:
_next = url_for('home')
return redirect(_next)
return inner