views.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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-17 13:28:58 (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 urllib.parse import urljoin
  18. from werkzeug.contrib.atom import AtomFeed
  19. from maple.filters import Filters
  20. site = Blueprint('tag', __name__)
  21. @site.route('', defaults={'tag': None})
  22. @site.route('/<tag>')
  23. def tag(tag):
  24. if tag is None:
  25. tags = Tags.query.distinct(Tags.tagname).all()
  26. data = {'tags': tags}
  27. return render_template('forums/tag_list.html', **data)
  28. else:
  29. page = is_num(request.args.get('page'))
  30. topics = Topic.query.join(Topic.tags).filter(
  31. Tags.tagname == tag).paginate(page,
  32. app.config['PER_PAGE'],
  33. error_out=True)
  34. tag = Tags.query.filter_by(tagname=tag).first_or_404()
  35. data = {'tag': tag, 'topics': topics}
  36. return render_template('forums/tag.html', **data)
  37. @site.route('/<tag>/feed')
  38. def rss(tag):
  39. feed = AtomFeed('Recent Topics',
  40. feed_url=request.url,
  41. url=request.url_root,
  42. subtitle='I like solitude, yearning for freedom')
  43. topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit(
  44. 10).all()
  45. for topic in topics:
  46. feed.add(
  47. topic.title,
  48. Filters.safe_markdown(topic.content)
  49. if topic.is_markdown else topic.content,
  50. content_type='html',
  51. author=topic.author.username,
  52. url=urljoin(request.url_root,
  53. url_for('topic.topic', uid=topic.uid)),
  54. updated=topic.publish,
  55. published=topic.publish)
  56. return feed.get_response()