__init__.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from flask import Flask, make_response, request, jsonify, render_template, flash
  2. from config import Config
  3. from app.extensions import db, extract_exc
  4. from app.models.users import tbl_users
  5. from flask_login import LoginManager
  6. # flask app factory function/instantiator
  7. def create_app(config_class=Config):
  8. app = Flask(__name__)
  9. app.config.from_object(config_class)
  10. # Initialize Flask extensions here
  11. db.init_app(app)
  12. # Register blueprints here
  13. from app.main import bp as main_bp
  14. app.register_blueprint(main_bp, url_prefix="/cv_db")
  15. from app.auth import bp as auth_bp
  16. app.register_blueprint(auth_bp, url_prefix="/cv_db/auth")
  17. @app.route("/cv_db/test/")
  18. def test_page():
  19. return '<h1>Testing the Flask Application Factory Pattern</h1>'
  20. # end of test_page view function for route /dummy_api/test/
  21. login_manager = LoginManager()
  22. login_manager.login_view = 'auth.login'
  23. login_manager.init_app(app)
  24. @login_manager.user_loader
  25. def load_user(user_id):
  26. # since the user_id is just the primary key of our user table, use it in the query for the user
  27. return db.session.get(tbl_users, int(user_id))
  28. # end of load_user function
  29. @app.errorhandler(404)
  30. def page_not_found(e):
  31. s_path = str(request.path)
  32. return render_template("errors/404.html", js=True, s_alert="", path=s_path), 404
  33. # end of page_not_found function
  34. @app.errorhandler(Exception)
  35. def page_exception(e):
  36. d_exc = extract_exc()
  37. # ['i_lineno', 's_filename', 's_except', 's_tb', 's_additional']
  38. l_tb = str(d_exc["s_tb"]).strip().split("\n")
  39. l_tb.reverse()
  40. if len(l_tb)>2: l_tb = l_tb[:3]
  41. s_tb = " | ".join(l_tb)
  42. s_path = str(request.path)
  43. s_logging = f"<ul><li>date-time: {s_dt}</li>\n<li>line no.: {d_exc['i_lineno']}</li>\n<li>filename: {d_exc['s_filename']}</li>\n<li>except: {d_exc['s_except']}</li>\n<li>additional: {d_exc['s_additional']}</li>\n</ul>" if isinstance(d_exc, dict) else f"date-time: {s_dt}\n{s_path}\n" + str(d_exc["s_except"])
  44. d_out = {"url": request.url.replace(request.url_root, ""), "file": d_exc["s_filename"], "line no.": d_exc["i_lineno"], "exception": d_exc["s_except"], "traceback": s_tb}
  45. d_exc["traceback"] = d_exc["traceback"].split("|")
  46. return render_template("errors/500.html", js=True, s_alert="", exc=d_exc, html_content=s_logging), 500
  47. # end of page_exception function
  48. @app.template_filter("date_time_format")
  49. def my_dt_format(dt_date_time):
  50. return dt_date_time.strftime("%Y-%m-%d %H:%M")
  51. # end of my_dt_format filter function
  52. @app.template_filter("date_format")
  53. def my_d_format(dt_date_time):
  54. return dt_date_time.strftime("%Y-%m-%d")
  55. # end of my_d_format filter function
  56. return app
  57. # end of create_app function