Exemple #1
0
 def _precache_categories(self):
     Category.allow_relationship_preloading = True
     Category.preload_relationships(
         Category.query,
         'acl_entries',
         strategy=lambda rel: apply_acl_entry_strategy(
             subqueryload(rel), CategoryPrincipal))
     self._category_cache = Category.query.all()
Exemple #2
0
        def _preload_categories(objs):
            nonlocal preloaded_categories
            obj_types = {type(o) for o in objs}
            assert len(obj_types) == 1
            obj_type = obj_types.pop()

            if obj_type == Event:
                chain_query = db.session.query(Event.category_chain).filter(
                    Event.id.in_(o.id for o in objs))
            elif obj_type == Category:
                chain_query = db.session.query(Category.chain_ids).filter(
                    Category.id.in_(o.id for o in objs))
            elif obj_type == Contribution:
                chain_query = (db.session.query(Event.category_chain).join(
                    Contribution.event).filter(
                        Contribution.id.in_(o.id for o in objs)))
            elif obj_type == Attachment:
                chain_query = (db.session.query(Event.category_chain).join(
                    Attachment.folder).join(AttachmentFolder.event).filter(
                        Attachment.id.in_(o.id for o in objs)))
            elif obj_type == EventNote:
                chain_query = (db.session.query(Event.category_chain).join(
                    EventNote.event).filter(
                        EventNote.id.in_(o.id for o in objs)))
            else:
                raise Exception(f'Unhandled object type: {obj_type}')
            category_ids = set(
                itertools.chain.from_iterable(id for id, in chain_query))
            query = (Category.query.filter(
                Category.id.in_(category_ids)).options(
                    load_only('id', 'parent_id', 'protection_mode')))
            Category.preload_relationships(
                query,
                'acl_entries',
                strategy=lambda rel: _apply_acl_entry_strategy(
                    subqueryload(rel), CategoryPrincipal))
            preloaded_categories |= set(query)