views.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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-6-25 17:59:5 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import Blueprint, 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 = {'tags': tags}
  28. return render_template('forums/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 = {'tag': tag, 'topics': topics, 'top_topics': top_topics}
  38. return render_template('forums/tag.html', **data)
  39. @site.route('/<tag>/feed')
  40. def rss(tag):
  41. feed = AtomFeed('%s·HonMaple社区' % tag,
  42. feed_url=request.url,
  43. url=request.url_root,
  44. subtitle='I like solitude, yearning for freedom')
  45. topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit(
  46. 10).all()
  47. for topic in topics:
  48. feed.add(
  49. topic.title,
  50. escape(Filters.safe_markdown(topic.content) if topic.is_markdown
  51. else topic.content),
  52. content_type='html',
  53. author=topic.author.username,
  54. url=urljoin(request.url_root,
  55. url_for('topic.topic', uid=topic.uid)),
  56. updated=topic.publish,
  57. published=topic.publish)
  58. return feed.get_response()