views.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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-7-4 17:41:42 (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 = {'title': '%s - ' % g.parent_b,
  41. 'boards': boards,
  42. 'topics': topics,
  43. 'top_topics': top_topics}
  44. return render_template('board/board_list.html', **data)
  45. else:
  46. board = Board.query.filter_by(board=child_b).first_or_404()
  47. topic_base = board.topics
  48. topics = topic_base.filter(Topic.is_top == False).paginate(page, 20,
  49. True)
  50. top_topics = topic_base.filter(Topic.is_top == True).limit(5).all()
  51. data = {'title': '%s - ' % board.board,
  52. 'board': board,
  53. 'topics': topics,
  54. 'top_topics': top_topics}
  55. return render_template('board/board.html', **data)