views.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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-06-03 14:32:06 (CST)
  9. # Last Update:星期六 2016-6-25 17:52:50 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import Blueprint, g, render_template, abort, request
  14. from maple.helpers import is_num
  15. from maple.topic.models import Topic
  16. from maple.forums.models import Board
  17. site = Blueprint('board', __name__)
  18. @site.url_value_preprocessor
  19. def pull_url(endpoint, values):
  20. g.parent_b = values.pop('parent_b', None)
  21. board = Board.query.filter_by(parent_board=g.parent_b).first()
  22. if board is None:
  23. abort(404)
  24. @site.url_defaults
  25. def add_url(endpoint, values):
  26. if 'parent_b' in values or not g.parent_b:
  27. return
  28. values['parent_b'] = g.parent_b
  29. @site.route('', defaults={'child_b': None})
  30. @site.route('/<child_b>')
  31. def board(child_b):
  32. page = is_num(request.args.get('page'))
  33. if child_b is None:
  34. boards = Board.query.filter_by(parent_board=g.parent_b).all()
  35. topic_base = Topic.query.join(Topic.board).filter(Board.parent_board ==
  36. g.parent_b)
  37. topics = topic_base.filter(Topic.is_top == False).paginate(page, 20,
  38. True)
  39. top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
  40. data = {'boards': boards, 'topics': topics, 'top_topics': top_topics}
  41. return render_template('forums/board_list.html', **data)
  42. else:
  43. board = Board.query.filter_by(board=child_b).first_or_404()
  44. topic_base = board.topics
  45. topics = topic_base.filter(Topic.is_top == False).paginate(page, 20,
  46. True)
  47. top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
  48. data = {'board': board, 'topics': topics, 'top_topics': top_topics}
  49. return render_template('forums/board.html', **data)