views.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  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-14 23:20:14 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import Blueprint, render_template, request
  14. from maple import app
  15. from maple.helpers import is_num
  16. from maple.topic.models import Tags, Topic
  17. site = Blueprint('tag', __name__)
  18. @site.route('', defaults={'tag': None})
  19. @site.route('/<tag>')
  20. def tag(tag):
  21. if tag is None:
  22. tags = Tags.query.distinct(Tags.tagname).all()
  23. data = {'tags': tags}
  24. return render_template('forums/tag_list.html', **data)
  25. else:
  26. page = is_num(request.args.get('page'))
  27. topics = Topic.query.join(Topic.tags).filter(
  28. Tags.tagname == tag).paginate(page,
  29. app.config['PER_PAGE'],
  30. error_out=True)
  31. tag = Tags.query.filter_by(tagname=tag).first_or_404()
  32. data = {'tag': tag, 'topics': topics}
  33. return render_template('forums/tag.html', **data)