views.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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) 2014 by the FlaskBB Team.
  8. :license: BSD, see LICENSE for more details.
  9. """
  10. import datetime
  11. from flask import (Blueprint, redirect, url_for, current_app,
  12. request, flash)
  13. from flask.ext.login import login_required, current_user
  14. from flaskbb.extensions import db
  15. from flaskbb.utils.settings import flaskbb_config
  16. from flaskbb.utils.helpers import (get_online_users, time_diff, render_template,
  17. format_quote)
  18. from flaskbb.utils.permissions import (can_post_reply, can_post_topic,
  19. can_delete_topic, can_delete_post,
  20. can_edit_post, can_moderate)
  21. from flaskbb.forum.models import (Category, Forum, Topic, Post, ForumsRead,
  22. TopicsRead)
  23. from flaskbb.forum.forms import (QuickreplyForm, ReplyForm, NewTopicForm,
  24. ReportForm, UserSearchForm, SearchPageForm)
  25. from flaskbb.user.models import User, Group
  26. forum = Blueprint("forum", __name__)
  27. @forum.route("/")
  28. def index():
  29. categories = Category.get_all(user=current_user)
  30. # Fetch a few stats about the forum
  31. user_count = User.query.count()
  32. topic_count = Topic.query.count()
  33. post_count = Post.query.count()
  34. newest_user = User.query.order_by(User.id.desc()).first()
  35. # Check if we use redis or not
  36. if not current_app.config["REDIS_ENABLED"]:
  37. online_users = User.query.filter(User.lastseen >= time_diff()).count()
  38. # Because we do not have server side sessions, we cannot check if there
  39. # are online guests
  40. online_guests = None
  41. else:
  42. online_users = len(get_online_users())
  43. online_guests = len(get_online_users(guest=True))
  44. return render_template("forum/index.html",
  45. categories=categories,
  46. user_count=user_count,
  47. topic_count=topic_count,
  48. post_count=post_count,
  49. newest_user=newest_user,
  50. online_users=online_users,
  51. online_guests=online_guests)
  52. @forum.route("/category/<int:category_id>")
  53. @forum.route("/category/<int:category_id>-<slug>")
  54. def view_category(category_id, slug=None):
  55. category, forums = Category.\
  56. get_forums(category_id=category_id, user=current_user)
  57. return render_template("forum/category.html", forums=forums,
  58. category=category)
  59. @forum.route("/forum/<int:forum_id>")
  60. @forum.route("/forum/<int:forum_id>-<slug>")
  61. def view_forum(forum_id, slug=None):
  62. page = request.args.get('page', 1, type=int)
  63. forum, forumsread = Forum.get_forum(forum_id=forum_id, user=current_user)
  64. if forum.external:
  65. return redirect(forum.external)
  66. topics = Forum.get_topics(forum_id=forum.id, user=current_user, page=page,
  67. per_page=flaskbb_config["TOPICS_PER_PAGE"])
  68. return render_template("forum/forum.html", forum=forum, topics=topics,
  69. forumsread=forumsread,)
  70. @forum.route("/topic/<int:topic_id>", methods=["POST", "GET"])
  71. @forum.route("/topic/<int:topic_id>-<slug>", methods=["POST", "GET"])
  72. def view_topic(topic_id, slug=None):
  73. page = request.args.get('page', 1, type=int)
  74. # Fetch some information about the topic
  75. topic = Topic.query.filter_by(id=topic_id).first()
  76. # Count the topic views
  77. topic.views += 1
  78. topic.save()
  79. # fetch the posts in the topic
  80. posts = Post.query.\
  81. join(User, Post.user_id == User.id).\
  82. filter(Post.topic_id == topic.id).\
  83. add_entity(User).\
  84. order_by(Post.id.asc()).\
  85. paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)
  86. # Update the topicsread status if the user hasn't read it
  87. forumsread = None
  88. if current_user.is_authenticated():
  89. forumsread = ForumsRead.query.\
  90. filter_by(user_id=current_user.id,
  91. forum_id=topic.forum.id).first()
  92. topic.update_read(current_user, topic.forum, forumsread)
  93. form = None
  94. if can_post_reply(user=current_user, topic=topic):
  95. form = QuickreplyForm()
  96. if form.validate_on_submit():
  97. post = form.save(current_user, topic)
  98. return view_post(post.id)
  99. return render_template("forum/topic.html", topic=topic, posts=posts,
  100. last_seen=time_diff(), form=form)
  101. @forum.route("/post/<int:post_id>")
  102. def view_post(post_id):
  103. post = Post.query.filter_by(id=post_id).first_or_404()
  104. count = post.topic.post_count
  105. page = count / flaskbb_config["POSTS_PER_PAGE"]
  106. if count > flaskbb_config["POSTS_PER_PAGE"]:
  107. page += 1
  108. else:
  109. page = 1
  110. return redirect(post.topic.url + "?page=%d#pid%s" % (page, post.id))
  111. @forum.route("/<int:forum_id>/topic/new", methods=["POST", "GET"])
  112. @forum.route("/<int:forum_id>-<slug>/topic/new", methods=["POST", "GET"])
  113. @login_required
  114. def new_topic(forum_id, slug=None):
  115. forum = Forum.query.filter_by(id=forum_id).first_or_404()
  116. if not can_post_topic(user=current_user, forum=forum):
  117. flash("You do not have the permissions to create a new topic.", "danger")
  118. return redirect(forum.url)
  119. form = NewTopicForm()
  120. if form.validate_on_submit():
  121. if request.form['button'] == 'preview':
  122. return render_template("forum/new_topic.html", forum=forum,
  123. form=form, preview=form.content.data)
  124. else:
  125. topic = form.save(current_user, forum)
  126. # redirect to the new topic
  127. return redirect(url_for('forum.view_topic', topic_id=topic.id))
  128. return render_template("forum/new_topic.html", forum=forum, form=form)
  129. @forum.route("/topic/<int:topic_id>/delete")
  130. @forum.route("/topic/<int:topic_id>-<slug>/delete")
  131. @login_required
  132. def delete_topic(topic_id, slug=None):
  133. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  134. if not can_delete_topic(user=current_user, topic=topic):
  135. flash("You do not have the permissions to delete the topic", "danger")
  136. return redirect(topic.forum.url)
  137. involved_users = User.query.filter(Post.topic_id == topic.id,
  138. User.id == Post.user_id).all()
  139. topic.delete(users=involved_users)
  140. return redirect(url_for("forum.view_forum", forum_id=topic.forum_id))
  141. @forum.route("/topic/<int:topic_id>/lock")
  142. @forum.route("/topic/<int:topic_id>-<slug>/lock")
  143. @login_required
  144. def lock_topic(topic_id, slug=None):
  145. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  146. # TODO: Bulk lock
  147. if not can_moderate(user=current_user, forum=topic.forum):
  148. flash("You do not have the permissions to lock this topic", "danger")
  149. return redirect(topic.url)
  150. topic.locked = True
  151. topic.save()
  152. return redirect(topic.url)
  153. @forum.route("/topic/<int:topic_id>/unlock")
  154. @forum.route("/topic/<int:topic_id>-<slug>/unlock")
  155. @login_required
  156. def unlock_topic(topic_id, slug=None):
  157. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  158. # TODO: Bulk unlock
  159. # Unlock is basically the same as lock
  160. if not can_moderate(user=current_user, forum=topic.forum):
  161. flash("Yo do not have the permissions to unlock this topic", "danger")
  162. return redirect(topic.url)
  163. topic.locked = False
  164. topic.save()
  165. return redirect(topic.url)
  166. @forum.route("/topic/<int:topic_id>/move/<int:forum_id>")
  167. @forum.route("/topic/<int:topic_id>-<topic_slug>/move/<int:forum_id>-<forum_slug>")
  168. @login_required
  169. def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None):
  170. forum = Forum.query.filter_by(id=forum_id).first_or_404()
  171. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  172. # TODO: Bulk move
  173. if not can_moderate(user=current_user, forum=topic.forum):
  174. flash("You do not have the permissions to move this topic", "danger")
  175. return redirect(forum.url)
  176. if not topic.move(forum):
  177. flash("Could not move the topic to forum %s" % forum.title, "danger")
  178. return redirect(topic.url)
  179. flash("Topic was moved to forum %s" % forum.title, "success")
  180. return redirect(topic.url)
  181. @forum.route("/topic/<int:old_id>/merge/<int:new_id>")
  182. @forum.route("/topic/<int:old_id>-<old_slug>/merge/<int:new_id>-<new_slug>")
  183. @login_required
  184. def merge_topic(old_id, new_id, old_slug=None, new_slug=None):
  185. old_topic = Topic.query.filter_by(id=old_id).first_or_404()
  186. new_topic = Topic.query.filter_by(id=new_id).first_or_404()
  187. # TODO: Bulk merge
  188. if not can_moderate(user=current_user, forum=old_topic.forum):
  189. flash("Yo do not have the permissions to merge this topic", "danger")
  190. return redirect(old_topic.url)
  191. if not old_topic.merge(new_topic):
  192. flash("Could not merge the topic.", "danger")
  193. return redirect(old_topic.url)
  194. flash("Topic succesfully merged.", "success")
  195. return redirect(new_topic.url)
  196. @forum.route("/topic/<int:topic_id>/post/new", methods=["POST", "GET"])
  197. @forum.route("/topic/<int:topic_id>-<slug>/post/new", methods=["POST", "GET"])
  198. @login_required
  199. def new_post(topic_id, slug=None):
  200. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  201. if not can_post_reply(user=current_user, topic=topic):
  202. flash("You do not have the permissions to post here", "danger")
  203. return redirect(topic.forum.url)
  204. form = ReplyForm()
  205. if form.validate_on_submit():
  206. if request.form['button'] == 'preview':
  207. return render_template("forum/new_post.html", topic=topic,
  208. form=form, preview=form.content.data)
  209. else:
  210. post = form.save(current_user, topic)
  211. return view_post(post.id)
  212. return render_template("forum/new_post.html", topic=topic, form=form)
  213. @forum.route("/topic/<int:topic_id>/post/<int:post_id>/reply", methods=["POST", "GET"])
  214. @login_required
  215. def reply_post(topic_id, post_id):
  216. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  217. post = Post.query.filter_by(id=post_id).first_or_404()
  218. if not can_post_reply(user=current_user, topic=topic):
  219. flash("You do not have the permissions to post in this topic", "danger")
  220. return redirect(topic.forum.url)
  221. form = ReplyForm()
  222. if form.validate_on_submit():
  223. if request.form['button'] == 'preview':
  224. return render_template("forum/new_post.html", topic=topic,
  225. form=form, preview=form.content.data)
  226. else:
  227. form.save(current_user, topic)
  228. return redirect(post.topic.url)
  229. else:
  230. form.content.data = format_quote(post)
  231. return render_template("forum/new_post.html", topic=post.topic, form=form)
  232. @forum.route("/post/<int:post_id>/edit", methods=["POST", "GET"])
  233. @login_required
  234. def edit_post(post_id):
  235. post = Post.query.filter_by(id=post_id).first_or_404()
  236. if not can_edit_post(user=current_user, post=post):
  237. flash("You do not have the permissions to edit this post", "danger")
  238. return redirect(post.topic.url)
  239. form = ReplyForm()
  240. if form.validate_on_submit():
  241. if request.form['button'] == 'preview':
  242. return render_template("forum/new_post.html", topic=post.topic,
  243. form=form, preview=form.content.data)
  244. else:
  245. form.populate_obj(post)
  246. post.date_modified = datetime.datetime.utcnow()
  247. post.modified_by = current_user.username
  248. post.save()
  249. return redirect(post.topic.url)
  250. else:
  251. form.content.data = post.content
  252. return render_template("forum/new_post.html", topic=post.topic, form=form)
  253. @forum.route("/post/<int:post_id>/delete")
  254. @login_required
  255. def delete_post(post_id, slug=None):
  256. post = Post.query.filter_by(id=post_id).first_or_404()
  257. # TODO: Bulk delete
  258. if not can_delete_post(user=current_user, post=post):
  259. flash("You do not have the permissions to edit this post", "danger")
  260. return redirect(post.topic.url)
  261. first_post = post.first_post
  262. topic_url = post.topic.url
  263. forum_url = post.topic.forum.url
  264. post.delete()
  265. # If the post was the first post in the topic, redirect to the forums
  266. if first_post:
  267. return redirect(forum_url)
  268. return redirect(topic_url)
  269. @forum.route("/post/<int:post_id>/report", methods=["GET", "POST"])
  270. @login_required
  271. def report_post(post_id):
  272. post = Post.query.filter_by(id=post_id).first_or_404()
  273. form = ReportForm()
  274. if form.validate_on_submit():
  275. form.save(current_user, post)
  276. flash("Thanks for reporting!", "success")
  277. return render_template("forum/report_post.html", form=form)
  278. @forum.route("/post/<int:post_id>/raw", methods=["POST", "GET"])
  279. @login_required
  280. def raw_post(post_id):
  281. post = Post.query.filter_by(id=post_id).first_or_404()
  282. return format_quote(post)
  283. @forum.route("/markread")
  284. @forum.route("/<int:forum_id>/markread")
  285. @forum.route("/<int:forum_id>-<slug>/markread")
  286. @login_required
  287. def markread(forum_id=None, slug=None):
  288. # Mark a single forum as read
  289. if forum_id:
  290. forum = Forum.query.filter_by(id=forum_id).first_or_404()
  291. forumsread = ForumsRead.query.filter_by(user_id=current_user.id,
  292. forum_id=forum.id).first()
  293. TopicsRead.query.filter_by(user_id=current_user.id,
  294. forum_id=forum.id).delete()
  295. if not forumsread:
  296. forumsread = ForumsRead()
  297. forumsread.user_id = current_user.id
  298. forumsread.forum_id = forum.id
  299. forumsread.last_read = datetime.datetime.utcnow()
  300. forumsread.cleared = datetime.datetime.utcnow()
  301. db.session.add(forumsread)
  302. db.session.commit()
  303. return redirect(forum.url)
  304. # Mark all forums as read
  305. ForumsRead.query.filter_by(user_id=current_user.id).delete()
  306. TopicsRead.query.filter_by(user_id=current_user.id).delete()
  307. forums = Forum.query.all()
  308. forumsread_list = []
  309. for forum in forums:
  310. forumsread = ForumsRead()
  311. forumsread.user_id = current_user.id
  312. forumsread.forum_id = forum.id
  313. forumsread.last_read = datetime.datetime.utcnow()
  314. forumsread.cleared = datetime.datetime.utcnow()
  315. forumsread_list.append(forumsread)
  316. db.session.add_all(forumsread_list)
  317. db.session.commit()
  318. return redirect(url_for("forum.index"))
  319. @forum.route("/who_is_online")
  320. def who_is_online():
  321. if current_app.config['REDIS_ENABLED']:
  322. online_users = get_online_users()
  323. else:
  324. online_users = User.query.filter(User.lastseen >= time_diff()).all()
  325. return render_template("forum/online_users.html",
  326. online_users=online_users)
  327. @forum.route("/memberlist", methods=['GET', 'POST'])
  328. def memberlist():
  329. page = request.args.get('page', 1, type=int)
  330. search_form = UserSearchForm()
  331. if search_form.validate():
  332. users = search_form.get_results().\
  333. paginate(page, flaskbb_config['USERS_PER_PAGE'], False)
  334. return render_template("forum/memberlist.html", users=users,
  335. search_form=search_form)
  336. else:
  337. users = User.query.\
  338. paginate(page, flaskbb_config['USERS_PER_PAGE'], False)
  339. return render_template("forum/memberlist.html", users=users,
  340. search_form=search_form)
  341. @forum.route("/topictracker")
  342. @login_required
  343. def topictracker():
  344. page = request.args.get("page", 1, type=int)
  345. topics = current_user.tracked_topics.\
  346. outerjoin(TopicsRead,
  347. db.and_(TopicsRead.topic_id == Topic.id,
  348. TopicsRead.user_id == current_user.id)).\
  349. add_entity(TopicsRead).\
  350. order_by(Post.id.desc()).\
  351. paginate(page, flaskbb_config['TOPICS_PER_PAGE'], True)
  352. return render_template("forum/topictracker.html", topics=topics)
  353. @forum.route("/topictracker/<int:topic_id>/add")
  354. @forum.route("/topictracker/<int:topic_id>-<slug>/add")
  355. @login_required
  356. def track_topic(topic_id, slug=None):
  357. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  358. current_user.track_topic(topic)
  359. current_user.save()
  360. return redirect(topic.url)
  361. @forum.route("/topictracker/<int:topic_id>/delete")
  362. @forum.route("/topictracker/<int:topic_id>-<slug>/delete")
  363. @login_required
  364. def untrack_topic(topic_id, slug=None):
  365. topic = Topic.query.filter_by(id=topic_id).first_or_404()
  366. current_user.untrack_topic(topic)
  367. current_user.save()
  368. return redirect(topic.url)
  369. @forum.route("/search", methods=['GET', 'POST'])
  370. def search():
  371. form = SearchPageForm()
  372. if form.validate_on_submit():
  373. result = form.get_results()
  374. return render_template('forum/search_result.html', form=form,
  375. result=result)
  376. return render_template('forum/search_form.html', form=form)