index.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from flask import Blueprint, render_template, request
  2. from flask_login import current_user
  3. from configure import conf
  4. import app
  5. from object.blog import BlogArticle
  6. from object.msg import Message
  7. from sql.statistics import get_hello_click, add_hello_click, get_home_click, add_home_click
  8. index = Blueprint("base", __name__)
  9. @index.route('/')
  10. @app.cache.cached(timeout=conf["VIEW_CACHE_EXPIRE"])
  11. def hello_page():
  12. app.HBlogFlask.print_load_page_log(f"hello")
  13. add_hello_click()
  14. return render_template("index/hello.html")
  15. @index.route('/home')
  16. def index_page():
  17. blog_list = BlogArticle.get_blog_list(limit=5, offset=0, not_top=True)
  18. msg_list = Message.get_message_list(limit=6, offset=0, show_secret=False)
  19. app.HBlogFlask.print_load_page_log(f"index")
  20. add_home_click()
  21. return render_template("index/index.html",
  22. blog_list=blog_list,
  23. msg_list=msg_list,
  24. show_email=current_user.check_role("ReadUserInfo"),
  25. hello_clicks=get_hello_click(),
  26. home_clicks=get_home_click())
  27. @index.context_processor
  28. @app.cache.cached(timeout=conf["CACHE_EXPIRE"], key_prefix="inject_base:index")
  29. def inject_base():
  30. """ index默认模板变量, 覆盖app变量 """
  31. return {"top_nav": ["active", "", "", "", "", ""]}
  32. def get_icp():
  33. for i in conf["ICP"]:
  34. if i in request.host:
  35. return conf["ICP"][i]
  36. def get_gongan():
  37. for i in conf["GONG_AN"]:
  38. if i in request.host:
  39. return conf["GONG_AN"][i]
  40. @index.app_context_processor
  41. @app.cache.cached(timeout=conf["CACHE_EXPIRE"], key_prefix="inject_base")
  42. def inject_base():
  43. """ app默认模板变量 """
  44. return {"blog_name": conf['BLOG_NAME'],
  45. "top_nav": ["", "", "", "", "", ""],
  46. "blog_describe": conf['BLOG_DESCRIBE'],
  47. "conf": conf,
  48. "get_icp": get_icp,
  49. "get_gongan": get_gongan}