def test_news_only_filter(client, app): query = get_setting('news_only_filter') assert query is None # reset default filter app.config['NEWS_ONLY_FILTER'] = [] _items = get_json(client, '/wire/search?newsOnly=1')['_items'] assert len(_items) == 3 post_json(client, '/settings/general_settings', {'news_only_filter': 'type:text'}) _items = get_json(client, '/wire/search?newsOnly=1')['_items'] assert len(_items) == 0
def test_general_settings(client, app): app.general_setting('foo', 'Foo', default='bar') assert 'bar' == get_setting('foo') post_json(client, '/settings/general_settings', {'foo': 'baz'}) assert 'baz' == get_setting('foo') post_json(client, '/settings/general_settings', {'foo': ''}) assert 'bar' == get_setting('foo') # without key returns all settings with metadata assert 'foo' in get_setting() assert 'Foo' == get_setting()['foo']['label'] assert 'bar' == get_setting()['foo']['default']
def remove_internal_renditions(item): clean_renditions = dict() # associations featuremedia will contain the internal newsroom renditions, we need to remove these. if ((item.get('associations') or {}).get('featuremedia') or {}).get('renditions'): for key, rendition in\ item['associations']['featuremedia']['renditions'].items(): if key in get_setting('news_api_allowed_renditions').split(','): rendition.pop('media', None) clean_renditions[key] = rendition item['associations']['featuremedia']['renditions'] = clean_renditions for key, meta in item.get('associations', {}).items(): if isinstance(meta, dict): meta.pop('products', None) meta.pop('subscribers', None) return item
def apply_request_filter(self, search): """ Generate the filters from request args :param newsroom.search.SearchQuery search: The search query instance """ super().apply_request_filter(search) if search.args.get('bookmarks'): set_bookmarks_query(search.query, search.args['bookmarks']) if search.args.get('newsOnly') and not ( search.args.get('navigation') or search.args.get('product_type')): news_only_filter = get_setting('news_only_filter') if news_only_filter: search.query['bool']['must_not'].append( query_string(news_only_filter)) elif app.config.get('NEWS_ONLY_FILTERS'): for f in app.config.get('NEWS_ONLY_FILTERS', []): search.query['bool']['must_not'].append(f)
def apply_time_limit_filter(self, search): """ Generate the time limit filter :param SearchQuery search: the search query instance """ if search.is_admin: return limit_days = get_setting( self.limit_days_setting ) if self.limit_days_setting is not None else None if limit_days and search.company and not search.company.get( 'archive_access', False): search.query['bool']['must'].append({ 'range': { 'versioncreated': { 'gte': 'now-%dd/d' % int(limit_days), } } })
def get_version(self, id, version, formatter_name): formatter = self._get_formatter(formatter_name) if not formatter: abort(404) if version: item = get_resource_service('items_versions').find_one( req=None, _id_document=id, version=version) if not item: abort(404) resource_def = app.config['DOMAIN']['items'] id_field = versioned_id_field(resource_def) item['_id'] = item[id_field] else: item = get_resource_service('items').find_one(req=None, _id=id) if not item: abort(404) # Ensure that the item has not expired if utcnow() - timedelta( days=int(get_setting('news_api_time_limit_days'))) > item.get( 'versioncreated', utcnow()): abort(404) ret = formatter.format_item(item) return {'formatted_item': ret, 'mimetype': formatter.MIMETYPE}
def set_product_query(query, company, section, user=None, navigation_id=None, events_only=False): """ Checks the user for admin privileges If user is administrator then there's no filtering If user is not administrator then products apply if user has a company If user is not administrator and has no company then everything will be filtered :param query: search query :param company: company :param section: section i.e. wire, agenda, marketplace etc :param user: user to check against (used for notification checking) :param navigation_id: navigation to filter products :param events_only: From agenda to display events only or not If not provided session user will be checked """ products = None if is_admin(user): if navigation_id: products = get_products_by_navigation(navigation_id) else: return # admin will see everything by default if company: products = get_products_by_company(company['_id'], navigation_id, product_type=section) else: # user does not belong to a company so blocking all stories abort(403, gettext('User does not belong to a company.')) query['bool']['should'] = [] product_ids = [ p['sd_product_id'] for p in products if p.get('sd_product_id') ] if product_ids: query['bool']['should'].append( {'terms': { 'products.code': product_ids }}) # add company type filters (if any) if company and company.get('company_type'): for company_type in app.config.get('COMPANY_TYPES', []): if company_type['id'] == company['company_type']: if company_type.get('wire_must'): query['bool']['must'].append(company_type['wire_must']) if company_type.get('wire_must_not'): query['bool']['must_not'].append( company_type['wire_must_not']) planning_items_should = [] for product in products: if product.get('query'): query['bool']['should'].append(query_string(product['query'])) if product.get('planning_item_query') and not events_only: # form the query for the agenda planning items planning_items_should.append( planning_items_query_string( product.get('planning_item_query'))) if planning_items_should: query['bool']['should'].append( nested_query('planning_items', { 'bool': { 'should': planning_items_should, 'minimum_should_match': 1 } }, name='products')) query['bool']['minimum_should_match'] = 1 wire_time_limit_days = get_setting('wire_time_limit_days') if company and not is_admin(user) and not company.get( 'archive_access', False) and wire_time_limit_days: query['bool']['must'].append({ 'range': { 'versioncreated': { 'gte': 'now-%dd/d' % int(wire_time_limit_days), } } }) if not query['bool']['should']: abort(403, gettext('Your company doesn\'t have any products defined.'))
def get(self, req, lookup, size=25, aggs=True, ignore_latest=False): query = _items_query(ignore_latest) user = get_user() company = get_user_company(user) get_resource_service('section_filters').apply_section_filter( query, self.section) set_product_query(query, company, self.section, navigation_id=req.args.get('navigation')) if req.args.get('q'): query['bool']['must'].append(query_string(req.args['q'])) if req.args.get('newsOnly') and not (req.args.get('navigation') or req.args.get('product_type')): news_only_filter = get_setting('news_only_filter') if news_only_filter: query['bool']['must_not'].append( query_string(news_only_filter)) elif app.config.get('NEWS_ONLY_FILTERS'): for f in app.config.get('NEWS_ONLY_FILTERS', []): query['bool']['must_not'].append(f) if req.args.get('bookmarks'): set_bookmarks_query(query, req.args['bookmarks']) filters = None if req.args.get('filter'): filters = json.loads(req.args['filter']) if not app.config.get('FILTER_BY_POST_FILTER', False): if filters: query['bool']['must'] += _filter_terms(filters) 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'] = [{'versioncreated': 'desc'}] source['size'] = size source['from'] = int(req.args.get('from', 0)) if app.config.get('FILTER_BY_POST_FILTER', False): if filters or req.args.get('created_from') or req.args.get( 'created_to'): source['post_filter'] = {'bool': {'must': []}} if filters: source['post_filter']['bool']['must'] += _filter_terms(filters) if req.args.get('created_from') or req.args.get('created_to'): source['post_filter']['bool']['must'].append( versioncreated_range(req.args)) if source['from'] >= 1000: # https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html#pagination return abort(400) if not source[ 'from'] and aggs: # avoid aggregations when handling pagination source['aggs'] = get_aggregations() internal_req = ParsedRequest() internal_req.args = {'source': json.dumps(source)} return super().get(internal_req, lookup)