__init__.py 3.0 KB

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