__init__.py 3.1 KB

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