def save(self): data = self.data # delete submit and csrf_token from data data.pop('submit', None) data.pop('csrf_token', None) forum = Forum(**data) return forum.save()
def test_would_force_login_for_anon_in_guest_unallowed( self, database, guest, category): with database.session.no_autoflush: forum = Forum(title="no guest", category=category) forum.groups = Group.query.filter(Group.guest == False).all() forum.save() assert utils.should_force_login(guest, forum)
def save(self): data = self.data # remove the button data.pop('submit', None) forum = Forum(**data) # flush SQLA info from created instance so that it can be merged return forum.save()
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 test_forum_save(category, moderator_user): """Test the save forum method""" forum = Forum(title="Test Forum", category_id=category.id) forum.moderators = [moderator_user] forum.save() assert forum.title == "Test Forum" assert forum.moderators == [moderator_user]
def view_forum(forum_id, slug=None): page = request.args.get('page', 1, type=int) forum, forumsread = Forum.get_forum(forum_id=forum_id, user=current_user) topics = Forum.get_topics(forum_id=forum.id, user=current_user, page=page, per_page=current_app.config["TOPICS_PER_PAGE"]) return render_template("forum/forum.html", forum=forum, topics=topics, forumsread=forumsread,)
def save(self): data = self.data # remove the button data.pop('submit', None) forum = Forum(**data) # flush SQLA info from created instance so that it can be merged make_transient(forum) make_transient_to_detached(forum) return forum.save()
def test_topic_merge_other_forum(topic): """You cannot merge a topic with a topic from another forum.""" forum_other = Forum(title="Test Forum 2", category_id=1) forum_other.save() topic_other = Topic(title="Test Topic 2") post_other = Post(content="Test Content 2") topic_other.save(user=topic.user, forum=forum_other, post=post_other) assert not topic.merge(topic_other)
def test_forum_save(category, moderator_user): forum = Forum(title="Test Forum", category_id=category.id) forum.save() assert forum.title == "Test Forum" # Test with adding a moderator forum.save([moderator_user]) for moderator in forum.moderators: assert moderator == moderator_user
def save(self): forum = Forum(title=self.title.data, description=self.description.data, position=self.position.data) if self.is_category.data: forum.is_category = True else: forum.parent_id = self.parent.data.id return forum.save()
def save(self): data = self.data # delete submit and csrf_token from data data.pop('submit', None) data.pop('csrf_token', None) forum = Forum(**data) # flush SQLA info from created instance so that it can be merged make_transient(forum) make_transient_to_detached(forum) return forum.save()
def test_redirects_to_login_with_anon( self, guest, category, request_context, application ): forum = Forum(title="no guest", category=category) forum.groups = Group.query.filter(Group.guest == False).all() # sets current_forum _request_ctx_stack.top.forum = forum result = utils.force_login_if_needed() # use in rather than == because it can contain query params as well assert url_for(application.config["LOGIN_VIEW"]) in result.headers["Location"]
def save(self): forum = Forum(title=self.title.data, description=self.description.data, position=self.position.data, external=self.external.data, show_moderators=self.show_moderators.data, locked=self.locked.data) if self.moderators.data: # is already validated forum.moderators = self.moderators.data forum.category_id = self.category.data.id return forum.save()
def test_topic_move(topic_normal): forum_other = Forum(title="Test Forum 2", category_id=1) forum_other.save() forum_old = Forum.query.filter_by(id=topic_normal.forum_id).first() assert topic_normal.move(forum_other) assert forum_old.topics == [] assert forum_old.topic_count == 0 assert forum_old.post_count == 0 assert forum_other.topic_count == 1 assert forum_other.post_count == 1
def create_test_data(): """ Creates 5 users, 2 categories and 2 forums in each category. It also opens a new topic topic in each forum with a post. """ create_default_groups() create_default_settings() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def create_test_data(): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. """ create_default_groups() create_default_settings() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def test_forum_get_topics(topic, user): forum = topic.forum with current_app.test_request_context(): # Test with logged in user login_user(user) topics = Forum.get_topics(forum_id=forum.id, user=current_user) assert topics.items == [(topic, None)] # Test with logged out user logout_user() topics = Forum.get_topics(forum_id=forum.id, user=current_user) assert topics.items == [(topic, None)]
def test_forum_get_topics(topic, user): forum = topic.forum with current_app.test_request_context(): # Test with logged in user login_user(user) topics = Forum.get_topics(forum_id=forum.id, user=current_user) assert topics.items == [(topic, topic.last_post, None)] # Test with logged out user logout_user() topics = Forum.get_topics(forum_id=forum.id, user=current_user) assert topics.items == [(topic, topic.last_post, None)]
def test_forum_get_forum(forum, user): with current_app.test_request_context(): # Test with logged in user login_user(user) forum_with_forumsread = \ Forum.get_forum(forum_id=forum.id, user=current_user) assert forum_with_forumsread == (forum, None) # Test with logged out user logout_user() forum_with_forumsread = \ Forum.get_forum(forum_id=forum.id, user=current_user) assert forum_with_forumsread == (forum, None)
def test_topic_move(topic): """Tests the topic move method.""" forum_other = Forum(title="Test Forum 2", category_id=1) forum_other.save() forum_old = Forum.query.filter_by(id=topic.forum_id).first() assert topic.move(forum_other) assert forum_old.topics.all() == [] assert forum_old.last_post_id is None assert forum_old.topic_count == 0 assert forum_old.post_count == 0 assert forum_other.last_post_id == topic.last_post_id assert forum_other.topic_count == 1 assert forum_other.post_count == 1
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( "forum/forum.html", forum=forum_instance, topics=topics, forumsread=forumsread, )
def create_welcome_forum(): """This will create the `welcome forum` with a welcome topic.""" if User.query.count() < 1: raise "You need to create the admin user first!" user = User.query.filter_by(id=1).first() category = Category(title="My Category", position=1) category.save() forum = Forum(title="Welcome", description="Your first forum", category_id=category.id) forum.save() topic = Topic(title="Welcome!") post = Post(content="Have fun with your new FlaskBB Forum!") topic.save(user=user, forum=forum, post=post)
def create_welcome_forum(): """ This will create the `welcome forum` that nearly every forum software has after the installation process is finished """ if User.query.count() < 1: raise "You need to create the admin user first!" user = User.query.filter_by(id=1).first() category = Category(title="My Category", position=1) category.save() forum = Forum(title="Welcome", description="Your first forum", category_id=category.id) forum.save() topic = Topic(title="Welcome!") post = Post(content="Have fun with your new FlaskBB Forum!") topic.save(user=user, forum=forum, post=post)
def create_welcome_forum(): """This will create the `welcome forum` with a welcome topic. Returns True if it's created successfully. """ if User.query.count() < 1: return False user = User.query.filter_by(id=1).first() category = Category(title="My Category", position=1) category.save() forum = Forum(title="Welcome", description="Your first forum", category_id=category.id) forum.save() topic = Topic(title="Welcome!") post = Post(content="Have fun with your new FlaskBB Forum!") topic.save(user=user, forum=forum, post=post) return True
def create_test_data(): create_default_groups() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() # create a category category = Forum(is_category=True, title="Test Category", description="Test Description") category.save() # create 2 forums in the category for i in range(1, 3): forum_title = "Test Forum %s " % i forum = Forum(title=forum_title, description="Test Description", parent_id=category.id) forum.save() # Create a subforum subforum_title = "Test Subforum %s " % i subforum = Forum(title=subforum_title, description="Test Description", parent_id=forum.id) subforum.save() user = User.query.filter_by(id=1).first() # create 1 topic in each forum for i in range(2, 6): # Forum ids are not sequential because categories. forum = Forum.query.filter_by(id=i).first() topic = Topic() post = Post() topic.title = "Test Title %s" % (i-1) post.content = "Test Content" topic.save(user=user, forum=forum, post=post)
def index(): categories = Forum.get_categories().all() # Fetch a few stats about the forum user_count = User.query.count() topic_count = Topic.query.count() post_count = Post.query.count() newest_user = User.query.order_by(User.id.desc()).first() return render_template("forum/index.html", categories=categories, user_count=user_count, topic_count=topic_count, post_count=post_count, newest_user=newest_user, online_users=len(get_online_users()), online_guests=len(get_online_users(guest=True)))
def save(self): forum = Forum(title=self.title.data, description=self.description.data, position=self.position.data) if self.moderators.data and not self.is_category.data: forum.moderators = self.moderators.data if self.is_category.data: forum.is_category = True forum.parent_id = None else: forum.parent_id = self.parent.data.id return forum.save()
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 forum(category): forum = Forum(title="Test Forum", category_id=category.id) forum.save() return forum
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() # 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 forum(category, default_settings, default_groups): """A single forum in a category.""" forum = Forum(title="Test Forum", category_id=category.id) forum.groups = default_groups forum.save() return forum
def forum_locked(category, default_settings): """A single locked forum in a category.""" forum = Forum(title="Test Forum", category_id=category.id) forum.locked = True forum.save() return forum
def test_would_force_login_for_anon_in_guest_unallowed(self, guest, category): forum = Forum(title="no guest", category=category) forum.groups = Group.query.filter(Group.guest == False).all() assert utils.should_force_login(guest, forum)
def save(self): data = self.data # remove the button data.pop('submit', None) forum = Forum(**data) return forum.save()
def save(self): forum = Forum(title=self.title.data, description=self.description.data, position=self.position.data, category_id=self.category.data.id) return forum.save()
def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. Returns the amount of created users, categories, forums, topics and posts as a dict. :param users: The number of users. :param categories: The number of categories. :param forums: The number of forums which are created in each category. :param topics: The number of topics which are created in each forum. :param posts: The number of posts which are created in each topic. """ create_default_groups() create_default_settings() data_created = {'users': 0, 'categories': 0, 'forums': 0, 'topics': 0, 'posts': 0} # create 5 users for u in range(1, users + 1): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.activated = True user.save() data_created['users'] += 1 user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # lets send them a few private messages for i in range(1, 3): # TODO pass # create 2 categories for i in range(1, categories + 1): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() data_created['categories'] += 1 # create 2 forums in each category for j in range(1, forums + 1): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() data_created['forums'] += 1 for t in range(1, topics + 1): # create a topic topic = Topic(title="Test Title %s" % j) post = Post(content="Test Content") topic.save(post=post, user=user1, forum=forum) data_created['topics'] += 1 for p in range(1, posts + 1): # create a second post in the forum post = Post(content="Test Post") post.save(user=user2, topic=topic) data_created['posts'] += 1 return data_created
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 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 forum(category): """A single forum in a category.""" forum = Forum(title="Test Forum", category_id=category.id) forum.save() return forum