class ViewPost(MethodView): decorators = [ allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that topic"), level="warning", endpoint=lambda *a, **k: current_category.url)) ] def get(self, post_id): """Redirects to a post in a topic.""" post = Post.query.filter_by(id=post_id).first_or_404() post_in_topic = Post.query.filter(Post.topic_id == post.topic_id, Post.id <= post_id).order_by( Post.id.asc()).count() page = int( math.ceil(post_in_topic / float(flaskbb_config["POSTS_PER_PAGE"]))) return redirect( url_for("forum.view_topic", topic_id=post.topic.id, slug=post.topic.slug, page=page, _anchor="pid{}".format(post.id)))
class ViewForum(MethodView): decorators = [ allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that forum"), level="warning", endpoint=lambda *a, **k: current_category.url)) ] def get(self, forum_id, slug=None): page = request.args.get("page", 1, type=int) forum_instance, forumsread = Forum.get_forum(forum_id=forum_id, user=real(current_user)) if forum_instance.external: return redirect(forum_instance.external) topics = Forum.get_topics(forum_id=forum_instance.id, user=real(current_user), page=page, per_page=flaskbb_config["TOPICS_PER_PAGE"]) return render_template( "forum/forum.html", forum=forum_instance, topics=topics, forumsread=forumsread, )
class NewTopic(MethodView): decorators = [ login_required, allows.requires( CanAccessForum(), CanPostTopic, on_fail=FlashAndRedirect( message=_("You are not allowed to post a topic here"), level="warning", endpoint=lambda *a, **k: current_forum.url ) ), ] def get(self, forum_id, slug=None): forum_instance = Forum.query.filter_by(id=forum_id).first_or_404() return render_template( "forum/new_topic.html", forum=forum_instance, form=self.form() ) def post(self, forum_id, slug=None): forum_instance = Forum.query.filter_by(id=forum_id).first_or_404() form = self.form() if form.validate_on_submit(): topic = form.save(real(current_user), forum_instance) return redirect(url_for("forum.view_topic", topic_id=topic.id)) return render_template( "forum/new_topic.html", forum=forum_instance, form=form ) def form(self): current_app.pluggy.hook.flaskbb_form_new_topic(form=NewTopicForm) return NewTopicForm()
class ViewForum(MethodView): decorators = [allows.requires(CanAccessForum())] def get(self, forum_id, slug=None): page = request.args.get('page', 1, type=int) forum_instance, forumsread = Forum.get_forum( forum_id=forum_id, user=real(current_user) ) if forum_instance.external: return redirect(forum_instance.external) topics = Forum.get_topics( forum_id=forum_instance.id, user=real(current_user), page=page, per_page=flaskbb_config['TOPICS_PER_PAGE'] ) return render_template( 'forum/forum.html', forum=forum_instance, topics=topics, forumsread=forumsread, )
class MarkRead(MethodView): decorators = [ login_required, allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that forum"), level="warning", endpoint=lambda *a, **k: current_category.url)), ] def post(self, forum_id=None, slug=None): # Mark a single forum as read if forum_id is not None: forum_instance = Forum.query.filter_by(id=forum_id).first_or_404() forumsread = ForumsRead.query.filter_by( user_id=real(current_user).id, forum_id=forum_instance.id).first() TopicsRead.query.filter_by(user_id=real(current_user).id, forum_id=forum_instance.id).delete() if not forumsread: forumsread = ForumsRead() forumsread.user = real(current_user) forumsread.forum = forum_instance forumsread.last_read = time_utcnow() forumsread.cleared = time_utcnow() db.session.add(forumsread) db.session.commit() flash( _("Forum %(forum)s marked as read.", forum=forum_instance.title), "success") return redirect(forum_instance.url) # Mark all forums as read ForumsRead.query.filter_by(user_id=real(current_user).id).delete() TopicsRead.query.filter_by(user_id=real(current_user).id).delete() forums = Forum.query.all() forumsread_list = [] for forum_instance in forums: forumsread = ForumsRead() forumsread.user = real(current_user) forumsread.forum = forum_instance forumsread.last_read = time_utcnow() forumsread.cleared = time_utcnow() forumsread_list.append(forumsread) db.session.add_all(forumsread_list) db.session.commit() flash(_("All forums marked as read."), "success") return redirect(url_for("forum.index"))
class RawPost(MethodView): decorators = [ login_required, allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that forum"), level="warning", endpoint=lambda *a, **k: current_category.url)), ] def get(self, post_id): post = Post.query.filter_by(id=post_id).first_or_404() return format_quote(username=post.username, content=post.content)
class NewPost(MethodView): decorators = [ login_required, allows.requires(CanAccessForum(), CanPostReply, on_fail=FlashAndRedirect( message=_("You are not allowed to post a reply"), level="warning", endpoint=lambda *a, **k: url_for( "forum.view_topic", topic_id=k["topic_id"], ))), ] def get(self, topic_id, slug=None, post_id=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() form = self.form() if post_id is not None: post = Post.query.filter_by(id=post_id).first_or_404() form.content.data = format_quote(post.username, post.content) return render_template("forum/new_post.html", topic=topic, form=form) def post(self, topic_id, slug=None, post_id=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() form = self.form() # check if topic exists if post_id is not None: post = Post.query.filter_by(id=post_id).first_or_404() if form.validate_on_submit(): try: post = form.save(real(current_user), topic) return redirect(url_for("forum.view_post", post_id=post.id)) except StopNewPost as e: flash(e.reason, "danger") except BaseFlaskBBError as e: flash(e.reason, "warning") except Exception: flash(_("Unrecoverable error while submitting new post")) return render_template("forum/new_post.html", topic=topic, form=form) def form(self): current_app.pluggy.hook.flaskbb_form_new_post(form=ReplyForm) return ReplyForm()
class UntrackTopic(MethodView): decorators = [ login_required, allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that forum"), level="warning", endpoint=lambda *a, **k: current_category.url)), ] def post(self, topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() real(current_user).untrack_topic(topic) real(current_user).save() return redirect(topic.url)
class NewPost(MethodView): decorators = [ login_required, allows.requires(CanAccessForum(), CanPostReply, on_fail=FlashAndRedirect( message=_("You are not allowed to post a reply"), level="warning", endpoint=lambda *a, **k: url_for( "forum.view_topic", topic_id=k["topic_id"], ))), ] def get(self, topic_id, slug=None, post_id=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() form = self.form() if post_id is not None: post = Post.query.filter_by(id=post_id).first_or_404() form.content.data = format_quote(post.username, post.content) return render_template('forum/new_post.html', topic=topic, form=form) def post(self, topic_id, slug=None, post_id=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() form = self.form() # check if topic exists if post_id is not None: post = Post.query.filter_by(id=post_id).first_or_404() if form.validate_on_submit(): if 'preview' in request.form: return render_template('forum/new_post.html', topic=topic, form=form, preview=form.content.data) else: post = form.save(real(current_user), topic) return redirect(url_for('forum.view_post', post_id=post.id)) return render_template('forum/new_post.html', topic=topic, form=form) def form(self): current_app.pluggy.hook.flaskbb_form_new_post(form=ReplyForm) return ReplyForm()
class ViewTopic(MethodView): decorators = [ allows.requires( CanAccessForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to access that topic"), level="warning", endpoint=lambda *a, **k: current_category.url)) ] def get(self, topic_id, slug=None): page = request.args.get("page", 1, type=int) # Fetch some information about the topic topic = Topic.get_topic(topic_id=topic_id, user=real(current_user)) # Count the topic views topic.views += 1 topic.save() # Update the topicsread status if the user hasn't read it forumsread = None if current_user.is_authenticated: forumsread = ForumsRead.query.filter_by( user_id=current_user.id, forum_id=topic.forum_id).first() topic.update_read(real(current_user), topic.forum, forumsread) # fetch the posts in the topic posts = Post.query.outerjoin(User, Post.user_id == User.id).filter( Post.topic_id == topic.id).add_entity(User).order_by( Post.id.asc()).paginate(page, flaskbb_config["POSTS_PER_PAGE"], False) # Abort if there are no posts on this page if len(posts.items) == 0: abort(404) return render_template("forum/topic.html", topic=topic, posts=posts, last_seen=time_diff(), form=self.form()) @allows.requires( CanPostReply, on_fail=FlashAndRedirect( message=_("You are not allowed to post a reply to this topic."), level="warning", endpoint=lambda *a, **k: url_for( "forum.view_topic", topic_id=k["topic_id"], ))) def post(self, topic_id, slug=None): topic = Topic.get_topic(topic_id=topic_id, user=real(current_user)) form = self.form() if not form: flash(_("Cannot post reply"), "warning") return redirect("forum.view_topic", topic_id=topic_id, slug=slug) elif form.validate_on_submit(): post = form.save(real(current_user), topic) return redirect(url_for("forum.view_post", post_id=post.id)) else: for e in form.errors.get("content", []): flash(e, "danger") return redirect( url_for("forum.view_topic", topic_id=topic_id, slug=slug)) def form(self): if Permission(CanPostReply): return QuickreplyForm() return None
@forum.route("/category/<int:category_id>") @forum.route("/category/<int:category_id>-<slug>") def view_category(category_id, slug=None): category, forums = Category.\ get_forums(category_id=category_id, user=current_user) return render_template("forum/category.html", forums=forums, category=category) @forum.route("/forum/<int:forum_id>") @forum.route("/forum/<int:forum_id>-<slug>") @allows.requires(CanAccessForum()) def view_forum(forum_id, slug=None): page = request.args.get('page', 1, type=int) forum_instance, forumsread = Forum.get_forum(forum_id=forum_id, user=current_user) if forum_instance.external: return redirect(forum_instance.external) topics = Forum.get_topics(forum_id=forum_instance.id, user=current_user, page=page, per_page=flaskbb_config["TOPICS_PER_PAGE"]) return render_template(