views.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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-24 17:16:21 (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 Topic
  17. from maple.filters import Filters
  18. from .models import Tags
  19. from urllib.parse import urljoin
  20. from werkzeug.utils import escape
  21. from werkzeug.contrib.atom import AtomFeed
  22. def tag(tag):
  23. if tag is None:
  24. tags = Tags.query.distinct(Tags.tagname).all()
  25. data = {'title': '所有标签 - ', 'tags': tags}
  26. return render_template('tag/tag_list.html', **data)
  27. else:
  28. page = is_num(request.args.get('page'))
  29. topic_base = Topic.query.join(Topic.tags).filter(Tags.tagname == tag)
  30. topics = topic_base.filter(Topic.is_top == False).paginate(
  31. page, app.config['PER_PAGE'],
  32. error_out=True)
  33. top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
  34. tag = Tags.query.filter_by(tagname=tag).first_or_404()
  35. data = {'title': '%s - ' % tag,
  36. 'tag': tag,
  37. 'topics': topics,
  38. 'top_topics': top_topics}
  39. return render_template('tag/tag.html', **data)
  40. # @site.route('/hot')
  41. # def hot():
  42. # tags = Tags.query.order_by(Tags.time.desc()).limit(10).all()
  43. # return tags
  44. # @site.route('/recent')
  45. # def recent():
  46. # tags = Tags.query.order_by(Tags.time.desc()).limit(10).all()
  47. # return tags
  48. def rss(tag):
  49. feed = AtomFeed('%s·HonMaple社区' % tag,
  50. feed_url=request.url,
  51. url=request.url_root,
  52. subtitle='I like solitude, yearning for freedom')
  53. topics = Topic.query.join(Topic.tags).filter(Tags.tagname == tag).limit(
  54. 10).all()
  55. for topic in topics:
  56. feed.add(
  57. topic.title,
  58. escape(Filters.safe_markdown(topic.content) if topic.is_markdown
  59. else topic.content),
  60. content_type='html',
  61. author=topic.author.username,
  62. url=urljoin(request.url_root,
  63. url_for('topic.topic', topicId=topic.uid)),
  64. updated=topic.publish,
  65. published=topic.publish)
  66. return feed.get_response()