views.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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-25 1:22:43 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (render_template, g, request, abort, redirect, flash,
  14. url_for, current_app)
  15. from flask_login import current_user
  16. from flask_babelex import gettext as _
  17. from flask_maple.forms import flash_errors
  18. from maple.helpers import is_num
  19. from maple.user.models import User
  20. from maple.forums.models import Notice, Board
  21. from maple.topic.models import Topic
  22. from .forms import MessageForm
  23. from maple import cache, db
  24. @cache.cached(timeout=60)
  25. def index():
  26. topics = Topic.query.filter_by(is_good=True, is_top=False).paginate(1, 10)
  27. top_topics = Topic.query.filter_by(is_top=True).limit(5)
  28. if not topics.items:
  29. topics = Topic.query.filter_by(is_top=False).paginate(1, 10)
  30. data = {'title': '', 'topics': topics, 'top_topics': top_topics}
  31. return render_template('forums/index.html', **data)
  32. @cache.cached(timeout=60)
  33. def forums():
  34. boards = {}
  35. parent_boards = db.session.query(Board.parent_board).group_by(
  36. Board.parent_board)
  37. for parent_board in parent_boards:
  38. child_board = Board.query.filter_by(parent_board=parent_board).all()
  39. boards[parent_board[0]] = child_board
  40. data = {'title': _('Index - '), 'boards': boards}
  41. return render_template('forums/forums.html', **data)
  42. @cache.cached(timeout=60)
  43. def notice():
  44. page = is_num(request.args.get('page'))
  45. notices = Notice.query.filter_by(
  46. rece_id=current_user.id).order_by(Notice.publish.desc()).paginate(
  47. page, current_app.config['PER_PAGE'],
  48. error_out=True)
  49. unread_notices = Notice.query.filter_by(rece_id=current_user.id,
  50. is_read=False).all()
  51. if unread_notices:
  52. for notice in unread_notices:
  53. notice.is_read = True
  54. db.session.commit()
  55. data = {'title': _('Notice - '), 'notices': notices}
  56. return render_template('forums/notice.html', **data)
  57. @cache.cached(timeout=60)
  58. def userlist():
  59. page = is_num(request.args.get('page'))
  60. users = User.query.paginate(page,
  61. current_app.config['PER_PAGE'],
  62. error_out=True)
  63. data = {'title': _('Userlist - '), 'users': users}
  64. return render_template('forums/userlist.html', **data)
  65. def message(receId):
  66. form = MessageForm()
  67. rece_user = User.query.filter_by(id=receId).first_or_404()
  68. if form.validate_on_submit() and request.method == "POST":
  69. message = Notice()
  70. message.category = 'privacy'
  71. message.content = {'content': form.message.data}
  72. message.rece_user = rece_user
  73. message.send_id = current_user.id
  74. db.session.add(message)
  75. db.session.commit()
  76. flash(_('send succeccfully'), category='success')
  77. return redirect(url_for('user.user', user_url=rece_user.username))
  78. else:
  79. if form.errors:
  80. flash_errors(form)
  81. return redirect(url_for('user.user', user_url=rece_user.username))
  82. @cache.cached(timeout=60)
  83. def about():
  84. data = {'title': _('About - ')}
  85. return render_template('forums/about.html', **data)
  86. @cache.cached(timeout=60)
  87. def help():
  88. data = {'title': _('Help - ')}
  89. return render_template('forums/help.html', **data)
  90. @cache.cached(timeout=60)
  91. def contact():
  92. data = {'title': _('Contact - ')}
  93. return render_template('forums/contact.html', **data)
  94. def order():
  95. from maple.main.orderby import form_judge
  96. form = g.sort_form
  97. if form.validate_on_submit():
  98. topics = form_judge(form)
  99. data = {'topics': topics}
  100. return render_template('base/sort.html', **data)
  101. else:
  102. abort(404)