filters.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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:星期四 2016-12-29 22:0:34 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from maple.extension import redis_data
  15. from api.topic.models import Topic
  16. from api.reply.models import Reply
  17. from api.user.models import User
  18. from flask import Markup, g
  19. from flask_babelex import format_datetime
  20. from misaka import Markdown, HtmlRenderer
  21. from bleach import clean
  22. def safe_clean(text):
  23. tags = ['b', 'i', 'font', 'br', 'blockquote', 'div', 'h2', 'a']
  24. attrs = {'*': ['style', 'id', 'class'], 'font': ['color'], 'a': ['href']}
  25. styles = ['color']
  26. return Markup(clean(text, tags=tags, attributes=attrs, styles=styles))
  27. def markdown(text):
  28. renderer = HtmlRenderer()
  29. md = Markdown(renderer, extensions=('fenced-code', ))
  30. return Markup(md(text))
  31. def safe_markdown(text):
  32. renderer = HtmlRenderer()
  33. md = Markdown(renderer, extensions=('fenced-code', ))
  34. return Markup(safe_clean(md(text)))
  35. def timesince(dt, default="just now"):
  36. now = datetime.utcnow()
  37. diff = now - dt
  38. if diff.days > 10:
  39. return format_datetime(dt, 'Y-M-d H:m')
  40. elif diff.days <= 10 and diff.days > 0:
  41. periods = ((diff.days, "day", "days"), )
  42. elif diff.days <= 0 and diff.seconds > 3600:
  43. periods = ((diff.seconds / 3600, "hour", "hours"), )
  44. elif diff.seconds <= 3600 and diff.seconds > 90:
  45. periods = ((diff.seconds / 60, "minute", "minutes"), )
  46. else:
  47. return default
  48. for period, singular, plural in periods:
  49. if period:
  50. return "%d %s ago" % (period, singular if period == 1 else plural)
  51. return default
  52. def show_time():
  53. from flask_babelex import format_datetime
  54. if g.user.is_authenticated:
  55. return 'LOCALE:' + format_datetime(datetime.utcnow())
  56. else:
  57. return 'UTC:' + format_datetime(datetime.utcnow())
  58. def get_user_infor(name):
  59. user = User.query.filter(User.username == name).first()
  60. return user
  61. def get_last_reply(uid):
  62. reply = Reply.query.join(Reply.topic).filter(Topic.id == uid).first()
  63. return reply
  64. def get_read_count(id):
  65. read = redis_data.hget('topic:%s' % str(id), 'read')
  66. replies = redis_data.hget('topic:%s' % str(id), 'replies')
  67. if not read:
  68. read = 0
  69. else:
  70. read = int(read)
  71. if not replies:
  72. replies = 0
  73. else:
  74. replies = int(replies)
  75. return replies, read
  76. def is_collected(topicId):
  77. from maple.topic.models import CollectTopic
  78. from flask_login import current_user
  79. for collect in current_user.collects:
  80. cid = CollectTopic.query.filter_by(
  81. collect_id=collect.id, topic_id=topicId).first()
  82. if cid is not None:
  83. return True
  84. return False
  85. def notice_count():
  86. from maple.forums.models import Notice
  87. if g.user.is_authenticated:
  88. count = Notice.query.filter_by(
  89. rece_id=g.user.id, is_read=False).count()
  90. if count > 0:
  91. return count
  92. return None
  93. def hot_tags():
  94. from api.tag.models import Tags
  95. tags = Tags.query.limit(9).all()
  96. return tags
  97. def recent_tags():
  98. from api.tag.models import Tags
  99. tags = Tags.query.limit(12).all()
  100. return tags
  101. def is_online(username):
  102. from maple.main.records import load_online_sign_users
  103. online_users = load_online_sign_users()
  104. if username in online_users:
  105. return True
  106. return False
  107. class Title(object):
  108. setting = {'title': 'Honmaple', 'picture': '', 'description': '爱生活,更爱自由'}
  109. title = setting['title']
  110. picture = setting['picture']
  111. description = setting['description']
  112. def register_jinja2(app):
  113. app.jinja_env.globals['Title'] = Title
  114. app.jinja_env.globals['hot_tags'] = hot_tags
  115. app.jinja_env.globals['recent_tags'] = recent_tags
  116. app.jinja_env.globals['notice_count'] = notice_count
  117. app.jinja_env.globals['show_time'] = show_time
  118. app.jinja_env.filters['get_last_reply'] = get_last_reply
  119. app.jinja_env.filters['get_user_infor'] = get_user_infor
  120. app.jinja_env.filters['get_read_count'] = get_read_count
  121. app.jinja_env.filters['timesince'] = timesince
  122. app.jinja_env.filters['markdown'] = safe_markdown
  123. app.jinja_env.filters['safe_clean'] = safe_clean
  124. app.jinja_env.filters['is_collected'] = is_collected
  125. app.jinja_env.filters['is_online'] = is_online