예제 #1
0
    def search_contribs(self, q, user, page, category_id, event_id,
                        admin_override_enabled):
        # XXX: Ideally we would search in subcontributions as well, but our pagination
        # does not really work when we do not have a single unique ID

        contrib_filters = [
            Contribution.title_matches(q)
            | Contribution.description_matches(q), ~Contribution.is_deleted,
            ~Event.is_deleted
        ]

        if category_id is not None:
            contrib_filters.append(Event.category_chain_overlaps(category_id))
        if event_id is not None:
            contrib_filters.append(Contribution.event_id == event_id)

        query = (Contribution.query.filter(*contrib_filters).join(
            Contribution.event).options(
                load_only('id', 'session_id', 'event_id', 'protection_mode'),
                undefer(Contribution.effective_protection_mode),
                _apply_acl_entry_strategy(
                    selectinload(Contribution.acl_entries),
                    ContributionPrincipal),
                joinedload(Contribution.session).options(
                    load_only('id', 'protection_mode', 'event_id'),
                    selectinload(Session.acl_entries)),
                contains_eager('event').options(
                    _apply_acl_entry_strategy(selectinload(Event.acl_entries),
                                              EventPrincipal))))

        objs, pagenav = self._paginate(query, page, Contribution.id, user,
                                       admin_override_enabled)

        event_strategy = joinedload(Contribution.event)
        event_strategy.joinedload(Event.own_venue)
        event_strategy.joinedload(Event.own_room).options(
            raiseload('*'), joinedload('location'))
        event_strategy.undefer(Event.detailed_category_chain)

        session_strategy = joinedload(Contribution.session)
        session_strategy.joinedload(Session.own_venue)
        session_strategy.joinedload(Session.own_room).options(
            raiseload('*'), joinedload('location'))

        session_block_strategy = joinedload(Contribution.session_block)
        session_block_strategy.joinedload(SessionBlock.own_venue)
        session_block_strategy.joinedload(SessionBlock.own_room).options(
            raiseload('*'), joinedload('location'))
        session_block_session_strategy = session_block_strategy.joinedload(
            SessionBlock.session)
        session_block_session_strategy.joinedload(Session.own_venue)
        session_block_session_strategy.joinedload(Session.own_room).options(
            raiseload('*'), joinedload('location'))

        query = (Contribution.query.filter(
            Contribution.id.in_(c.id for c in objs)).options(
                selectinload(Contribution.person_links).joinedload(
                    'person').joinedload('user').load_only('is_system'),
                event_strategy,
                session_strategy,
                session_block_strategy,
                joinedload(Contribution.type),
                joinedload(Contribution.own_venue),
                joinedload(Contribution.own_room).options(
                    raiseload('*'), joinedload('location')),
                joinedload(Contribution.timetable_entry),
            ))
        contribs_by_id = {c.id: c for c in query}
        contribs = [contribs_by_id[c.id] for c in objs]

        res = HTMLStrippingContributionSchema(many=True).dump(contribs)
        return pagenav, ContributionResultSchema(many=True).load(res)