filters.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: filter.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-15 00:39:29 (CST)
  9. # Last Update:星期一 2016-7-25 20:58:51 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from datetime import datetime
  14. from maple import redis_data, cache
  15. from maple.settings import setting
  16. from maple.topic.models import Reply, Topic
  17. from maple.user.models import User
  18. from flask import Markup, g
  19. from misaka import Markdown, HtmlRenderer
  20. from pygments import highlight
  21. from pygments.formatters import HtmlFormatter
  22. from pygments.lexers import get_lexer_by_name
  23. from bleach import clean
  24. def safe_clean(text):
  25. tags = ['b', 'i', 'font', 'br', 'blockquote', 'div', 'h2']
  26. attrs = {'*': ['style', 'id', 'class'], 'font': ['color']}
  27. styles = ['color']
  28. return Markup(clean(text, tags=tags, attributes=attrs, styles=styles))
  29. class Filters(object):
  30. def safe_markdown(text):
  31. class HighlighterRenderer(HtmlRenderer):
  32. def blockcode(self, text, lang):
  33. lang = 'python'
  34. if not lang:
  35. return '\n<pre><code>{}</code></pre>\n'.format(text.strip(
  36. ))
  37. lexer = get_lexer_by_name(lang, stripall=True)
  38. formatter = HtmlFormatter()
  39. return highlight(text, lexer, formatter)
  40. renderer = HighlighterRenderer()
  41. md = Markdown(renderer, extensions=('fenced-code', ))
  42. return Markup(md(safe_clean(text)))
  43. # return Markup(md(text))
  44. def timesince(dt, default="just now"):
  45. from flask_babelex import format_datetime
  46. now = datetime.utcnow()
  47. diff = now - dt
  48. if diff.days > 10:
  49. return format_datetime(dt, 'Y-M-d H:m')
  50. elif diff.days <= 10 and diff.days > 0:
  51. periods = ((diff.days, "day", "days"), )
  52. elif diff.days <= 0 and diff.seconds > 3600:
  53. periods = ((diff.seconds / 3600, "hour", "hours"), )
  54. elif diff.seconds <= 3600 and diff.seconds > 90:
  55. periods = ((diff.seconds / 60, "minute", "minutes"), )
  56. else:
  57. return default
  58. for period, singular, plural in periods:
  59. if period:
  60. return "%d %s ago" % (period, singular if period == 1 else
  61. plural)
  62. return default
  63. def show_time():
  64. from flask_babelex import format_datetime
  65. if g.user.is_authenticated:
  66. return 'LOCALE:' + format_datetime(datetime.utcnow())
  67. else:
  68. return 'UTC:' + format_datetime(datetime.utcnow())
  69. def get_user_infor(name):
  70. user = User.query.filter(User.username == name).first()
  71. return user
  72. @cache.memoize(timeout=60)
  73. def get_last_reply(uid):
  74. reply = Reply.query.join(Reply.topic).filter(Topic.id == uid).first()
  75. return reply
  76. @cache.memoize(timeout=30)
  77. def get_read_count(id):
  78. read = redis_data.hget('topic:%s' % str(id), 'read')
  79. replies = redis_data.hget('topic:%s' % str(id), 'replies')
  80. if not read:
  81. read = 0
  82. else:
  83. read = int(read)
  84. if not replies:
  85. replies = 0
  86. else:
  87. replies = int(replies)
  88. return replies, read
  89. @cache.memoize(timeout=30)
  90. def is_collected(topicId):
  91. from maple.topic.models import CollectTopic
  92. from flask_login import current_user
  93. for collect in current_user.collects:
  94. cid = CollectTopic.query.filter_by(collect_id=collect.id,
  95. topic_id=topicId).first()
  96. if cid is not None:
  97. return True
  98. return False
  99. def notice_count():
  100. from maple.forums.models import Notice
  101. if g.user.is_authenticated:
  102. count = Notice.query.filter_by(rece_id=g.user.id,
  103. is_read=False).count()
  104. if count > 0:
  105. return count
  106. return None
  107. @cache.memoize(timeout=60)
  108. def hot_tags():
  109. from maple.tag.models import Tags
  110. tags = Tags.query.order_by(Tags.time.desc()).limit(9).all()
  111. return tags
  112. @cache.memoize(timeout=60)
  113. def recent_tags():
  114. from maple.tag.models import Tags
  115. tags = Tags.query.order_by(Tags.time.desc()).limit(12).all()
  116. return tags
  117. class Title(object):
  118. title = setting['title']
  119. picture = setting['picture']
  120. description = setting['description']