def prefill_search_products(self, search): """ Prefill the search products :param SearchQuery search: The search query instance """ if search.args.get('requested_products'): products = search.args['requested_products'] if isinstance(products, list): search.requested_products = products elif getattr(products, 'split', None): search.requested_products = list(products.split(',')) else: raise BadParameterValueError( 'Invalid requested_products parameter') if search.is_admin: if len(search.navigation_ids): search.products = get_products_by_navigation( search.navigation_ids, product_type=search.section) else: # admin will see everything by default, # regardless of company products search.products = [] elif search.company: search.products = get_products_by_company( search.company.get('_id'), search.navigation_ids, product_type=search.section) else: search.products = []
def set_product_query(query, company, user=None, navigation_id=None, product_type=None): """ 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 user: user to check against (used for notification checking) :param navigation_id: navigation to filter products :param product_type: product_type to filter products If not provided session user will be checked """ products = None if product_type and company: products = get_products_by_company(company['_id'], product_type=product_type) else: 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) 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 }}) for product in products: if product.get('query'): if product['query'] == '_featured': if navigation_id: # only return featured when nav item is selected raise FeaturedQuery else: query['bool']['should'].append(query_string(product['query'])) query['bool']['minimum_should_match'] = 1 if not query['bool']['should']: abort(403, gettext('Your company doesn\'t have any products defined.'))
def delete(_id): """ Deletes the navigations by given id """ get_entity_or_404(_id, 'navigations') # remove all references from products db = app.data.get_mongo_collection('products') products = get_products_by_navigation(_id) for product in products: db.update_one({'_id': product['_id']}, {'$pull': {'navigations': _id}}) get_resource_service('navigations').delete({'_id': ObjectId(_id)}) return jsonify({'success': True}), 200
def get_navigation_story_count(self, navigations, section, company, user): """Get story count by navigation""" search = SearchQuery() self.prefill_search_args(search) self.prefill_search_items(search) search.section = section search.user = user search.company = company self.apply_section_filter(search) aggs = {'navigations': {'filters': {'filters': {}}}} for navigation in navigations: navigation_id = navigation.get('_id') products = get_products_by_navigation(navigation_id) or [] navigation_filter = { 'bool': { 'should': [], 'minimum_should_match': 1 } } for product in products: if product.get('query'): navigation_filter['bool']['should'].append( query_string(product.get('query'))) if navigation_filter['bool']['should']: aggs['navigations']['filters']['filters'][str( navigation_id)] = navigation_filter source = {'query': search.query, 'aggs': aggs, 'size': 0} req = ParsedRequest() req.args = {'source': json.dumps(source)} try: results = self.internal_get(req, None) buckets = results.hits['aggregations']['navigations']['buckets'] for navigation in navigations: navigation_id = navigation.get('_id') doc_count = buckets.get(str(navigation_id), {}).get('doc_count', 0) if doc_count > 0: navigation['story_count'] = doc_count except Exception as exc: logger.error( 'Error in get_navigation_story_count for query: {}'.format( json.dumps(source)), exc, exc_info=True)
def get_navigation_story_count(self, navigations): """Get story count by navigation""" query = _items_query() get_resource_service('section_filters').apply_section_filter( query, self.section) aggs = {'navigations': {'filters': {'filters': {}}}} for navigation in navigations: navigation_id = navigation.get('_id') products = get_products_by_navigation(navigation_id) or [] navigation_filter = { 'bool': { 'should': [], 'minimum_should_match': 1 } } for product in products: if product.get('query'): navigation_filter['bool']['should'].append( query_string(product.get('query'))) if navigation_filter['bool']['should']: aggs['navigations']['filters']['filters'][str( navigation_id)] = navigation_filter source = {'query': query, 'aggs': aggs, 'size': 0} req = ParsedRequest() req.args = {'source': json.dumps(source)} try: results = super().get(req, None) buckets = results.hits['aggregations']['navigations']['buckets'] for navigation in navigations: navigation_id = navigation.get('_id') doc_count = buckets.get(str(navigation_id), {}).get('doc_count', 0) if doc_count > 0: navigation['story_count'] = doc_count except Exception as exc: logger.error( 'Error in get_navigation_story_count for query: {}'.format( json.dumps(source)), exc, exc_info=True)
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.'))