def get_all(cls, user): """Get all categories with all associated forums. It returns a list with tuples. Those tuples are containing the category and their associated forums (whose are stored in a list). For example:: [(<Category 1>, [(<Forum 2>, <ForumsRead>), (<Forum 1>, None)]), (<Category 2>, [(<Forum 3>, None), (<Forum 4>, None)])] :param user: The user object is needed to check if we also need their forumsread object. """ # import Group model locally to avoid cicular imports from flaskbb.user.models import Group if user.is_authenticated(): # get list of user group ids user_groups = [gr.id for gr in user.groups] # filter forums by user groups user_forums = Forum.query.filter(Forum.groups.any( Group.id.in_(user_groups)) ).subquery() forum_alias = aliased(Forum, user_forums) # get all forums = cls.query.join( forum_alias, cls.id == forum_alias.category_id ).outerjoin( ForumsRead, db.and_( ForumsRead.forum_id == forum_alias.id, ForumsRead.user_id == user.id ) ).add_entity( forum_alias ).add_entity( ForumsRead ).order_by( Category.position, Category.id, forum_alias.position ).all() else: guest_group = Group.get_guest_group() # filter forums by guest groups guest_forums = Forum.query.filter( Forum.groups.any(Group.id==guest_group.id) ).subquery() forum_alias = aliased(Forum, guest_forums) forums = cls.query.join( forum_alias, cls.id == forum_alias.category_id ).add_entity( forum_alias ).order_by( Category.position, Category.id, forum_alias.position ).all() return get_categories_and_forums(forums, user)
def get_forums(cls, category_id, user): """Get the forums for the category. It returns a tuple with the category and the forums with their forumsread object are stored in a list. A return value can look like this for a category with two forums:: (<Category 1>, [(<Forum 1>, None), (<Forum 2>, None)]) :param category_id: The category id :param user: The user object is needed to check if we also need their forumsread object. """ from flaskbb.user.models import Group if user.is_authenticated: # get list of user group ids user_groups = [gr.id for gr in user.groups] # filter forums by user groups user_forums = Forum.query.\ filter(Forum.groups.any(Group.id.in_(user_groups))).\ subquery() forum_alias = aliased(Forum, user_forums) forums = cls.query.\ filter(cls.id == category_id).\ join(forum_alias, cls.id == forum_alias.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == forum_alias.id, ForumsRead.user_id == user.id)).\ add_entity(forum_alias).\ add_entity(ForumsRead).\ order_by(forum_alias.position).\ all() else: guest_group = Group.get_guest_group() # filter forums by guest groups guest_forums = Forum.query.\ filter(Forum.groups.any(Group.id == guest_group.id)).\ subquery() forum_alias = aliased(Forum, guest_forums) forums = cls.query.\ filter(cls.id == category_id).\ join(forum_alias, cls.id == forum_alias.category_id).\ add_entity(forum_alias).\ order_by(forum_alias.position).\ all() if not forums: abort(404) return get_forums(forums, user)
def get_all(cls, user): """Get all categories with all associated forums. It returns a list with tuples. Those tuples are containing the category and their associated forums (whose are stored in a list). For example:: [(<Category 1>, [(<Forum 2>, <ForumsRead>), (<Forum 1>, None)]), (<Category 2>, [(<Forum 3>, None), (<Forum 4>, None)])] :param user: The user object is needed to check if we also need their forumsread object. """ # import Group model locally to avoid cicular imports from flaskbb.user.models import Group if user.is_authenticated: # get list of user group ids user_groups = [gr.id for gr in user.groups] # filter forums by user groups user_forums = Forum.query.\ filter(Forum.groups.any(Group.id.in_(user_groups))).\ subquery() forum_alias = aliased(Forum, user_forums) # get all forums = cls.query.\ join(forum_alias, cls.id == forum_alias.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == forum_alias.id, ForumsRead.user_id == user.id)).\ add_entity(forum_alias).\ add_entity(ForumsRead).\ order_by(Category.position, Category.id, forum_alias.position).\ all() else: guest_group = Group.get_guest_group() # filter forums by guest groups guest_forums = Forum.query.\ filter(Forum.groups.any(Group.id == guest_group.id)).\ subquery() forum_alias = aliased(Forum, guest_forums) forums = cls.query.\ join(forum_alias, cls.id == forum_alias.category_id).\ add_entity(forum_alias).\ order_by(Category.position, Category.id, forum_alias.position).\ all() return get_categories_and_forums(forums, user)
def decorated(*args, **kwargs): forum_id = kwargs['forum_id'] if 'forum_id' in kwargs else args[1] from flaskbb.forum.models import Forum from flaskbb.user.models import Group # get list of user group ids if current_user.is_authenticated(): user_groups = [gr.id for gr in current_user.groups] else: user_groups = [Group.get_guest_group().id] user_forums = Forum.query.filter( Forum.id == forum_id, Forum.groups.any(Group.id.in_(user_groups)) ).all() if len(user_forums) < 1: abort(403) return func(*args, **kwargs)
def get_forums(cls, category_id, user): """Get the forums for the category. It returns a tuple with the category and the forums with their forumsread object are stored in a list. A return value can look like this for a category with two forums:: (<Category 1>, [(<Forum 1>, None), (<Forum 2>, None)]) :param category_id: The category id :param user: The user object is needed to check if we also need their forumsread object. """ from flaskbb.user.models import Group if user.is_authenticated(): # get list of user group ids user_groups = [gr.id for gr in user.groups] # filter forums by user groups user_forums = Forum.query.filter( Forum.groups.any(Group.id.in_(user_groups))).subquery() forum_alias = aliased(Forum, user_forums) forums = cls.query.filter(cls.id == category_id).join( forum_alias, cls.id == forum_alias.category_id).outerjoin( ForumsRead, db.and_(ForumsRead.forum_id == forum_alias.id, ForumsRead.user_id == user.id)).add_entity( forum_alias).add_entity(ForumsRead).order_by( forum_alias.position).all() else: guest_group = Group.get_guest_group() # filter forums by guest groups guest_forums = Forum.query.filter( Forum.groups.any(Group.id == guest_group.id)).subquery() forum_alias = aliased(Forum, guest_forums) forums = cls.query.filter(cls.id == category_id).join( forum_alias, cls.id == forum_alias.category_id).add_entity( forum_alias).order_by(forum_alias.position).all() if not forums: abort(404) return get_forums(forums, user)