def highlight_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not can_moderate(user=current_user, 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 unlock_topic(topic_id=None, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not can_moderate(user=current_user, 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 trivialize_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() # Unlock is basically the same as lock if not can_moderate(user=current_user, forum=topic.forum): flash("Yo do not have the permissions to trivialize this topic", "danger") return redirect(topic.url) topic.important = False topic.save() return redirect(topic.url)
def highlight_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() if not can_moderate(user=current_user, 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 trivialize_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() # Unlock is basically the same as lock if not can_moderate(user=current_user, 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 lock_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() # TODO: Bulk lock if not can_moderate(user=current_user, forum=topic.forum): flash("You do not have the permissions to lock this topic", "danger") return redirect(topic.url) topic.locked = True topic.save() return redirect(topic.url)
def unlock_topic(topic_id, slug=None): topic = Topic.query.filter_by(id=topic_id).first_or_404() # TODO: Bulk unlock # Unlock is basically the same as lock if not can_moderate(user=current_user, forum=topic.forum): flash("Yo do not have the permissions to unlock this topic", "danger") return redirect(topic.url) topic.locked = False topic.save() return redirect(topic.url)
def merge_topic(old_id, new_id, old_slug=None, new_slug=None): old_topic = Topic.query.filter_by(id=old_id).first_or_404() new_topic = Topic.query.filter_by(id=new_id).first_or_404() # TODO: Bulk merge if not can_moderate(user=current_user, forum=topic.forum): flash("Yo do not have the permissions to merge this topic", "danger") return redirect(old_topic.url) if not old_topic.merge(new_topic): flash("Could not merge the topic.", "danger") return redirect(old_topic.url) flash("Topic succesfully merged.", "success") return redirect(new_topic.url)
def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None): forum = Forum.query.filter_by(id=forum_id).first_or_404() topic = Topic.query.filter_by(id=topic_id).first_or_404() # TODO: Bulk move if not can_moderate(user=current_user, forum=topic.forum): flash("Yo do not have the permissions to move this topic", "danger") return redirect(forum.url) if not topic.move(forum): flash("Could not move the topic to forum %s" % forum.title, "danger") return redirect(topic.url) flash("Topic was moved to forum %s" % forum.title, "success") return redirect(topic.url)
def merge_topic(old_id, new_id, old_slug=None, new_slug=None): old_topic = Topic.query.filter_by(id=old_id).first_or_404() new_topic = Topic.query.filter_by(id=new_id).first_or_404() # TODO: Bulk merge if not can_moderate(user=current_user, forum=old_topic.forum): flash("Yo do not have the permissions to merge this topic", "danger") return redirect(old_topic.url) if not old_topic.merge(new_topic): flash("Could not merge the topic.", "danger") return redirect(old_topic.url) flash("Topic succesfully merged.", "success") return redirect(new_topic.url)
def move_topic(topic_id, forum_id, topic_slug=None, forum_slug=None): forum = Forum.query.filter_by(id=forum_id).first_or_404() topic = Topic.query.filter_by(id=topic_id).first_or_404() # TODO: Bulk move if not can_moderate(user=current_user, forum=topic.forum): flash("You do not have the permissions to move this topic", "danger") return redirect(forum.url) if not topic.move(forum): flash("Could not move the topic to forum %s" % forum.title, "danger") return redirect(topic.url) flash("Topic was moved to forum %s" % forum.title, "success") return redirect(topic.url)
def merge_topic(old_id, new_id, old_slug=None, new_slug=None): _old_topic = Topic.query.filter_by(id=old_id).first_or_404() _new_topic = Topic.query.filter_by(id=new_id).first_or_404() # TODO: Bulk merge # Looks to me that the user should have permissions on both forums, right? if not can_moderate(user=current_user, forum=_old_topic.forum): flash(_("You do not have the permissions to merge this topic."), "danger") return redirect(_old_topic.url) if not _old_topic.merge(_new_topic): flash(_("Could not merge the topics."), "danger") return redirect(_old_topic.url) flash(_("Topics succesfully merged."), "success") return redirect(_new_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.permissions import can_moderate, can_delete_topic from flaskbb.user.models import User from flaskbb.forum.models import Post modified_topics = 0 if action != "delete": for topic in topics: if not can_moderate(user, topic.forum): flash( _("You do not have the permissions to execute this " "action."), "danger") return False 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 can_delete_topic(user, topic): 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 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.permissions import can_moderate, can_delete_topic from flaskbb.user.models import User from flaskbb.forum.models import Post modified_topics = 0 if action != "delete": for topic in topics: if not can_moderate(user, topic.forum): flash(_("You do not have the permissions to execute this " "action."), "danger") return False 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 can_delete_topic(user, topic): 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 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 can_moderate(current_user, 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() # 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 can_moderate(current_user, forum_instance) or \ not can_moderate(current_user, new_forum): 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 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 can_moderate(current_user, 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() # 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 can_moderate(current_user, forum_instance) or \ not can_moderate(current_user, new_forum): 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, )