common.py 662 B

123456789101112131415161718192021222324252627282930
  1. import functools
  2. from flask import session, g
  3. def do_index_class(index):
  4. """自定义过滤器,过滤点击排行html的class"""
  5. if index == 0:
  6. return "first"
  7. elif index == 1:
  8. return "second"
  9. elif index == 2:
  10. return "third"
  11. else:
  12. return ""
  13. def user_login_data(f):
  14. @functools.wraps(f)
  15. def wrapper(*args, **kwargs):
  16. # 获取到当前登录用户的id
  17. user_id = session.get("user_id")
  18. user = None
  19. if user_id:
  20. from app.models import User
  21. user = User.query.get(user_id)
  22. g.user = user
  23. return f(*args, **kwargs)
  24. return wrapper