def make_json_app(): def make_json_error(ex): response = jsonify(message=str(ex)) response.status_code = (ex.code if isinstance(ex, HTTPException) else 500) return response for code in default_exceptions: app.register_error_handler(code, make_json_error)
return render_template('home/register.html', form=form) @login_manager.user_loader def load_user(user_id): return ProviderUser.objects(pk=user_id).first() @babel.localeselector def get_locale(): # if a user is logged in, use the locale from the user settings if current_user and current_user.is_authenticated: lc = current_user.settings.locale return lc if session.get('language'): lang = session['language'] return lang # otherwise try to guess the language from the user accept # header the browser transmits. We support de/fr/en in this # example. The best match wins. return request.accept_languages.best_match(constants.AVAILABLE_LOCALES) def page_not_found(e): return render_template('404.html'), 404 app.register_error_handler(404, page_not_found)
def generate(ec): app.register_error_handler( ec, lambda error: handler(ec, getattr(error_msg, 'MSG_{}'.format(ec)), error))
import werkzeug from flask import render_template from app import app @app.errorhandler(werkzeug.exceptions.NotFound) def handle_not_found(e): return render_template("404.html"), 404 @app.errorhandler(FileNotFoundError) def handle_file_not_found(e): return render_template("404.html"), 404 app.register_error_handler(404, handle_not_found) app.register_error_handler(404, handle_file_not_found)
from flask import request, jsonify from werkzeug.exceptions import abort from app import app from app.exeption import * def page_not_found(err_description): return jsonify(error=str(err_description)), 404 def incorrect_input(err_description): return jsonify(error=str(err_description)), 400 app.register_error_handler(404, page_not_found) app.register_error_handler(400, incorrect_input) @app.route('/get_stat', methods=['POST']) def get_statistics(): try: request_body = dict(request.json) date_start = request_body['date_start'] date_end = request_body['date_end'] if 'sort' in request_body: sort = request_body['sort'] else: sort = 'None' from app import interaction return jsonify( interaction.get_statistics(date_start=date_start,
header='403 FORBIDDEN', description='Sinulla ei ole oikeutta pyytämääsi toimintoon...') def not_found(e): return render_template('error.html', title='ERROR404', header='404 NOT FOUND', description='Etsimääsi resurssia ei löytynyt...') def unsupported_media_type(e): return render_template('error.html', title='ERROR415', header='415 UNSUPPORTED MEDIA TYPE', description='Lähettämääsi mediatyyppiä ei tueta...') def internal_server_error(e): return render_template('error.html', title='ERROR500', header='500 INTERNAL SERVER ERROR', description='Serverillä meni jotain pieleen...') app.register_error_handler(400, bad_request) app.register_error_handler(403, forbidden) app.register_error_handler(404, not_found) app.register_error_handler(415, unsupported_media_type) app.register_error_handler(500, internal_server_error)
from flask import render_template from app import app # , db @app.errorhandler(404) def not_found_error(error): return render_template('errors/404.html'), 404 @app.errorhandler(500) def internal_error(error): # db.session.rollback() return render_template('errors/500.html'), 500 app.register_error_handler(404, not_found_error) app.register_error_handler(500, internal_error)
def create_app(config_filename): app = Flask(__name__) app.register_error_handler(404, internal_server_error) return app
def handle_db_session_when_exc(): try: g.db_session.rollback() finally: g.db_session.close() g.DBSession.remove() def handle_exc(error): handle_db_session_when_exc() return jsonify({"msg": u"系统内部错误", "code": -1}), 500 for cls in HTTPException.__subclasses__(): app.register_error_handler(cls, handle_exc) @app.errorhandler(GulDanException) def handle_guldan_exception(error): handle_db_session_when_exc() return make_error_response(error, -1) @app.errorhandler(NotLoginException) def handle_notlogin_exception(error): handle_db_session_when_exc() return make_error_response(error, -2) def init_admin_user():
def create_app(config_filename): app = Flask(__name__) app.register_error_handler(404, page_not_found) return app
user.confirmed = 1 user.confirm_token = "confirmed" db.session.commit() return redirect("/") @app.route("/unconfirmed") @login_required def unconfirmed(): if current_user.confirmed: return redirect("/") return render_template("unconfirmed.html") @app.route("/telegram_api", methods=["POST"]) def telegram_api(): print(request.json) return {"ok": True} @app.before_request def before_app_request(): if current_user.is_authenticated and current_user.banned is 1: return render_template("banned.html") # if request.MOBILE: # return render_template("mobile.html") app.register_error_handler(404, not_found) app.register_error_handler(403, forbidden)
from app import app, socketio from api import api_routes #ok I need to keep this for some reason '''''' '''''' '''''' '''''' '''''' '''''' ''' Single page website with Vue.js ''' '''''' '''''' '''''' '''''' '''''' '''''' @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): # if app.debug: # Nodejs server # return request.get('http://localhost:8080/{}'.format(path)).text return render_template("index.html") # @app.errorhandler(401) def custom_401(error): # dumb browsers causing unwanted popups print('custom_401') return Response('Login Required', 401, {'WWWAuthenticate': 'CustomBasic realm="Login Required"'}) app.register_error_handler(401, custom_401) app.register_blueprint(api_routes) if __name__ == '__main__': # app.run(debug=True, use_debugger=False, use_reloader=False) socketio.run(app, debug=True)