filters.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: filters.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-11-07 21:00:32 (CST)
  9. # Last Update:星期三 2017-3-29 20:42:23 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from config import SITE
  15. from bleach import clean
  16. from flask import Markup, g
  17. from flask_babelex import format_datetime
  18. from flask_login import current_user
  19. from misaka import HtmlRenderer, Markdown
  20. def safe_clean(text):
  21. tags = ['b', 'i', 'font', 'br', 'blockquote', 'div', 'h2', 'a']
  22. attrs = {'*': ['style', 'id', 'class'], 'font': ['color'], 'a': ['href']}
  23. styles = ['color']
  24. return Markup(clean(text, tags=tags, attributes=attrs, styles=styles))
  25. def markdown(text):
  26. renderer = HtmlRenderer()
  27. md = Markdown(renderer, extensions=('fenced-code', ))
  28. return Markup(md(text))
  29. def safe_markdown(text):
  30. renderer = HtmlRenderer()
  31. md = Markdown(renderer, extensions=('fenced-code', ))
  32. return Markup(safe_clean(md(text)))
  33. def timesince(dt, default="just now"):
  34. now = datetime.utcnow()
  35. diff = now - dt
  36. if diff.days > 10:
  37. return format_datetime(dt, 'Y-M-d H:m')
  38. elif diff.days <= 10 and diff.days > 0:
  39. periods = ((diff.days, "day", "days"), )
  40. elif diff.days <= 0 and diff.seconds > 3600:
  41. periods = ((diff.seconds / 3600, "hour", "hours"), )
  42. elif diff.seconds <= 3600 and diff.seconds > 90:
  43. periods = ((diff.seconds / 60, "minute", "minutes"), )
  44. else:
  45. return default
  46. for period, singular, plural in periods:
  47. if period:
  48. return "%d %s ago" % (period, singular if period == 1 else plural)
  49. return default
  50. def show_time():
  51. from flask_babelex import format_datetime
  52. if g.user.is_authenticated:
  53. return 'LOCALE:' + format_datetime(datetime.utcnow())
  54. else:
  55. return 'UTC:' + format_datetime(datetime.utcnow())
  56. def notice_count():
  57. from forums.api.forums.models import Notice
  58. if g.user.is_authenticated:
  59. count = Notice.query.filter_by(
  60. rece_id=g.user.id, is_read=False).count()
  61. if count > 0:
  62. return count
  63. return None
  64. def hot_tags():
  65. from forums.api.tag.models import Tags
  66. tags = Tags.query.limit(9).all()
  67. return tags
  68. def recent_tags():
  69. from forums.api.tag.models import Tags
  70. tags = Tags.query.limit(12).all()
  71. return tags
  72. def is_online(user):
  73. from forums.api.user.models import UserSetting
  74. from forums.common.records import load_online_sign_users
  75. setting = user.setting
  76. if setting.online_status == UserSetting.STATUS_ALLOW_ALL:
  77. return user.username in load_online_sign_users()
  78. elif setting.online_status == UserSetting.STATUS_ALLOW_AUTHENTICATED:
  79. return user.username in load_online_sign_users(
  80. ) and current_user.is_authenticated
  81. elif setting.online_status == UserSetting.STATUS_ALLOW_OWN:
  82. return current_user.id == user.id
  83. return False
  84. def is_not_confirmed(user):
  85. return (not user.is_confirmed and user.id == current_user.id)
  86. def register_jinja2(app):
  87. app.jinja_env.globals['SITE'] = SITE
  88. app.jinja_env.globals['hot_tags'] = hot_tags
  89. app.jinja_env.globals['recent_tags'] = recent_tags
  90. app.jinja_env.globals['notice_count'] = notice_count
  91. app.jinja_env.globals['show_time'] = show_time
  92. app.jinja_env.filters['timesince'] = timesince
  93. app.jinja_env.filters['markdown'] = safe_markdown
  94. app.jinja_env.filters['safe_clean'] = safe_clean
  95. app.jinja_env.filters['is_online'] = is_online
  96. app.jinja_env.filters['is_not_confirmed'] = is_not_confirmed