def get_company_products(): """ Returns products by company """ results = [] company_products = {} companies = get_entity_dict(query_resource('companies')) products = query_resource('products') for product in products: for company in product.get('companies', []): company_product = company_products.get(company, {}) products = company_product.get('products', []) products.append(product) company_product['products'] = products company_products[company] = company_product for _id, details in company_products.items(): if companies.get(ObjectId(_id)): results.append({ '_id': _id, 'name': companies[ObjectId(_id)]['name'], 'is_enabled': companies[ObjectId(_id)]['is_enabled'], 'products': details.get('products', []) }) sorted_results = sorted(results, key=lambda k: k['name']) return {'results': sorted_results, 'name': gettext('Products per company')}
def get_company_saved_searches(): """ Returns number of saved searches by company """ results = [] company_topics = defaultdict(int) companies = get_entity_dict(query_resource('companies')) users = get_entity_dict(query_resource('users')) topics = query_resource('topics') for topic in topics: company = users.get(topic.get('user', ''), {}).get('company') if company: company_topics[company] += 1 for _id, topic_count in company_topics.items(): results.append({ '_id': _id, 'name': companies.get(_id, {}).get('name'), 'is_enabled': companies.get(_id, {}).get('is_enabled'), 'topic_count': topic_count }) sorted_results = sorted(results, key=lambda k: k['name']) return { 'results': sorted_results, 'name': gettext('Saved searches per company') }
def settings(): data = { 'companies': list(query_resource('companies', max_results=200)), 'services': app.config['SERVICES'], 'products': list(query_resource('products', max_results=200)), } return flask.render_template('settings.html', setting_type="companies", data=data)
def get_settings_data(): return { 'companies': list(query_resource('companies', max_results=200)), 'services': app.config['SERVICES'], 'products': list(query_resource('products', max_results=200)), 'sections': app.sections, }
def get_settings_data(): return { 'products': list(query_resource('products')), 'navigations': list(query_resource('navigations')), 'companies': list(query_resource('companies')), 'sections': [s for s in current_app.sections if s.get('_id') != 'monitoring'], # monitoring has no products }
def get_company_report(): """ Returns products by company """ results = [] company_products = {} companies = get_entity_dict(query_resource('companies')) products = query_resource('products') for product in products: for company in product.get('companies', []): company_product = company_products.get(company, {}) products = company_product.get('products', []) products.append(product) company_product['products'] = products company_products[company] = company_product for _id, details in company_products.items(): users = list(query_resource('users', lookup={'company': ObjectId(_id)})) if companies.get(ObjectId(_id)): company = companies[ObjectId(_id)] results.append({ '_id': _id, 'name': company['name'], 'is_enabled': company['is_enabled'], 'products': details.get('products', []), 'users': users, 'company': company, 'account_manager': company.get('account_manager'), }) sorted_results = sorted(results, key=lambda k: k['name']) return {'results': sorted_results, 'name': gettext('Company')}
def get_settings_data(): return { 'products': list(query_resource('products', max_results=200)), 'navigations': list(query_resource('navigations', max_results=200)), 'companies': list(query_resource('companies', max_results=200)), 'sections': current_app.sections, }
def get_user_saved_searches(): """ Returns number of saved searches by user """ results = [] user_topics = defaultdict(int) companies = get_entity_dict(query_resource('companies')) users = get_entity_dict(query_resource('users')) topics = query_resource('topics') for topic in topics: company = users.get(topic.get('user', ''), {}).get('company') if company: user_topics[topic['user']] += 1 for _id, topic_count in user_topics.items(): results.append({ '_id': _id, 'name': '{} {}'.format( users.get(_id, {}).get('first_name'), users.get(_id, {}).get('last_name')), 'is_enabled': users.get(_id, {}).get('is_enabled'), 'company': companies.get(users.get(_id, {}).get('company', ''), {}).get('name'), 'topic_count': topic_count }) sorted_results = sorted(results, key=lambda k: k['name']) return { 'results': sorted_results, 'name': gettext('Saved searches per user') }
def settings(): data = { 'users': list(query_resource('users', max_results=200)), "companies": list(query_resource('companies', max_results=200)), } return flask.render_template('settings.html', setting_type="users", data=data)
def settings(): data = { 'products': list(query_resource('products', max_results=200)), "navigations": list(query_resource('navigations', max_results=200)), } return flask.render_template('settings.html', setting_type="navigations", data=data)
def get_company_api_usage(): args = deepcopy(request.args.to_dict()) date_range = get_date_filters(args) if not date_range.get('gt') and date_range.get('lt'): abort(400, 'No date range specified.') source = {} must_terms = [{"range": {"created": date_range}}] source['query'] = {'bool': {'must': must_terms}} source['sort'] = [{'created': 'desc'}] source['size'] = 200 source['from'] = int(args.get('from', 0)) source['aggs'] = { "items": { "aggs": { "endpoints": { "terms": { "size": 0, "field": "endpoint" } } }, "terms": { "size": 0, "field": "subscriber" } } } company_ids = [t['company'] for t in query_resource(API_TOKENS)] source['query']['bool']['must'].append( {"terms": { "subscriber": company_ids }}) companies = get_entity_dict(query_resource( 'companies', lookup={'_id': { '$in': company_ids }}), str_id=True) req = ParsedRequest() req.args = {'source': json.dumps(source)} if source['from'] >= 1000: # https://www.elastic.co/guide/en/elasticsearch/guide/current/pagination.html#pagination return abort(400) unique_endpoints = [] search_result = superdesk.get_resource_service('api_audit').get(req, None) results = format_report_results(search_result, unique_endpoints, companies) results = { 'results': results, 'name': gettext('Company News API Usage'), 'result_headers': unique_endpoints, } return results
def get_settings_data(): return { 'products': list(query_resource('products', lookup={'is_enabled': True})), 'cards': list(query_resource('cards')), 'dashboards': current_app.dashboards, 'navigations': list(query_resource('navigations', lookup={'is_enabled': True})) }
def get_settings_data(): return { 'companies': list(query_resource('companies')), 'services': app.config['SERVICES'], 'products': list(query_resource('products')), 'sections': app.sections, 'company_types': get_company_types_options(app.config.get('COMPANY_TYPES', [])), }
def settings(): data = { 'products': list( query_resource('products', lookup={'is_enabled': True}, max_results=200)), "cards": list(query_resource('cards', max_results=200)), } return flask.render_template('settings.html', setting_type="cards", data=data)
def company_users(id): """TODO(petr): use projection to hide fields like token/email.""" users = list( query_resource('users', lookup={'company': ObjectId(id)}, max_results=50)) return jsonify(users), 200
def search(): lookup = None if flask.request.args.get('q'): regex = re.compile('.*{}.*'.format(flask.request.args.get('q')), re.IGNORECASE) lookup = {'$or': [{'first_name': regex}, {'last_name': regex}]} users = list(query_resource('users', lookup=lookup)) return jsonify(users), 200
def search(): lookup = None if flask.request.args.get('q'): regex = re.compile('.*{}.*'.format(flask.request.args.get('q')), re.IGNORECASE) lookup = {'name': regex} products = list(query_resource('navigations', lookup=lookup, max_results=200)) return jsonify(products), 200
def get_home_data(): user = get_user() cards = list(query_resource('cards', lookup={'dashboard': 'newsroom'})) company_id = str(user['company']) if user and user.get('company') else None items_by_card = get_items_by_card(cards) return { 'cards': cards, 'itemsByCard': items_by_card, 'products': get_products_by_company(company_id), 'user': str(user['_id']) if user else None, 'userType': user.get('user_type'), 'company': company_id, 'formats': [{ 'format': f['format'], 'name': f['name'], 'types': f['types'], 'assets': f['assets'] } for f in app.download_formatters.values()], 'context': 'wire', }
def search(): lookup = None if flask.request.args.get('q'): regex = re.compile('.*{}.*'.format(flask.request.args.get('q')), re.IGNORECASE) lookup = {'label': regex} products = list(query_resource('cards', lookup=lookup)) return jsonify(products), 200
def get_settings_data(): return { 'companies': list(query_resource('companies')), 'services': app.config['SERVICES'], 'products': list(query_resource('products')), 'sections': app.sections, 'company_types': get_company_types_options(app.config.get('COMPANY_TYPES', [])), 'api_enabled': app.config.get('NEWS_API_ENABLED', False), 'ui_config': get_resource_service('ui_config').getSectionConfig('companies'), }
def search(): lookup = None if flask.request.args.get('q'): regex = re.compile('.*{}.*'.format(flask.request.args.get('q')), re.IGNORECASE) lookup = {'name': regex} section_filters = list(query_resource('section_filters', lookup=lookup)) return jsonify(section_filters), 200
def monitoring_companies(): monitoring_list = list( query_resource('monitoring', lookup={'schedule.interval': { '$ne': None }})) companies = get_items_by_id( [ObjectId(m['company']) for m in monitoring_list], 'companies') return jsonify(companies), 200
def get_settings_data(): """Get the settings data for section filter :param context """ data = { 'section_filters': list(query_resource('section_filters')), 'sections': current_app.sections, } return data
def company_reports(): companies = list(query_resource('companies')) data = { 'companies': companies, 'sections': newsroom_app.sections, 'api_enabled': app.config.get('NEWS_API_ENABLED', False), } return render_template('company_reports.html', setting_type="company_reports", data=data)
def save_navigation_products(id): get_entity_or_404(id, 'navigations') data = get_json_or_400() products = list(query_resource('products', max_results=200)) db = app.data.get_mongo_collection('products') for product in products: if str(product['_id']) in data.get('products', []): db.update_one({'_id': product['_id']}, {'$addToSet': {'navigations': id}}) else: db.update_one({'_id': product['_id']}, {'$pull': {'navigations': id}}) return jsonify(), 200
def notify_new_item(item, check_topics=True): if item.get('type') == 'composite': return lookup = {'is_enabled': True} all_users = list(query_resource('users', lookup=lookup, max_results=200)) user_ids = [u['_id'] for u in all_users] users_dict = {str(user['_id']): user for user in all_users} all_companies = list( query_resource('companies', lookup=lookup, max_results=200)) company_ids = [c['_id'] for c in all_companies] companies_dict = { str(company['_id']): company for company in all_companies } push_notification('new_item', item=item['_id']) if check_topics: notify_topic_matches(item, users_dict, companies_dict) notify_user_matches(item, users_dict, companies_dict, user_ids, company_ids)
def update_products(updates, company_id): products = list(query_resource('products')) db = app.data.get_mongo_collection('products') for product in products: if updates.get(str(product['_id'])): db.update_one({'_id': product['_id']}, {'$addToSet': { 'companies': company_id }}) else: db.update_one({'_id': product['_id']}, {'$pull': { 'companies': company_id }})
def get_product_company(): args = deepcopy(request.args.to_dict()) lookup = {'_id': ObjectId(args.get('product'))} if args.get('product') else None products = query_resource('products', lookup=lookup) res = [{'_id': product.get('_id'), 'product': product.get('name'), 'companies': product.get('companies', [])} for product in products] for r in res: r.update(get_company_names(r.get('companies', []))) results = { 'results': res, 'name': gettext('Companies permissioned per product') } return results
def get_home_page_data(): """Get home page data for market place""" user = get_user() navigations = get_navigations_by_company(str(user['company']) if user and user.get('company') else None, product_type=SECTION_ID) get_resource_service(search_endpoint_name).get_navigation_story_count(navigations) return { 'user': str(user['_id']) if user else None, 'company': str(user['company']) if user and user.get('company') else None, 'navigations': navigations, 'cards': list(query_resource('cards', lookup={'dashboard': SECTION_ID})), 'saved_items': get_bookmarks_count(user['_id'], SECTION_ID), 'context': SECTION_ID, 'home_page': True, 'title': SECTION_NAME }
def get_history_users(item_ids, active_user_ids, active_company_ids): lookup = { 'item': { '$in': item_ids }, 'user': { '$in': active_user_ids }, 'company': { '$in': active_company_ids } } histories = query_resource('history', lookup=lookup) user_matches = [str(h['user']) for h in histories] return user_matches