def is_visible_in(cls, category_id): """ Create a filter that checks whether the event is visible in the specified category. """ cte = Category.get_visible_categories_cte(category_id) return (db.exists(db.select([1])) .where(db.and_(cte.c.id == Event.category_id, db.or_(Event.visibility.is_(None), Event.visibility > cte.c.level))))