views.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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-12-17 20:45:08 (CST)
  9. # Last Update:星期二 2017-5-2 11:52:28 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import render_template, request
  14. from flask_babelex import gettext as _
  15. from forums.api.topic.models import Topic
  16. from forums.common.views import BaseMethodView as MethodView
  17. from forums.common.utils import (gen_filter_dict, gen_order_by)
  18. from forums.api.utils import gen_topic_filter, gen_topic_orderby
  19. from .models import Board
  20. class IndexView(MethodView):
  21. def get(self):
  22. topics = Topic.query.filter_by(
  23. is_good=True, is_top=False).paginate(1, 10)
  24. top_topics = Topic.query.filter_by(is_top=True).limit(5)
  25. if not topics.items:
  26. topics = Topic.query.filter_by(is_top=False).paginate(1, 10)
  27. data = {'title': '', 'topics': topics, 'top_topics': top_topics}
  28. return render_template('forums/index.html', **data)
  29. class AboutView(MethodView):
  30. def get(self):
  31. data = {'title': _('About - ')}
  32. return render_template('forums/about.html', **data)
  33. class HelpView(MethodView):
  34. def get(self):
  35. data = {'title': _('Help - ')}
  36. return render_template('forums/help.html', **data)
  37. class ContactView(MethodView):
  38. def get(self):
  39. data = {'title': _('Contact - ')}
  40. return render_template('forums/contact.html', **data)
  41. class BoardListView(MethodView):
  42. def get(self):
  43. query_dict = request.data
  44. page, number = self.page_info
  45. keys = ['name']
  46. order_by = gen_order_by(query_dict, keys)
  47. filter_dict = gen_filter_dict(query_dict, keys)
  48. filter_dict.update(parent_id=None)
  49. boards = Board.query.filter_by(
  50. **filter_dict).order_by(*order_by).paginate(page, number, True)
  51. data = {'title': 'Board', 'boards': boards}
  52. return render_template('board/board_list.html', **data)
  53. class BoardView(MethodView):
  54. def get(self, boardId):
  55. board = Board.query.filter_by(id=boardId).first_or_404()
  56. has_children = board.children.exists()
  57. topics = self.topics(boardId, has_children)
  58. data = {'title': 'Board', 'board': board, 'topics': topics}
  59. return render_template('board/board.html', **data)
  60. def topics(self, boardId, has_children):
  61. query_dict = request.data
  62. page, number = self.page_info
  63. keys = ['title']
  64. # order_by = gen_order_by(query_dict, keys)
  65. # filter_dict = gen_filter_dict(query_dict, keys)
  66. order_by = gen_topic_orderby(query_dict, keys)
  67. filter_dict = gen_topic_filter(query_dict, keys)
  68. if has_children:
  69. o = []
  70. for i in order_by:
  71. if i.startswith('-'):
  72. o.append(getattr(Topic, i.split('-')[1]).desc())
  73. else:
  74. o.append(getattr(Topic, i))
  75. topics = Topic.query.filter_by(**filter_dict).outerjoin(Board).or_(
  76. Board.parent_id == boardId,
  77. Board.id == boardId).order_by(*o).paginate(page, number, True)
  78. return topics
  79. filter_dict.update(board_id=boardId)
  80. topics = Topic.query.filter_by(
  81. **filter_dict).order_by(*order_by).paginate(page, number, True)
  82. return topics