base.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os.path
  2. from flask import Flask, url_for, request, current_app
  3. from flask_mail import Mail
  4. from flask_login import LoginManager, current_user
  5. from typing import Optional
  6. import sys
  7. import logging.handlers
  8. import logging
  9. from configure import conf
  10. from core.user import AnonymousUser
  11. class App:
  12. def __init__(self, import_name: str):
  13. self._app = Flask(import_name)
  14. self._app.config["SECRET_KEY"] = conf['secret-key']
  15. self.login_manager = LoginManager()
  16. self.login_manager.init_app(self._app)
  17. self.login_manager.anonymous_user = AnonymousUser # 设置未登录的匿名对象
  18. self._app.config["MAIL_SERVER"] = conf['email_server']
  19. self._app.config["MAIL_PORT"] = conf['email_port']
  20. self._app.config["MAIL_USE_TLS"] = conf['email_tls']
  21. self._app.config["MAIL_USE_SSL"] = conf['email_ssl']
  22. self._app.config["MAIL_USERNAME"] = conf['email_name']
  23. self._app.config["MAIL_PASSWORD"] = conf['email_passwd']
  24. self.mail = Mail(self._app)
  25. self._app.logger.setLevel(conf["log-level"])
  26. if conf["log-home"] is not None:
  27. handle = logging.handlers.TimedRotatingFileHandler(
  28. os.path.join(conf["log-home"], f"flask-{os.getpid()}.log"))
  29. handle.setFormatter(logging.Formatter(conf["log-format"]))
  30. self._app.logger.addHandler(handle)
  31. def get_app(self) -> Flask:
  32. return self._app
  33. def run(self):
  34. self.run()
  35. @staticmethod
  36. def get_max_page(count: int, count_page: int):
  37. return (count // count_page) + (0 if count % count_page == 0 else 1)
  38. @staticmethod
  39. def get_page(url, page: int, count: int):
  40. if count <= 9:
  41. page_list = [[f"{i + 1}", url_for(url, page=i + 1)] for i in range(count)]
  42. elif page <= 5:
  43. """
  44. [1][2][3][4][5][6][...][count - 1][count]
  45. """
  46. page_list = [[f"{i + 1}", url_for(url, page=i + 1)] for i in range(6)]
  47. page_list += [None,
  48. [f"{count - 1}", url_for(url, page=count - 1)],
  49. [f"{count}", url_for(url, page=count)]]
  50. elif page >= count - 5:
  51. """
  52. [1][2][...][count - 5][count - 4][count - 3][count - 2][count - 1][count]
  53. """
  54. page_list: Optional[list] = [["1", url_for(url, page=1)],
  55. ["2", url_for(url, page=2)],
  56. None]
  57. page_list += [[f"{count - 5 + i}", url_for(url, page=count - 5 + i), False] for i in range(6)]
  58. else:
  59. """
  60. [1][2][...][page - 2][page - 1][page][page + 1][page + 2][...][count - 1][count]
  61. """
  62. page_list: Optional[list] = [["1", url_for(url, page=1)],
  63. ["2", url_for(url, page=2)],
  64. None]
  65. page_list += [[f"{page - 2 + i}", url_for(url, page=page - 2 + i)] for i in range(5)]
  66. page_list += [None,
  67. [f"{count - 1}", url_for(url, page=count - 1)],
  68. [f"{count}", url_for(url, page=count)]]
  69. return page_list
  70. @staticmethod
  71. def __get_log_request_info():
  72. return (f"user: '{current_user.email}' "
  73. f"url: '{request.url}' blueprint: '{request.blueprint}' "
  74. f"args: {request.args} form: {request.form} "
  75. f"accept_encodings: '{request.accept_encodings}' "
  76. f"accept_charsets: '{request.accept_charsets}' "
  77. f"accept_mimetypes: '{request.accept_mimetypes}' "
  78. f"accept_languages: '{request.accept_languages}'")
  79. @staticmethod
  80. def print_load_page_log(page: str):
  81. current_app.logger.debug(
  82. f"[{request.method}] Load - '{page}' " + App.__get_log_request_info())
  83. @staticmethod
  84. def print_form_error_log(opt: str):
  85. current_app.logger.warning(
  86. f"[{request.method}] '{opt}' - Bad form " + App.__get_log_request_info())
  87. @staticmethod
  88. def print_sys_opt_fail_log(opt: str):
  89. current_app.logger.error(
  90. f"[{request.method}] System {opt} - fail " + App.__get_log_request_info())
  91. @staticmethod
  92. def print_sys_opt_success_log(opt: str):
  93. current_app.logger.warning(
  94. f"[{request.method}] System {opt} - success " + App.__get_log_request_info())
  95. @staticmethod
  96. def print_user_opt_fail_log(opt: str):
  97. current_app.logger.debug(
  98. f"[{request.method}] User {opt} - fail " + App.__get_log_request_info())
  99. @staticmethod
  100. def print_user_opt_success_log(opt: str):
  101. current_app.logger.debug(
  102. f"[{request.method}] User {opt} - success " + App.__get_log_request_info())
  103. @staticmethod
  104. def print_user_opt_error_log(opt: str):
  105. current_app.logger.warning(
  106. f"[{request.method}] User {opt} - system fail " + App.__get_log_request_info())
  107. @staticmethod
  108. def print_import_user_opt_success_log(opt: str):
  109. current_app.logger.info(
  110. f"[{request.method}] User {opt} - success " + App.__get_log_request_info())
  111. @staticmethod
  112. def print_user_not_allow_opt_log(opt: str):
  113. current_app.logger.info(
  114. f"[{request.method}] User '{opt}' - reject " + App.__get_log_request_info())