def _mappers_configured(): event_alias = db.aliased(Event) # Event.category_chain -- the category ids of the event, starting # with the root category down to the event's immediate parent. cte = Category.get_tree_cte() query = select([cte.c.path]).where( cte.c.id == Event.category_id).correlate_except(cte).scalar_subquery() Event.category_chain = column_property(query, deferred=True) # Event.detailed_category_chain -- the category chain of the event, starting # with the root category down to the event's immediate parent. cte = Category.get_tree_cte(lambda cat: db.func.json_build_object( 'id', cat.id, 'title', cat.title)) query = select([cte.c.path]).where( cte.c.id == Event.category_id).correlate_except(cte).scalar_subquery() Event.detailed_category_chain = column_property(query, deferred=True) # Event.effective_protection_mode -- the effective protection mode # (public/protected) of the event, even if it's inheriting it from its # parent category query = (select([ db.case( { ProtectionMode.inheriting.value: Category.effective_protection_mode }, else_=Event.protection_mode, value=Event.protection_mode) ]).where( Category.id == Event.category_id).correlate(Event).scalar_subquery()) Event.effective_protection_mode = column_property(query, deferred=True) # Event.series_pos -- the position of the event in its series subquery = (select([ event_alias.id, db.func.row_number().over(order_by=(event_alias.start_dt, event_alias.id)).label('pos') ]).where((event_alias.series_id == Event.series_id) & ~event_alias.is_deleted).correlate(Event).alias()) query = select([subquery.c.pos ]).where(subquery.c.id == Event.id).correlate_except( subquery).scalar_subquery() Event.series_pos = column_property(query, group='series', deferred=True) # Event.series_count -- the number of events in the event's series query = (db.select([db.func.count(event_alias.id) ]).where((event_alias.series_id == Event.series_id) & ~event_alias.is_deleted).correlate_except( event_alias).scalar_subquery()) Event.series_count = column_property(query, group='series', deferred=True) # Event.contributions_count -- the number of contributions in the event from indico.modules.events.contributions.models.contributions import Contribution query = (db.select([db.func.count(Contribution.id) ]).where((Contribution.event_id == Event.id) & ~Contribution.is_deleted).correlate_except( Contribution).scalar_subquery()) Event.contributions_count = db.column_property(query, deferred=True)
def _mappers_configured(): event_alias = db.aliased(Event) # Event.category_chain -- the category ids of the event, starting # with the root category down to the event's immediate parent. cte = Category.get_tree_cte() query = select([cte.c.path]).where(cte.c.id == Event.category_id).correlate_except(cte) Event.category_chain = column_property(query, deferred=True) # Event.effective_protection_mode -- the effective protection mode # (public/protected) of the event, even if it's inheriting it from its # parent category query = (select([db.case({ProtectionMode.inheriting.value: Category.effective_protection_mode}, else_=Event.protection_mode, value=Event.protection_mode)]) .where(Category.id == Event.category_id)) Event.effective_protection_mode = column_property(query, deferred=True) # Event.series_pos -- the position of the event in its series subquery = (select([event_alias.id, db.func.row_number().over(order_by=(event_alias.start_dt, event_alias.id)).label('pos')]) .where((event_alias.series_id == Event.series_id) & ~event_alias.is_deleted) .correlate(Event) .alias()) query = select([subquery.c.pos]).where(subquery.c.id == Event.id).correlate_except(subquery) Event.series_pos = column_property(query, group='series', deferred=True) # Event.series_count -- the number of events in the event's series query = (db.select([db.func.count(event_alias.id)]) .where((event_alias.series_id == Event.series_id) & ~event_alias.is_deleted) .correlate_except(event_alias)) Event.series_count = column_property(query, group='series', deferred=True)
def category_chain_overlaps(cls, category_ids): """ Create a filter that checks whether the event has any of the provided category ids in its parent chain. :param category_ids: A list of category ids or a single category id """ from indico.modules.categories import Category if not isinstance(category_ids, (list, tuple, set)): category_ids = [category_ids] cte = Category.get_tree_cte() return (cte.c.id == Event.category_id) & cte.c.path.overlap(category_ids)
def get_excluded_categories(*, deep=False): """Get excluded category IDs. :param deep: Whether to get all subcategory ids as well """ from indico_livesync.plugin import LiveSyncPlugin ids = { int(x['id']) for x in LiveSyncPlugin.settings.get('excluded_categories') } if not deep or not ids: return ids cte = Category.get_tree_cte() query = (db.session.query(Category.id).join( cte, Category.id == cte.c.id).filter(cte.c.path.overlap(ids), ~cte.c.is_deleted)) return {x.id for x in query}
def _precache_categories(self): cte = Category.get_tree_cte(lambda cat: db.func.json_build_object( 'id', cat.id, 'title', cat.title)) self.categories = dict( db.session.execute(select([cte.c.id, cte.c.path])).fetchall())