def view_category(category_id, slug=None): if current_user.is_authenticated(): forum_query = Category.query.\ filter(Category.id == category_id).\ join(Forum, Category.id == Forum.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == Forum.id, ForumsRead.user_id == 1)).\ add_entity(Forum).\ add_entity(ForumsRead).\ order_by(Forum.position).\ all() else: # we do not need to join the ForumsRead because the user isn't # signed in forum_query = Category.query.\ filter(Category.id == category_id).\ join(Forum, Category.id == Forum.category_id).\ add_entity(Forum).\ order_by(Forum.position).\ all() forum_query = [(category, forum, None) for category, forum in forum_query] category = get_forums(forum_query) return render_template("forum/category.html", categories=category)
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 index(): # Get the categories and forums if current_user.is_authenticated(): forum_query = Category.query.\ join(Forum, Category.id == Forum.category_id).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == Forum.id, ForumsRead.user_id == current_user.id)).\ add_entity(Forum).\ add_entity(ForumsRead).\ order_by(Category.id, Category.position, Forum.position).\ all() else: # we do not need to join the ForumsRead because the user isn't # signed in forum_query = Category.query.\ join(Forum, Category.id == Forum.category_id).\ add_entity(Forum).\ order_by(Category.id, Category.position, Forum.position).\ all() forum_query = [(category, forum, None) for category, forum in forum_query] categories = get_forums(forum_query) # 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() # Check if we use redis or not if not current_app.config["REDIS_ENABLED"]: online_users = User.query.filter(User.lastseen >= time_diff()).count() # Because we do not have server side sessions, we cannot check if there # are online guests online_guests = None else: online_users = len(get_online_users()) online_guests = len(get_online_users(guest=True)) 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=online_users, online_guests=online_guests)
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. """ if user.is_authenticated(): forums = cls.query.\ filter(cls.id == category_id).\ 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(Forum.position).\ all() else: forums = cls.query.\ filter(cls.id == category_id).\ join(Forum, cls.id == Forum.category_id).\ add_entity(Forum).\ order_by(Forum.position).\ all() if not forums: abort(404) return get_forums(forums, user)