Ejemplo n.º 1
0
 def get_saved_items_count(self):
     query = _agenda_query()
     user = get_user()
     company = get_user_company(user)
     set_product_query(query, company)
     set_saved_items_query(query, str(user['_id']))
     cursor = self.get_items_by_query(query, size=0)
     return cursor.count()
Ejemplo n.º 2
0
 def get_saved_items_count(self):
     query = _agenda_query()
     get_resource_service('section_filters').apply_section_filter(query, self.section)
     user = get_user()
     company = get_user_company(user)
     set_product_query(query, company, self.section)
     set_saved_items_query(query, str(user['_id']))
     cursor = self.get_items_by_query(query, size=0)
     return cursor.count()
Ejemplo n.º 3
0
    def get(self, req, lookup):
        query = _agenda_query()
        user = get_user()
        company = get_user_company(user)
        try:
            set_product_query(query,
                              company,
                              navigation_id=req.args.get('navigation'))
        except FeaturedQuery:
            return self.featured(req, lookup)

        if req.args.get('q'):
            query['bool']['must'].append(query_string(req.args['q']))

        if req.args.get('id'):
            query['bool']['must'].append({'term': {'_id': req.args['id']}})

        if req.args.get('bookmarks'):
            set_saved_items_query(query, req.args['bookmarks'])

        if req.args.get('date_from'):
            query['bool']['should'].append(_event_date_range(req.args))
            query['bool']['should'].append(_display_date_range(req.args))

        if req.args.get('created_from') or req.args.get('created_to'):
            query['bool']['must'].append(versioncreated_range(req.args))

        source = {'query': query}
        source['sort'] = [{'dates.start': 'asc'}]
        source['size'] = 100  # we should fetch all items for given date
        source['from'] = req.args.get('from', 0, type=int)

        set_post_filter(source, req)

        if source['from'] >= 1000:
            # https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html#pagination
            return abort(400)

        if not source['from'] and not req.args.get(
                'bookmarks'):  # avoid aggregations when handling pagination
            source['aggs'] = aggregations

        internal_req = ParsedRequest()
        internal_req.args = {'source': json.dumps(source)}
        return super().get(internal_req, lookup)
Ejemplo n.º 4
0
    def get(self, req, lookup):
        if req.args.get('featured'):
            return self.get_featured_stories(req, lookup)

        query = _agenda_query()
        user = get_user()
        company = get_user_company(user)
        is_events_only = is_events_only_access(user, company) or req.args.get('eventsOnlyView')
        get_resource_service('section_filters').apply_section_filter(query, self.section)
        product_query = {'bool': {'must': [], 'should': []}}

        set_product_query(
            product_query,
            company,
            self.section,
            navigation_id=req.args.get('navigation'),
            events_only=is_events_only
        )
        query['bool']['must'].append(product_query)

        if req.args.get('q'):
            test_query = {'or': []}
            try:
                q = json.loads(req.args.get('q'))
                if isinstance(q, dict):
                    # used for product testing
                    if q.get('query'):
                        test_query['or'].append(query_string(q.get('query')))
                    if q.get('planning_item_query'):
                        test_query['or'].append(
                            nested_query(
                                'planning_items',
                                planning_items_query_string(q.get('planning_item_query')),
                                name='product_test'
                            )
                        )
                    if test_query['or']:
                        query['bool']['must'].append(test_query)
            except Exception:
                pass

            if not test_query.get('or'):
                query['bool']['must'].append(get_agenda_query(req.args['q'], is_events_only))

        if req.args.get('id'):
            query['bool']['must'].append({'term': {'_id': req.args['id']}})

        if req.args.get('bookmarks'):
            set_saved_items_query(query, req.args['bookmarks'])

        if req.args.get('date_from') or req.args.get('date_to'):
            query['bool']['should'].extend(_event_date_range(req.args))
            if not is_events_only:
                query['bool']['should'].append(_display_date_range(req.args))

        source = {'query': query}
        source['sort'] = [{'dates.start': 'asc'}]
        source['size'] = 100  # we should fetch all items for given date
        source['from'] = req.args.get('from', 0, type=int)

        set_post_filter(source, req, is_events_only)

        if source['from'] >= 1000:
            # https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html#pagination
            return abort(400)

        if not source['from'] and not req.args.get('bookmarks'):  # avoid aggregations when handling pagination
            source['aggs'] = get_agenda_aggregations(is_events_only)

        if not is_admin_or_internal(user):
            _remove_fields(source, PRIVATE_FIELDS)

        if is_events_only:
            # no adhoc planning items and remove planning items and coverages fields
            query['bool']['must'].append({'exists': {'field': 'event_id'}})
            _remove_fields(source, PLANNING_ITEMS_FIELDS)

        internal_req = ParsedRequest()
        internal_req.args = {'source': json.dumps(source)}
        cursor = super().get(internal_req, lookup)

        if req.args.get('date_from') and req.args.get('date_to'):
            date_range = _get_date_filters(req.args)
            for doc in cursor.docs:
                # make the items display on the featured day,
                # it's used in ui instead of dates.start and dates.end
                doc.update({
                    '_display_from': date_range.get('gt'),
                    '_display_to': date_range.get('lt'),
                })
        return cursor