def post(self, post_id): post = Post.query.filter(Post.id == post_id).first_or_404() if not Permission(Has("makehidden"), IsAtleastModeratorInForum(forum=post.topic.forum)): flash(_("You do not have permission to hide this post"), "danger") return redirect(post.topic.url) if post.hidden: flash(_("Post is already hidden"), "warning") return redirect(post.topic.url) first_post = post.first_post post.hide(current_user) post.save() if first_post: flash(_("Topic hidden"), "success") else: flash(_("Post hidden"), "success") if post.first_post and not Permission(Has("viewhidden")): return redirect(post.topic.forum.url) return redirect(post.topic.url)
def post(self, topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not Permission(Has('makehidden'), IsAtleastModeratorInForum(forum=topic.forum)): flash(_("You do not have permission to unhide this topic"), "danger") return redirect(topic.url) topic.unhide() topic.save() return redirect(topic.url)
class TrivializeTopic(MethodView): decorators = [allows.requires(IsAtleastModeratorInForum()), login_required] def post(topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() topic.important = False topic.save() return redirect(topic.url)
class UnlockTopic(MethodView): decorators = [allows.requires(IsAtleastModeratorInForum()), login_required] def post(self, topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() topic.locked = False topic.save() return redirect(topic.url)
def unlock_topic(topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not Permission(IsAtleastModeratorInForum(forum=topic.forum)): flash(_("You do not have the permissions to unlock this topic."), "danger") return redirect(topic.url) topic.locked = False topic.save() return redirect(topic.url)
def highlight_topic(topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not Permission(IsAtleastModeratorInForum(forum=topic.forum)): flash(_("You do not have the permissions to highlight this topic."), "danger") return redirect(topic.url) topic.important = True topic.save() return redirect(topic.url)
def post(self, topic_id, slug=None): topic = Topic.query.with_hidden().filter_by(id=topic_id).first_or_404() if not Permission(Has("makehidden"), IsAtleastModeratorInForum(forum=topic.forum)): flash(_("You do not have permission to hide this topic"), "danger") return redirect(topic.url) topic.hide(user=current_user) topic.save() if Permission(Has("viewhidden")): return redirect(topic.url) return redirect(topic.forum.url)
def trivialize_topic(topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() # Unlock is basically the same as lock if not Permission(IsAtleastModeratorInForum(forum=topic.forum)): flash(_("You do not have the permissions to trivialize this topic."), "danger") return redirect(topic.url) topic.important = False topic.save() return redirect(topic.url)
def post(self, post_id): post = Post.query.filter(Post.id == post_id).first_or_404() if not Permission(Has('makehidden'), IsAtleastModeratorInForum(forum=post.topic.forum)): flash(_("You do not have permission to unhide this post"), "danger") return redirect(post.topic.url) if not post.hidden: flash(_("Post is already unhidden"), "warning") redirect(post.topic.url) post.unhide() post.save() flash(_("Post unhidden"), "success") return redirect(post.topic.url)
class TrivializeTopic(MethodView): decorators = [ login_required, allows.requires( IsAtleastModeratorInForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to trivialize this topic"), level="danger", # TODO(anr): consider the referrer -- for now, back to topic endpoint=lambda *a, **k: current_topic.url)), ] def post(self, topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() topic.important = False topic.save() return redirect(topic.url)
def do_topic_action(topics, user, action, reverse): """Executes a specific action for topics. Returns a list with the modified topic objects. :param topics: A iterable with ``Topic`` objects. :param user: The user object which wants to perform the action. :param action: One of the following actions: locked, important and delete. :param reverse: If the action should be done in a reversed way. For example, to unlock a topic, ``reverse`` should be set to ``True``. """ from flaskbb.utils.requirements import (IsAtleastModeratorInForum, CanDeleteTopic) from flaskbb.user.models import User from flaskbb.forum.models import Post if not Permission(IsAtleastModeratorInForum(forum=topics[0].forum)): flash(_("You do not have the permissions to execute this " "action."), "danger") return False modified_topics = 0 if action != "delete": for topic in topics: if getattr(topic, action) and not reverse: continue setattr(topic, action, not reverse) modified_topics += 1 topic.save() elif action == "delete": for topic in topics: if not Permission(CanDeleteTopic): flash( _("You do not have the permissions to delete this " "topic."), "danger") return False involved_users = User.query.filter(Post.topic_id == topic.id, User.id == Post.user_id).all() modified_topics += 1 topic.delete(involved_users) return modified_topics
def post(self, forum_id, slug=None): # noqa: C901 forum_instance, __ = Forum.get_forum(forum_id=forum_id, user=real(current_user)) mod_forum_url = url_for("forum.manage_forum", forum_id=forum_instance.id, slug=forum_instance.slug) ids = request.form.getlist("rowid") tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all() if not len(tmp_topics) > 0: flash( _("In order to perform this action you have to select at " "least one topic."), "danger") return redirect(mod_forum_url) # locking/unlocking if "lock" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="locked", reverse=False) flash(_("%(count)s topics locked.", count=changed), "success") return redirect(mod_forum_url) elif "unlock" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="locked", reverse=True) flash(_("%(count)s topics unlocked.", count=changed), "success") return redirect(mod_forum_url) # highlighting/trivializing elif "highlight" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="important", reverse=False) flash(_("%(count)s topics highlighted.", count=changed), "success") return redirect(mod_forum_url) elif "trivialize" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="important", reverse=True) flash(_("%(count)s topics trivialized.", count=changed), "success") return redirect(mod_forum_url) # deleting elif "delete" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="delete", reverse=False) flash(_("%(count)s topics deleted.", count=changed), "success") return redirect(mod_forum_url) # moving elif "move" in request.form: new_forum_id = request.form.get("forum") if not new_forum_id: flash(_("Please choose a new forum for the topics."), "info") return redirect(mod_forum_url) new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404() # check the permission in the current forum and in the new forum if not Permission( And(IsAtleastModeratorInForum(forum_id=new_forum_id), IsAtleastModeratorInForum(forum=forum_instance))): flash(_("You do not have the permissions to move this topic."), "danger") return redirect(mod_forum_url) if new_forum.move_topics_to(tmp_topics): flash(_("Topics moved."), "success") else: flash(_("Failed to move topics."), "danger") return redirect(mod_forum_url) # hiding/unhiding elif "hide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="hide", reverse=False) flash(_("%(count)s topics hidden.", count=changed), "success") return redirect(mod_forum_url) elif "unhide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="unhide", reverse=False) flash(_("%(count)s topics unhidden.", count=changed), "success") return redirect(mod_forum_url) else: flash(_("Unknown action requested"), "danger") return redirect(mod_forum_url)
class ManageForum(MethodView): decorators = [ login_required, allows.requires( IsAtleastModeratorInForum(), on_fail=FlashAndRedirect( message=_("You are not allowed to manage this forum"), level="danger", endpoint=lambda *a, **k: url_for( "forum.view_forum", forum_id=k["forum_id"], ))), ] def get(self, forum_id, slug=None): forum_instance, forumsread = Forum.get_forum(forum_id=forum_id, user=real(current_user)) if forum_instance.external: return redirect(forum_instance.external) # remove the current forum from the select field (move). available_forums = Forum.query.order_by(Forum.position).all() available_forums.remove(forum_instance) page = request.args.get("page", 1, type=int) 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/edit_forum.html", forum=forum_instance, topics=topics, available_forums=available_forums, forumsread=forumsread, ) # TODO(anr): Clean this up. @_@ def post(self, forum_id, slug=None): # noqa: C901 forum_instance, __ = Forum.get_forum(forum_id=forum_id, user=real(current_user)) mod_forum_url = url_for("forum.manage_forum", forum_id=forum_instance.id, slug=forum_instance.slug) ids = request.form.getlist("rowid") tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all() if not len(tmp_topics) > 0: flash( _("In order to perform this action you have to select at " "least one topic."), "danger") return redirect(mod_forum_url) # locking/unlocking if "lock" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="locked", reverse=False) flash(_("%(count)s topics locked.", count=changed), "success") return redirect(mod_forum_url) elif "unlock" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="locked", reverse=True) flash(_("%(count)s topics unlocked.", count=changed), "success") return redirect(mod_forum_url) # highlighting/trivializing elif "highlight" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="important", reverse=False) flash(_("%(count)s topics highlighted.", count=changed), "success") return redirect(mod_forum_url) elif "trivialize" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="important", reverse=True) flash(_("%(count)s topics trivialized.", count=changed), "success") return redirect(mod_forum_url) # deleting elif "delete" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="delete", reverse=False) flash(_("%(count)s topics deleted.", count=changed), "success") return redirect(mod_forum_url) # moving elif "move" in request.form: new_forum_id = request.form.get("forum") if not new_forum_id: flash(_("Please choose a new forum for the topics."), "info") return redirect(mod_forum_url) new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404() # check the permission in the current forum and in the new forum if not Permission( And(IsAtleastModeratorInForum(forum_id=new_forum_id), IsAtleastModeratorInForum(forum=forum_instance))): flash(_("You do not have the permissions to move this topic."), "danger") return redirect(mod_forum_url) if new_forum.move_topics_to(tmp_topics): flash(_("Topics moved."), "success") else: flash(_("Failed to move topics."), "danger") return redirect(mod_forum_url) # hiding/unhiding elif "hide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="hide", reverse=False) flash(_("%(count)s topics hidden.", count=changed), "success") return redirect(mod_forum_url) elif "unhide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="unhide", reverse=False) flash(_("%(count)s topics unhidden.", count=changed), "success") return redirect(mod_forum_url) else: flash(_("Unknown action requested"), "danger") return redirect(mod_forum_url)
def manage_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) # remove the current forum from the select field (move). available_forums = Forum.query.order_by(Forum.position).all() available_forums.remove(forum_instance) if not Permission(IsAtleastModeratorInForum(forum=forum_instance)): flash(_("You do not have the permissions to moderate this forum."), "danger") return redirect(forum_instance.url) 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"]) mod_forum_url = url_for("forum.manage_forum", forum_id=forum_instance.id, slug=forum_instance.slug) # the code is kind of the same here but it somehow still looks cleaner than # doin some magic if request.method == "POST": ids = request.form.getlist("rowid") tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all() if not len(tmp_topics) > 0: flash( _("In order to perform this action you have to select at " "least one topic."), "danger") return redirect(mod_forum_url) # locking/unlocking if "lock" in request.form: changed = do_topic_action(topics=tmp_topics, user=current_user, action="locked", reverse=False) flash(_("%(count)s topics locked.", count=changed), "success") return redirect(mod_forum_url) elif "unlock" in request.form: changed = do_topic_action(topics=tmp_topics, user=current_user, action="locked", reverse=True) flash(_("%(count)s topics unlocked.", count=changed), "success") return redirect(mod_forum_url) # highlighting/trivializing elif "highlight" in request.form: changed = do_topic_action(topics=tmp_topics, user=current_user, action="important", reverse=False) flash(_("%(count)s topics highlighted.", count=changed), "success") return redirect(mod_forum_url) elif "trivialize" in request.form: changed = do_topic_action(topics=tmp_topics, user=current_user, action="important", reverse=True) flash(_("%(count)s topics trivialized.", count=changed), "success") return redirect(mod_forum_url) # deleting elif "delete" in request.form: changed = do_topic_action(topics=tmp_topics, user=current_user, action="delete", reverse=False) flash(_("%(count)s topics deleted.", count=changed), "success") return redirect(mod_forum_url) # moving elif "move" in request.form: new_forum_id = request.form.get("forum") if not new_forum_id: flash(_("Please choose a new forum for the topics."), "info") return redirect(mod_forum_url) new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404() # check the permission in the current forum and in the new forum if not Permission( And(IsAtleastModeratorInForum(forum_id=new_forum_id), IsAtleastModeratorInForum(forum=forum_instance))): flash(_("You do not have the permissions to move this topic."), "danger") return redirect(mod_forum_url) new_forum.move_topics_to(tmp_topics) return redirect(mod_forum_url) return render_template( "forum/edit_forum.html", forum=forum_instance, topics=topics, available_forums=available_forums, forumsread=forumsread, )
def post(self, forum_id, slug=None): forum_instance, __ = Forum.get_forum(forum_id=forum_id, user=real(current_user)) mod_forum_url = url_for('forum.manage_forum', forum_id=forum_instance.id, slug=forum_instance.slug) ids = request.form.getlist('rowid') tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all() if not len(tmp_topics) > 0: flash( _('In order to perform this action you have to select at ' 'least one topic.'), 'danger') return redirect(mod_forum_url) # locking/unlocking if 'lock' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='locked', reverse=False) flash(_('%(count)s topics locked.', count=changed), 'success') return redirect(mod_forum_url) elif 'unlock' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='locked', reverse=True) flash(_('%(count)s topics unlocked.', count=changed), 'success') return redirect(mod_forum_url) # highlighting/trivializing elif 'highlight' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='important', reverse=False) flash(_('%(count)s topics highlighted.', count=changed), 'success') return redirect(mod_forum_url) elif 'trivialize' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='important', reverse=True) flash(_('%(count)s topics trivialized.', count=changed), 'success') return redirect(mod_forum_url) # deleting elif 'delete' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='delete', reverse=False) flash(_('%(count)s topics deleted.', count=changed), 'success') return redirect(mod_forum_url) # moving elif 'move' in request.form: new_forum_id = request.form.get('forum') if not new_forum_id: flash(_('Please choose a new forum for the topics.'), 'info') return redirect(mod_forum_url) new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404() # check the permission in the current forum and in the new forum if not Permission( And(IsAtleastModeratorInForum(forum_id=new_forum_id), IsAtleastModeratorInForum(forum=forum_instance))): flash(_('You do not have the permissions to move this topic.'), 'danger') return redirect(mod_forum_url) if new_forum.move_topics_to(tmp_topics): flash(_('Topics moved.'), 'success') else: flash(_('Failed to move topics.'), 'danger') return redirect(mod_forum_url) # hiding/unhiding elif "hide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="hide", reverse=False) flash(_("%(count)s topics hidden.", count=changed), "success") return redirect(mod_forum_url) elif "unhide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="unhide", reverse=False) flash(_("%(count)s topics unhidden.", count=changed), "success") return redirect(mod_forum_url) else: flash(_('Unknown action requested'), 'danger') return redirect(mod_forum_url)
class ManageForum(MethodView): decorators = [allows.requires(IsAtleastModeratorInForum()), login_required] def get(self, forum_id, slug=None): forum_instance, forumsread = Forum.get_forum(forum_id=forum_id, user=real(current_user)) if forum_instance.external: return redirect(forum_instance.external) # remove the current forum from the select field (move). available_forums = Forum.query.order_by(Forum.position).all() available_forums.remove(forum_instance) page = request.args.get('page', 1, type=int) 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/edit_forum.html', forum=forum_instance, topics=topics, available_forums=available_forums, forumsread=forumsread, ) def post(self, forum_id, slug=None): forum_instance, __ = Forum.get_forum(forum_id=forum_id, user=real(current_user)) mod_forum_url = url_for('forum.manage_forum', forum_id=forum_instance.id, slug=forum_instance.slug) ids = request.form.getlist('rowid') tmp_topics = Topic.query.filter(Topic.id.in_(ids)).all() if not len(tmp_topics) > 0: flash( _('In order to perform this action you have to select at ' 'least one topic.'), 'danger') return redirect(mod_forum_url) # locking/unlocking if 'lock' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='locked', reverse=False) flash(_('%(count)s topics locked.', count=changed), 'success') return redirect(mod_forum_url) elif 'unlock' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='locked', reverse=True) flash(_('%(count)s topics unlocked.', count=changed), 'success') return redirect(mod_forum_url) # highlighting/trivializing elif 'highlight' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='important', reverse=False) flash(_('%(count)s topics highlighted.', count=changed), 'success') return redirect(mod_forum_url) elif 'trivialize' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='important', reverse=True) flash(_('%(count)s topics trivialized.', count=changed), 'success') return redirect(mod_forum_url) # deleting elif 'delete' in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action='delete', reverse=False) flash(_('%(count)s topics deleted.', count=changed), 'success') return redirect(mod_forum_url) # moving elif 'move' in request.form: new_forum_id = request.form.get('forum') if not new_forum_id: flash(_('Please choose a new forum for the topics.'), 'info') return redirect(mod_forum_url) new_forum = Forum.query.filter_by(id=new_forum_id).first_or_404() # check the permission in the current forum and in the new forum if not Permission( And(IsAtleastModeratorInForum(forum_id=new_forum_id), IsAtleastModeratorInForum(forum=forum_instance))): flash(_('You do not have the permissions to move this topic.'), 'danger') return redirect(mod_forum_url) if new_forum.move_topics_to(tmp_topics): flash(_('Topics moved.'), 'success') else: flash(_('Failed to move topics.'), 'danger') return redirect(mod_forum_url) # hiding/unhiding elif "hide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="hide", reverse=False) flash(_("%(count)s topics hidden.", count=changed), "success") return redirect(mod_forum_url) elif "unhide" in request.form: changed = do_topic_action(topics=tmp_topics, user=real(current_user), action="unhide", reverse=False) flash(_("%(count)s topics unhidden.", count=changed), "success") return redirect(mod_forum_url) else: flash(_('Unknown action requested'), 'danger') return redirect(mod_forum_url)
def do_topic_action(topics, user, action, reverse): # noqa: C901 """Executes a specific action for topics. Returns a list with the modified topic objects. :param topics: A iterable with ``Topic`` objects. :param user: The user object which wants to perform the action. :param action: One of the following actions: locked, important and delete. :param reverse: If the action should be done in a reversed way. For example, to unlock a topic, ``reverse`` should be set to ``True``. """ if not topics: return False from flaskbb.utils.requirements import ( IsAtleastModeratorInForum, CanDeleteTopic, Has, ) if not Permission(IsAtleastModeratorInForum(forum=topics[0].forum)): flash( _("You do not have the permissions to execute this action."), "danger", ) return False modified_topics = 0 if action not in {"delete", "hide", "unhide"}: for topic in topics: if getattr(topic, action) and not reverse: continue setattr(topic, action, not reverse) modified_topics += 1 topic.save() elif action == "delete": if not Permission(CanDeleteTopic): flash( _("You do not have the permissions to delete these topics."), "danger", ) return False for topic in topics: modified_topics += 1 topic.delete() elif action == "hide": if not Permission(Has("makehidden")): flash( _("You do not have the permissions to hide these topics."), "danger", ) return False for topic in topics: if topic.hidden: continue modified_topics += 1 topic.hide(user) elif action == "unhide": if not Permission(Has("makehidden")): flash( _("You do not have the permissions to unhide these topics."), "danger", ) return False for topic in topics: if not topic.hidden: continue modified_topics += 1 topic.unhide() return modified_topics