views.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: views.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-05-20 13:18:19 (CST)
  9. # Last Update:星期五 2016-7-15 18:33:44 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import render_template, request, url_for
  14. from maple import app
  15. from maple.helpers import is_num
  16. from maple.topic.models import Tags, Topic
  17. from maple.filters import Filters
  18. from urllib.parse import urljoin
  19. from werkzeug.utils import escape
  20. from werkzeug.contrib.atom import AtomFeed
  21. # site = Blueprint('tag', __name__)
  22. # @site.route('', defaults={'tag': None})
  23. # @site.route('/<tag>')
  24. def tag(tag):
  25. if tag is None:
  26. tags = Tags.query.distinct(Tags.tagname).all()
  27. data = {'title': '所有标签 - ', 'tags': tags}
  28. return render_template('tag/tag_list.html', **data)
  29. else:
  30. page = is_num(request.args.get('page'))
  31. topic_base = Topic.query.join(Topic.tags).filter(Tags.tagname == tag)
  32. topics = topic_base.filter(Topic.is_top == False).paginate(
  33. page, app.config['PER_PAGE'],
  34. error_out=True)
  35. top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
  36. tag = Tags.query.filter_by(tagname=tag).first_or_404()
  37. data = {'title': '%s - ' % tag,
  38. 'tag': tag,
  39. 'topics': topics,
  40. 'top_topics': top_topics}
  41. return render_template('tag/tag.html', **data)
  42. # @site.route('/hot')
  43. # def hot():
  44. # tags = Tags.query.order_by(Tags.time.desc()).limit(10).all()
  45. # return tags
  46. # @site.route('/recent')
  47. # def recent():
  48. # tags = Tags.query.order_by(Tags.time.desc()).limit(10).all()
  49. # return tags
  50. # @site.route('/<tag>/feed')
  51. def rss(tag):
  52. feed = AtomFeed('%s·HonMaple社区' % tag,
  53. feed_url=request.url,
  54. url=request.url_root,
  55. subtitle='I like solitude, yearning for freedom')
  56. topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit(
  57. 10).all()
  58. for topic in topics:
  59. feed.add(
  60. topic.title,
  61. escape(Filters.safe_markdown(topic.content) if topic.is_markdown
  62. else topic.content),
  63. content_type='html',
  64. author=topic.author.username,
  65. url=urljoin(request.url_root,
  66. url_for('topic.topic', uid=topic.uid)),
  67. updated=topic.publish,
  68. published=topic.publish)
  69. return feed.get_response()