views.py 3.9 KB

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