views.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, perm_post_reply, perm_post_topic,
  16. perm_edit_post, perm_delete_topic,
  17. perm_delete_post, get_online_users)
  18. from flaskbb.forum.models import Category, Forum, Topic, Post
  19. from flaskbb.forum.forms import QuickreplyForm, ReplyForm, NewTopicForm
  20. from flaskbb.user.models import User
  21. forum = Blueprint("forum", __name__)
  22. @forum.route("/")
  23. def index():
  24. categories = Category.query.all()
  25. # Fetch a few stats about the forum
  26. user_count = User.query.count()
  27. topic_count = Topic.query.count()
  28. post_count = Post.query.count()
  29. newest_user = User.query.order_by(User.id.desc()).first()
  30. return render_template("forum/index.html", categories=categories,
  31. user_count=user_count,
  32. topic_count=topic_count,
  33. post_count=post_count,
  34. newest_user=newest_user.username,
  35. online_users=len(get_online_users()),
  36. online_guests=len(get_online_users(guest=True)))
  37. @forum.route("/category/<int:category_id>")
  38. def view_category(category_id):
  39. category = Category.query.filter_by(id=category_id).first()
  40. return render_template("forum/category.html", category=category)
  41. @forum.route("/forum/<int:forum_id>")
  42. def view_forum(forum_id):
  43. page = request.args.get('page', 1, type=int)
  44. forum = Forum.query.filter_by(id=forum_id).first()
  45. topics = Topic.query.filter_by(forum_id=forum.id).\
  46. order_by(Topic.last_post_id.desc()).\
  47. paginate(page, current_app.config['TOPICS_PER_PAGE'], False)
  48. return render_template("forum/forum.html", forum=forum, topics=topics)
  49. @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
  50. def view_topic(topic_id):
  51. page = request.args.get('page', 1, type=int)
  52. topic = Topic.query.filter_by(id=topic_id).first()
  53. posts = Post.query.filter_by(topic_id=topic.id).\
  54. paginate(page, current_app.config['POSTS_PER_PAGE'], False)
  55. # Count the topic views
  56. topic.views += 1
  57. topic.save()
  58. form = None
  59. if not topic.locked and perm_post_reply(user=current_user,
  60. forum=topic.forum):
  61. form = QuickreplyForm()
  62. if form.validate_on_submit():
  63. post = form.save(current_user, topic)
  64. return view_post(post.id)
  65. return render_template("forum/topic.html", topic=topic, posts=posts,
  66. per_page=current_app.config['POSTS_PER_PAGE'],
  67. last_seen=time_diff(), form=form)
  68. @forum.route("/post/<int:post_id>")
  69. def view_post(post_id):
  70. post = Post.query.filter_by(id=post_id).first()
  71. count = post.topic.post_count
  72. page = math.ceil(count / current_app.config["POSTS_PER_PAGE"])
  73. if count > 10:
  74. page += 1
  75. else:
  76. page = 1
  77. return redirect(url_for("forum.view_topic", topic_id=post.topic.id) +
  78. "?page=%d#pid%s" % (page, post.id))
  79. @forum.route("/forum/<int:forum_id>/topic/new", methods=["POST", "GET"])
  80. @login_required
  81. def new_topic(forum_id):
  82. forum = Forum.query.filter_by(id=forum_id).first()
  83. if not perm_post_topic(user=current_user, forum=forum):
  84. flash("You do not have the permissions to create a new topic.", "error")
  85. return redirect(url_for('forum.view_forum', forum_id=forum.id))
  86. form = NewTopicForm()
  87. if form.validate_on_submit():
  88. topic = form.save(current_user, forum)
  89. # redirect to the new topic
  90. return redirect(url_for('forum.view_topic', topic_id=topic.id))
  91. return render_template("forum/new_topic.html", forum=forum, form=form)
  92. @forum.route("/topic/<int:topic_id>/delete")
  93. @login_required
  94. def delete_topic(topic_id):
  95. topic = Topic.query.filter_by(id=topic_id).first()
  96. if not perm_delete_topic(user=current_user, forum=topic.forum,
  97. post_user_id=topic.first_post.user_id):
  98. flash("You do not have the permissions to delete the topic", "error")
  99. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  100. involved_users = User.query.filter(Post.topic_id == topic.id,
  101. User.id == Post.user_id).all()
  102. topic.delete(users=involved_users)
  103. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  104. @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
  105. @login_required
  106. def new_post(topic_id):
  107. topic = Topic.query.filter_by(id=topic_id).first()
  108. if topic.locked:
  109. flash("The topic is locked.", "error")
  110. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  111. if not perm_post_reply(user=current_user, forum=topic.forum):
  112. flash("You do not have the permissions to delete the topic", "error")
  113. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  114. form = ReplyForm()
  115. if form.validate_on_submit():
  116. post = form.save(current_user, topic)
  117. return view_post(post.id)
  118. return render_template("forum/new_post.html", topic=topic, form=form)
  119. @forum.route("/post/<int:post_id>/edit", methods=["POST", "GET"])
  120. @login_required
  121. def edit_post(post_id):
  122. post = Post.query.filter_by(id=post_id).first()
  123. if not perm_edit_post(user=current_user, forum=post.topic.forum,
  124. post_user_id=post.user_id):
  125. flash("You do not have the permissions to edit this post", "error")
  126. return redirect(url_for('forum.view_topic', topic_id=post.topic_id))
  127. form = ReplyForm()
  128. if form.validate_on_submit():
  129. form.populate_obj(post)
  130. post.date_modified = datetime.datetime.utcnow()
  131. post.save()
  132. return redirect(url_for('forum.view_topic', topic_id=post.topic.id))
  133. else:
  134. form.content.data = post.content
  135. return render_template("forum/new_post.html", topic=post.topic, form=form)
  136. @forum.route("/post/<int:post_id>/delete")
  137. @login_required
  138. def delete_post(post_id):
  139. post = Post.query.filter_by(id=post_id).first()
  140. if not perm_delete_post(user=current_user, forum=post.topic.forum,
  141. post_user_id=post.user_id):
  142. flash("You do not have the permissions to edit this post", "error")
  143. return redirect(url_for('forum.view_topic', topic_id=post.topic_id))
  144. topic_id = post.topic_id
  145. post.delete()
  146. # If the post was the first post in the topic, redirect to the forums
  147. if post.first_post:
  148. return redirect(url_for('forum.view_forum',
  149. forum_id=post.topic.forum_id))
  150. return redirect(url_for('forum.view_topic', topic_id=topic_id))
  151. @forum.route("/who_is_online")
  152. def who_is_online():
  153. return render_template("forum/online_users.html",
  154. online_users=get_online_users())
  155. @forum.route("/memberlist")
  156. def memberlist():
  157. page = request.args.get('page', 1, type=int)
  158. users = User.query.order_by(User.id).\
  159. paginate(page, current_app.config['POSTS_PER_PAGE'], False)
  160. return render_template("forum/memberlist.html",
  161. users=users,
  162. per_page=current_app.config['USERS_PER_PAGE'])
  163. @forum.route("/topictracker")
  164. def topic_tracker():
  165. #return render_template("forum/topictracker.html", topics=topics)
  166. pass
  167. @forum.route("/topictracker/<topic_id>/add")
  168. def add_to_topictrack(topic_id):
  169. pass
  170. @forum.route("/topictracker/<topic_id>/delete")
  171. def remove_from_topictrack(topic_id):
  172. pass