diff --git a/oshipka/persistance/__init__.py b/oshipka/persistance/__init__.py index 5d43145..a249a9f 100644 --- a/oshipka/persistance/__init__.py +++ b/oshipka/persistance/__init__.py @@ -5,13 +5,12 @@ import re from json import JSONEncoder 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_sqlalchemy import SQLAlchemy from markupsafe import escape, Markup 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_utils import Choice 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} 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 = [] diff --git a/oshipka/webapp/async_routes.py b/oshipka/webapp/async_routes.py index 5f45c56..a9b8dbb 100644 --- a/oshipka/webapp/async_routes.py +++ b/oshipka/webapp/async_routes.py @@ -10,6 +10,7 @@ from flask_table import Table, LinkCol, Col from oshipka.persistance import db from oshipka.webapp import test_bp, oshipka_bp +from oshipka.webapp.views import catch_flash TASKS = {} @@ -120,3 +121,9 @@ def stream(task_uuid): 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 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 diff --git a/oshipka/webapp/views.py b/oshipka/webapp/views.py index f393013..f2e1ae0 100644 --- a/oshipka/webapp/views.py +++ b/oshipka/webapp/views.py @@ -1,3 +1,4 @@ +from functools import wraps from uuid import uuid4 import inflect @@ -138,3 +139,22 @@ class ModelView(object): self.app.add_url_rule(url, methods=["GET", "POST"], endpoint='delete_{}'.format(self.model_name), 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