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. """ if user.is_authenticated(): forums = cls.query.\ join(Forum, cls.id == Forum.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == Forum.id, ForumsRead.user_id == user.id)).\ add_entity(Forum).\ add_entity(ForumsRead).\ order_by(Category.position, Category.id, Forum.position).\ all() else: # Get all the forums forums = cls.query.\ join(Forum, cls.id == Forum.category_id).\ add_entity(Forum).\ order_by(Category.position, Category.id, Forum.position).\ all() return get_categories_and_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. """ if user.is_authenticated(): forums = cls.query.\ join(Forum, cls.id == Forum.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == Forum.id, ForumsRead.user_id == user.id)).\ add_entity(Forum).\ add_entity(ForumsRead).\ order_by(Category.id, Category.position, Forum.position).\ all() else: # Get all the forums forums = cls.query.\ join(Forum, cls.id == Forum.category_id).\ add_entity(Forum).\ order_by(Category.id, Category.position, Forum.position).\ all() return get_categories_and_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 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)