views.py 18 KB

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