views.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.forum.views
  4. ~~~~~~~~~~~~~~~~~~~~
  5. This module handles the forum logic like creating and viewing
  6. topics and posts.
  7. :copyright: (c) 2013 by the FlaskBB Team.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. import datetime
  11. import math
  12. from flask import (Blueprint, render_template, redirect, url_for, current_app,
  13. request, flash)
  14. from flask.ext.login import login_required, current_user
  15. from flaskbb.helpers import time_diff, check_perm
  16. from flaskbb.forum.models import Category, Forum, Topic, Post
  17. from flaskbb.forum.forms import QuickreplyForm, ReplyForm, NewTopicForm
  18. from flaskbb.user.models import User
  19. forum = Blueprint("forum", __name__)
  20. @forum.route("/")
  21. def index():
  22. categories = Category.query.all()
  23. # Fetch a few stats about the forum
  24. user_count = User.query.count()
  25. topic_count = Topic.query.count()
  26. post_count = Post.query.count()
  27. newest_user = User.query.order_by(User.id.desc()).first()
  28. online_users = User.query.filter(User.lastseen >= time_diff())
  29. return render_template("forum/index.html", categories=categories,
  30. stats={'user_count': user_count,
  31. 'topic_count': topic_count,
  32. 'post_count': post_count,
  33. 'newest_user': newest_user.username,
  34. 'online_users': online_users})
  35. @forum.route("/category/<int:category_id>")
  36. def view_category(category_id):
  37. category = Category.query.filter_by(id=category_id).first()
  38. return render_template("forum/category.html", category=category)
  39. @forum.route("/forum/<int:forum_id>")
  40. def view_forum(forum_id):
  41. page = request.args.get('page', 1, type=int)
  42. forum = Forum.query.filter_by(id=forum_id).first()
  43. topics = Topic.query.filter_by(forum_id=forum.id).\
  44. order_by(Topic.last_post_id.desc()).\
  45. paginate(page, current_app.config['TOPICS_PER_PAGE'], False)
  46. return render_template("forum/forum.html", forum=forum, topics=topics)
  47. @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
  48. def view_topic(topic_id):
  49. page = request.args.get('page', 1, type=int)
  50. topic = Topic.query.filter_by(id=topic_id).first()
  51. posts = Post.query.filter_by(topic_id=topic.id).\
  52. paginate(page, current_app.config['POSTS_PER_PAGE'], False)
  53. # Count the topic views
  54. topic.views += 1
  55. topic.save()
  56. form = None
  57. if not topic.locked:
  58. if check_perm(current_user, 'postreply', topic.forum):
  59. form = QuickreplyForm()
  60. if form.validate_on_submit():
  61. post = form.save(current_user, topic)
  62. return view_post(post.id)
  63. return render_template("forum/topic.html", topic=topic, posts=posts,
  64. per_page=current_app.config['POSTS_PER_PAGE'],
  65. last_seen=time_diff(), form=form)
  66. @forum.route("/post/<int:post_id>")
  67. def view_post(post_id):
  68. post = Post.query.filter_by(id=post_id).first()
  69. count = post.topic.post_count
  70. page = math.ceil(count / current_app.config["POSTS_PER_PAGE"])
  71. if count > 10:
  72. page += 1
  73. else:
  74. page = 1
  75. return redirect(url_for("forum.view_topic", topic_id=post.topic.id) +
  76. "?page=%d#pid%s" % (page, post.id))
  77. @forum.route("/forum/<int:forum_id>/topic/new", methods=["POST", "GET"])
  78. @login_required
  79. def new_topic(forum_id):
  80. forum = Forum.query.filter_by(id=forum_id).first()
  81. if not check_perm(current_user, 'posttopic', forum):
  82. flash("You do not have the permissions to create a new topic.")
  83. return redirect(url_for('forum.view_forum', forum_id=forum.id))
  84. form = NewTopicForm()
  85. if form.validate_on_submit():
  86. topic = form.save(current_user, forum)
  87. # redirect to the new topic
  88. return redirect(url_for('forum.view_topic', topic_id=topic.id))
  89. return render_template("forum/new_topic.html", forum=forum, form=form)
  90. @forum.route("/topic/<int:topic_id>/delete")
  91. @login_required
  92. def delete_topic(topic_id):
  93. topic = Topic.query.filter_by(id=topic_id).first()
  94. if not check_perm(current_user, 'deletetopic', topic.forum):
  95. flash("You do not have the permissions to delete the topic")
  96. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  97. involved_users = User.query.filter(Post.topic_id == topic.id,
  98. User.id == Post.user_id).all()
  99. topic.delete(users=involved_users)
  100. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  101. @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
  102. @login_required
  103. def new_post(topic_id):
  104. topic = Topic.query.filter_by(id=topic_id).first()
  105. if topic.locked:
  106. flash("The topic is locked.")
  107. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  108. if not check_perm(current_user, 'postreply', topic.forum):
  109. flash("You do not have the permissions to delete the topic")
  110. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  111. form = ReplyForm()
  112. if form.validate_on_submit():
  113. post = form.save(current_user, topic)
  114. return view_post(post.id)
  115. return render_template("forum/new_post.html", topic=topic, form=form)
  116. @forum.route("/post/<int:post_id>/edit", methods=["POST", "GET"])
  117. @login_required
  118. def edit_post(post_id):
  119. post = Post.query.filter_by(id=post_id).first()
  120. if not check_perm(current_user, 'editpost', post.topic.forum,
  121. post.user_id):
  122. flash("You do not have the permissions to edit this post")
  123. return redirect(url_for('forum.view_topic', topic_id=post.topic_id))
  124. form = ReplyForm(obj=post)
  125. if form.validate_on_submit():
  126. form.populate_obj(post)
  127. post.date_modified = datetime.datetime.utcnow()
  128. post.save()
  129. return redirect(url_for('forum.view_topic', topic_id=post.topic.id))
  130. else:
  131. form.content.data = post.content
  132. return render_template("forum/new_post.html", topic=post.topic, form=form)
  133. @forum.route("/post/<int:post_id>/delete")
  134. @login_required
  135. def delete_post(post_id):
  136. post = Post.query.filter_by(id=post_id).first()
  137. if not check_perm(current_user, 'deletepost', post.topic.forum,
  138. post.user_id):
  139. flash("You do not have the permissions to edit this post")
  140. return redirect(url_for('forum.view_topic', topic_id=post.topic_id))
  141. topic_id = post.topic_id
  142. post.delete()
  143. # If the post was the first post in the topic, redirect to the forums
  144. if post.first_post:
  145. return redirect(url_for('forum.view_forum',
  146. forum_id=post.topic.forum_id))
  147. return redirect(url_for('forum.view_topic', topic=topic_id))
  148. @forum.route("/who_is_online")
  149. def who_is_online():
  150. pass
  151. @forum.route("/memberlist")
  152. def memberlist():
  153. page = request.args.get('page', 1, type=int)
  154. users = User.query.order_by(User.id).\
  155. paginate(page, current_app.config['POSTS_PER_PAGE'], False)
  156. return render_template("forum/memberlist.html",
  157. users=users,
  158. per_page=current_app.config['USERS_PER_PAGE'])