def get(self, req, lookup): """Return recently used event templates. `limit` query param can be used to override default limit. Default limit is 5. :param req: parsed request :param lookup: additional filter :return: """ if req is None: req = ParsedRequest() limit = req.args.get('limit', type=int) pipeline = [{ '$match': { "template": { '$ne': None } } }, { '$group': { '_id': "$template", } }, { '$sort': { "_created": -1 } }] if limit: pipeline.append({'$limit': limit}) templates_ids = [ _['_id'] for _ in app.data.mongo.pymongo( resource='events').db['events'].aggregate(pipeline) ] templates = list( app.data.mongo.pymongo( resource='events_template').db['events_template'].find( {'_id': { '$in': templates_ids }})) # keep `templates_ids` ordering templates.sort( key=lambda template: templates_ids.index(template['_id'])) # query not used templates templates += app.data.mongo.pymongo( resource='events_template').db['events_template'].find( {'_id': { '$nin': templates_ids }}) return ListCursor(templates)
def get(self, req, lookup): """ Overriding because of a different resource URL and user_id is part of the URL """ req = ParsedRequest() req.where = json.dumps({'$or': [lookup, {'is_global': True}]}) items = list(super().get(req, lookup=None)) for item in items: item['filter'] = decode_filter(item.get('filter')) return ListCursor(items)
def get(self, req, lookup): allowed = [] for resource, config in app.config.get("DOMAIN", {}).items(): for field, field_config in config.get("schema", {}).items(): if field_config.get("allowed"): allowed.append( { "_id": "{}.{}".format(resource, field), "items": [str(item) for item in field_config["allowed"]], } ) return ListCursor(allowed)
def get(self, req, **lookup): args = self._get_request_or_lookup(req, **lookup) if args.get('source'): params = { 'source': args['source'], 'repo': args.get('repo') } if args.get('aggs'): params['aggs'] = args['aggs'] elif args.get('params'): params = self.generate_elastic_query(args) if args.get('aggs'): params['aggs'] = args['aggs'] else: raise SuperdeskApiError.badRequestError('source/query not provided') docs = self.run_query(params, args) if args['return_type'] == 'highcharts_config': report = self.generate_highcharts_config(docs, args) elif args['return_type'] == MIME_TYPES.CSV: report = self.generate_csv(docs, args) elif args['return_type'] == MIME_TYPES.HTML: report = self.generate_html(docs, args) else: report = self.generate_report(docs, args) if 'include_items' in args and int(args['include_items']): report['_items'] = list(docs) if isinstance(report, list): return ListCursor(report) elif isinstance(report, ListCursor): return report elif isinstance(report, ElasticCursor): return report return ListCursor([report])
def get(self, req, lookup): """Search using provider.""" provider = self.get_provider() service = self._get_service(provider) if isinstance(service, str): return superdesk.get_resource_service(service).get(req, lookup) query = self._get_query(req) items = service.find(query) if isinstance(items, list): items = ListCursor(items) for item in items: self._set_item_defaults(item, provider) return items
def get(self, req, lookup): """ Overriding to filter out the providers if they haven't been registered with the application. """ providers = list(super().get(req, lookup)) filtered_providers = [] for provider in providers: if provider['search_provider'] in allowed_search_providers: filtered_providers.append(provider) return ListCursor(filtered_providers)
def get(self, req, lookup): def service(service_id): registered = registered_feeding_services[service_id] return { 'feeding_service': service_id, 'label': getattr(registered, 'label', service_id), 'fields': getattr(registered, 'fields', []), 'field_groups': getattr(registered, 'field_groups', {}), 'force_values': getattr(registered, 'force_values', {}) } return ListCursor( [service(_id) for _id in registered_feeding_services])
def get(self, req, lookup): args = getattr(req, 'args', {}) params = { 'source': json.loads(args.get('source') or '{}'), 'repo': args.get('repo') } request = self.get_parsed_request(params) docs = super().get(request, lookup) report = self.generate_report(docs, params) if 'include_items' in args and int(args['include_items']): report['_items'] = list(docs) return ListCursor([report])
def get(self, req, lookup): planning_types = list(super().get(req, lookup)) merged_planning_types = [] for default_planning_type in DEFAULT_EDITOR: planning_type = next((p for p in planning_types if p.get('name') == default_planning_type.get('name')), None) # If nothing is defined in database for this planning_type, use default if planning_type is None: merged_planning_types.append(default_planning_type) else: self.merge_planning_type(planning_type, default_planning_type) merged_planning_types.append(planning_type) return ListCursor(merged_planning_types)
def get(self, req, lookup): def service(service_id): feeding_service_class = registered_feeding_services[service_id] restricted_parsers = list(restricted_feeding_service_parsers.get(service_id, {}).keys()) return { 'feeding_service': service_id, 'label': getattr(feeding_service_class, 'label', service_id), 'fields': getattr(feeding_service_class, 'fields', []), 'field_groups': getattr(feeding_service_class, 'field_groups', {}), 'parser_restricted_values': restricted_parsers } return ListCursor( [service(_id) for _id in registered_feeding_services] )
def get(self, req, lookup): assert req.args.get('name'), {'name': 1} params = [ ('name_startsWith', req.args.get('name')), ('lang', req.args.get('lang')), ] for feature_class in app.config['GEONAMES_FEATURE_CLASSES']: params.append(('featureClass', feature_class.upper())) json_data = geonames_request('search', params) data = [ format_geoname_item(item) for item in json_data.get('geonames', []) ] return ListCursor(data)
def get(self, req, lookup): resource_def = app.config['DOMAIN']['items'] id_field = versioned_id_field(resource_def) if req and req.args and req.args.get(config.ID_FIELD): version_history = list(super().get_from_mongo( req=ParsedRequest(), lookup={id_field: req.args.get(config.ID_FIELD)})) else: version_history = list(super().get_from_mongo(req=req, lookup=lookup)) for doc in version_history: doc[config.ID_FIELD] = doc[id_field] return ListCursor(version_history)
def get(self, req, lookup): values = self._get_field_values() return ListCursor([{'field': 'anpa_category', 'operators': ['in', 'nin'], 'values': values['anpa_category'], 'value_field': 'qcode' }, {'field': 'urgency', 'operators': ['in', 'nin'], 'values': values['urgency'], 'value_field': 'value' }, {'field': 'genre', 'operators': ['in', 'nin'], 'values': values['genre'], 'value_field': 'value' }, {'field': 'subject', 'operators': ['in', 'nin'], 'values': values['subject'], 'value_field': 'qcode' }, {'field': 'priority', 'operators': ['in', 'nin'], 'values': values['priority'], 'value_field': 'value' }, {'field': 'keywords', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }, {'field': 'slugline', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }, {'field': 'type', 'operators': ['in', 'nin'], 'values': values['type'], 'value_field': 'value' }, {'field': 'source', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }, {'field': 'headline', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }, {'field': 'body_html', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }])
def get(self, req, lookup): """ Overriding to filter out the providers if they haven't been registered with the application. """ providers = list(super().get(req, lookup)) filtered_providers = [] for provider in providers: if provider["search_provider"] not in allowed_search_providers: continue if (req and req.args.get("manage") not in ("1", "true") and not current_user_has_item_privilege( self.datasource, provider)): continue filtered_providers.append(provider) return ListCursor(filtered_providers)
def get(self, req, lookup): """Search using provider.""" provider = self.get_provider(req=req) service = self._get_service(provider) if isinstance(service, str): return superdesk.get_resource_service(service).get(req, lookup) query = self._get_query(req) params = json.loads( req.args['params']) if req.args.get('params') else {} try: items = service.find(query, params) except TypeError: # BC items = service.find(query) if isinstance(items, list): items = ListCursor(items) for item in items: self._set_item_defaults(item, provider) return items
def get(self, req, lookup): """ Version of an article in Legal Archive isn't maintained by Eve. Overriding this to fetch the version history. """ resource_def = app.config['DOMAIN'][LEGAL_ARCHIVE_NAME] id_field = versioned_id_field(resource_def) if req and req.args and req.args.get(config.ID_FIELD): version_history = list(super().get_from_mongo(req=ParsedRequest(), lookup={id_field: req.args.get(config.ID_FIELD)})) else: version_history = list(super().get_from_mongo(req=req, lookup=lookup)) for doc in version_history: doc[config.ID_FIELD] = doc[id_field] self.enhance(doc) return ListCursor(version_history)
def get(self, req, lookup): planning_types = list(super().get(req, lookup)) merged_planning_types = [] for default_planning_type in DEFAULT_EDITOR: planning_type = next((p for p in planning_types if p.get('name') == default_planning_type.get('name')), None) # If nothing is defined in database for this planning_type, use default if planning_type is None: merged_planning_types.append(default_planning_type) else: self.merge_planning_type(planning_type, default_planning_type) merged_planning_types.append(planning_type) if not planning_link_updates_to_coverage(): coverage_type = [t for t in merged_planning_types if t['name'] == 'coverage'][0] coverage_type['editor']['flags']['enabled'] = False return ListCursor(merged_planning_types)
def get(self, req, lookup): assert req.args.get("name"), {"name": 1} params = [ ("name_startsWith", req.args.get("name")), ("lang", req.args.get("lang", "en").split("-")[0]), ("style", req.args.get("style", app.config["GEONAMES_SEARCH_STYLE"])), ] if req.args.get("featureClass"): params.append(("featureClass", req.args.get("featureClass"))) else: for feature_class in app.config["GEONAMES_FEATURE_CLASSES"]: params.append(("featureClass", feature_class.upper())) json_data = geonames_request("search", params) data = [ format_geoname_item(item) for item in json_data.get("geonames", []) ] return ListCursor(data)
def get(self, req, lookup): def service(service_id): feeding_service_class = registered_feeding_services[service_id] restricted_parsers = restricted_feeding_service_parsers.get( service_id, {}) if restricted_parsers is not None: restricted_parsers = list(restricted_parsers.keys()) fields = getattr(feeding_service_class, "fields", []) return { "feeding_service": service_id, "label": getattr(feeding_service_class, "label", service_id), "fields": fields, "field_groups": getattr(feeding_service_class, "field_groups", {}), "parser_restricted_values": restricted_parsers, } return ListCursor( [service(_id) for _id in registered_feeding_services])
def get(self, req, lookup): desks = tuple(super().get(req, lookup)) members_set = set() db_users = app.data.mongo.pymongo("users").db["users"] # find display_name from the users document for each member in desks document for desk in desks: if "members" in desk: users = tuple( db_users.find( { "_id": { "$in": [ member["user"] for member in desk.get("members", []) ] } }, {"display_name": 1})) members_set |= {(m["_id"], m["display_name"]) for m in users} if members_set: members_list = list(members_set) members_list.sort(key=lambda k: k[1].lower()) sorted_members_ids = tuple(m[0] for m in members_list) # sort the members of each desk according to ordered_dict for desk in desks: if "members" in desk: # remove members which don't exist in db desk["members"] = [ member for member in desk["members"] if member["user"] in sorted_members_ids ] # sort member in desk desk["members"].sort( key=lambda x: sorted_members_ids.index(x["user"])) return ListCursor(desks)
def get(self, req, lookup): configs = list(super().get(req, lookup)) merged_configs = [] for report_id, endpoint in registered_reports.items(): service = get_resource_service(endpoint) default_config = deepcopy(getattr(service, 'defaultConfig', {})) for key, val in base_config.items(): if key not in default_config: default_config[key] = val config = next((c for c in configs if c.get('_id') == report_id), None) if config is None: default_config['_id'] = report_id merged_configs.append(default_config) else: self.merge_config(config, default_config) merged_configs.append(config) return ListCursor(merged_configs)
def get(self, req, lookup): desks = tuple(super().get(req, lookup)) members_set = set() db_users = app.data.mongo.pymongo('users').db['users'] # find display_name from the users document for each member in desks document for desk in desks: if 'members' in desk: users = tuple( db_users.find( { '_id': { '$in': [member['user'] for member in desk['members']] } }, {'display_name': 1})) members_set |= {(m['_id'], m['display_name']) for m in users} if members_set: members_list = list(members_set) members_list.sort(key=lambda k: k[1].lower()) sorted_members_ids = tuple(m[0] for m in members_list) # sort the members of each desk according to ordered_dict for desk in desks: if 'members' in desk: # remove members which don't exist in db desk['members'] = [ member for member in desk['members'] if member['user'] in sorted_members_ids ] # sort member in desk desk['members'].sort( key=lambda x: sorted_members_ids.index(x['user'])) return ListCursor(desks)
def featured(self, req, lookup): """Return featured items.""" featured = get_resource_service('agenda_featured').find_one_today() if not featured or not featured.get('items'): return ListCursor([]) query = _agenda_query() query['bool']['must'].append({'terms': {'_id': featured['items']}}) if req.args.get('q'): query['bool']['must'].append(query_string(req.args['q'])) source = {'query': query} set_post_filter(source, req) source['size'] = len(featured['items']) source['from'] = req.args.get('from', 0, type=int) if not source['from']: source['aggs'] = aggregations internal_req = ParsedRequest() internal_req.args = {'source': json.dumps(source)} cursor = super().get(internal_req, lookup) docs_by_id = {} for doc in cursor.docs: docs_by_id[doc['_id']] = doc # make the items display on the featured day, # it's used in ui instead of dates.start and dates.end doc.update({ '_display_from': featured['display_from'], '_display_to': featured['display_to'], }) cursor.docs = [ docs_by_id[_id] for _id in featured['items'] if docs_by_id.get(_id) ] return cursor
def get(self, req, lookup): export_templates = list(super().get(req, lookup)) return ListCursor(export_templates)
def get(self, req, lookup): values = self._get_field_values() fields = [{ 'field': 'anpa_category', 'operators': ['in', 'nin'], 'values': values.get('anpa_category', []), 'value_field': 'qcode' }, { 'field': 'urgency', 'operators': ['in', 'nin', 'eq', 'ne', 'lt', 'lte', 'gt', 'gte'], 'values': values.get('urgency', []), 'value_field': 'qcode' }, { 'field': 'genre', 'operators': ['in', 'nin'], 'values': values.get('genre', []), 'value_field': 'qcode' }, { 'field': 'subject', 'operators': ['in', 'nin'], 'values': values.get('subject', []), 'value_field': 'qcode' }, { 'field': 'priority', 'operators': ['in', 'nin', 'eq', 'ne', 'lt', 'lte', 'gt', 'gte'], 'values': values.get('priority', []), 'value_field': 'qcode' }, { 'field': 'keywords', 'operators': ['in', 'nin'] }, { 'field': 'slugline', 'operators': [ 'in', 'nin', 'eq', 'ne', 'like', 'notlike', 'startswith', 'endswith' ] }, { 'field': 'type', 'operators': ['in', 'nin', 'eq', 'ne'], 'values': values.get('type', []), 'value_field': 'qcode' }, { 'field': 'source', 'operators': [ 'in', 'nin', 'eq', 'ne', 'like', 'notlike', 'startswith', 'endswith' ] }, { 'field': 'headline', 'operators': [ 'in', 'nin', 'eq', 'ne', 'like', 'notlike', 'startswith', 'endswith' ] }, { 'field': 'ednote', 'operators': [ 'in', 'nin', 'eq', 'ne', 'like', 'notlike', 'startswith', 'endswith' ] }, { 'field': 'body_html', 'operators': ['in', 'nin', 'like', 'notlike', 'startswith', 'endswith'] }, { 'field': 'desk', 'operators': ['in', 'nin', 'eq', 'ne'], 'values': values['desk'], 'value_field': '_id' }, { 'field': 'stage', 'operators': ['in', 'nin', 'eq', 'ne'], 'values': values['stage'], 'value_field': '_id' }, { 'field': 'sms', 'operators': ['in', 'nin', 'eq', 'ne'], 'values': values['sms'], 'value_field': 'name' }, { 'field': 'place', 'operators': ['match', 'in', 'nin'], 'values': values['place'], 'value_field': 'qcode' }, { 'field': 'ingest_provider', 'operators': ['eq', 'ne'], 'values': values['ingest_provider'], 'value_field': '_id' }, { 'field': 'embargo', 'operators': ['eq', 'ne'], 'values': values['embargo'], 'value_field': 'name' }, { 'field': 'featuremedia', 'operators': ['exists'], 'values': values['featuremedia'], 'value_field': 'name' }] fields.extend(self._get_vocabulary_fields(values)) return ListCursor(fields)
def get(self, req, lookup): items = list(super().get(req, lookup)) for item in items: item['filter'] = decode_filter(item.get('filter')) return ListCursor(items)
def get(self, req, lookup): return ListCursor()
def featured(self, req, lookup, featured): """Return featured items. :param ParsedRequest req: The parsed in request instance from the endpoint :param dict lookup: The parsed in lookup dictionary from the endpoint :param dict featured: list featured items """ user = get_user() company = get_user_company(user) if is_events_only_access(user, company): abort(403) if not featured or not featured.get('items'): return ListCursor([]) query = _agenda_query() get_resource_service('section_filters').apply_section_filter(query, self.section) planning_items_query = nested_query( 'planning_items', { 'bool': {'must': [{'terms': {'planning_items.guid': featured['items']}}]} }, name='featured' ) if req.args.get('q'): query['bool']['must'].append(query_string(req.args['q'])) planning_items_query['nested']['query']['bool']['must'].append(planning_items_query_string(req.args['q'])) query['bool']['must'].append(planning_items_query) source = {'query': query} set_post_filter(source, req) source['size'] = len(featured['items']) source['from'] = req.args.get('from', 0, type=int) if not source['from']: source['aggs'] = aggregations if company and not is_admin(user) and company.get('events_only', False): # no adhoc planning items and remove planning items and coverages fields query['bool']['must'].append({'exists': {'field': 'event'}}) _remove_fields(source, PLANNING_ITEMS_FIELDS) internal_req = ParsedRequest() internal_req.args = {'source': json.dumps(source)} cursor = self.internal_get(internal_req, lookup) docs_by_id = {} for doc in cursor.docs: for p in (doc.get('planning_items') or []): docs_by_id[p.get('guid')] = doc # make the items display on the featured day, # it's used in ui instead of dates.start and dates.end doc.update({ '_display_from': featured['display_from'], '_display_to': featured['display_to'], }) docs = [] agenda_ids = set() for _id in featured['items']: if docs_by_id.get(_id) and docs_by_id.get(_id).get('_id') not in agenda_ids: docs.append(docs_by_id.get(_id)) agenda_ids.add(docs_by_id.get(_id).get('_id')) cursor.docs = docs return cursor
def get(self, req, lookup): formatters = get_all_formatters() return ListCursor([{'name': type(f).__name__} for f in formatters])
def get(self, req, lookup): """ Return the list of languages defined on config file. """ return ListCursor(app.config.get("LANGUAGES", []))