views.py 2.2 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-19 16:51:32 (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. topics = Topic.query.join(Topic.tags).filter(
  32. Tags.tagname == tag).paginate(page,
  33. app.config['PER_PAGE'],
  34. error_out=True)
  35. tag = Tags.query.filter_by(tagname=tag).first_or_404()
  36. data = {'tag': tag, 'topics': topics}
  37. return render_template('forums/tag.html', **data)
  38. @site.route('/<tag>/feed')
  39. def rss(tag):
  40. feed = AtomFeed('%s·HonMaple社区'%tag,
  41. feed_url=request.url,
  42. url=request.url_root,
  43. subtitle='I like solitude, yearning for freedom')
  44. topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit(
  45. 10).all()
  46. for topic in topics:
  47. feed.add(
  48. topic.title,
  49. escape(Filters.safe_markdown(topic.content)
  50. if topic.is_markdown else topic.content),
  51. content_type='html',
  52. author=topic.author.username,
  53. url=urljoin(request.url_root,
  54. url_for('topic.topic', uid=topic.uid)),
  55. updated=topic.publish,
  56. published=topic.publish)
  57. return feed.get_response()