def run(self, name, _id): if not content_api.is_enabled(): print('Content API is not enabled.') return if not name and not _id: print('Please provide name or id.') return subscribers_service = superdesk.get_resource_service('subscribers') lookup = {'name': name} if name else {'_id': ObjectId(_id)} subscriber = subscribers_service.find_one(req=None, **lookup) if not subscriber: print('No subscriber found using %s' % lookup) print('Available subscribers:') subscribers = subscribers_service.find({}) for subscriber in subscribers: print(subscriber['name']) return print('TOKEN') print(generate_subscriber_token(subscriber).decode('utf-8')) print('-----')
def publish_content_api(self, doc, subscribers): """ Publish item to content api :param dict doc: content api item :param list subscribers: list of subscribers """ try: if content_api.is_enabled(): get_resource_service('content_api').publish(doc, subscribers) except Exception: logger.exception('Failed to queue item to API for item: {} for action {}'. format(doc[config.ID_FIELD], self.publish_type))
def publish(self, doc, target_media_type=None): """Queue the content for publishing. 1. Get the subscribers. 2. Update the headline of wire stories with the sequence 3. Queue the content for subscribers 4. Queue the content for previously published subscribers if any. 5. Sends notification if no formatter has found for any of the formats configured in Subscriber. 6. If not queued and not formatters then raise exception. :param dict doc: document to publish :param str target_media_type: dictate if the doc being queued is a Takes Package or an Individual Article. Valid values are - Wire, Digital. If Digital then the doc being queued is a Takes Package and if Wire then the doc being queues is an Individual Article. :return bool: if content is queued then True else False :raises PublishQueueError.item_not_queued_error: If the nothing is queued. """ # Step 1 subscribers, subscribers_yet_to_receive, subscriber_codes = self.get_subscribers(doc, target_media_type) # Step 2 if target_media_type == SUBSCRIBER_TYPES.WIRE: self._update_headline_sequence(doc) # Step 3 no_formatters, queued = self.queue_transmission(deepcopy(doc), subscribers, subscriber_codes) # Step 4 if subscribers_yet_to_receive: formatters_not_found, queued_new_subscribers = \ self.queue_transmission(deepcopy(doc), subscribers_yet_to_receive, subscriber_codes) no_formatters.extend(formatters_not_found) queued = queued or queued_new_subscribers # Step 5 self._push_formatter_notification(doc, no_formatters) # Step 6 if not target_media_type and not queued: logger.error('Nothing is saved to publish queue for story: {} for action: {}'. format(doc[config.ID_FIELD], self.publish_type)) # publish to content api if content_api.is_enabled(): get_resource_service('content_api').publish(doc, subscribers) return queued
def filter_subscribers(self, doc, subscribers, target_media_type): """Filter subscribers to whom the current document is going to be delivered. :param doc: Document to publish/kill/correct :param subscribers: List of Subscribers that might potentially get this document :param target_media_type: Valid values are - Wire, Digital. :return: List of of filtered subscribers and list of product codes per subscriber. """ filtered_subscribers = [] subscriber_codes = {} existing_products = { p[config.ID_FIELD]: p for p in list( get_resource_service("products").get(req=None, lookup=None)) } global_filters = deepcopy([ gf["cf"] for gf in self.filters.get("content_filters", {}).values() if gf["cf"].get("is_global", True) ]) # apply global filters self.conforms_global_filter(global_filters, doc) for subscriber in subscribers: if target_media_type and subscriber.get( "subscriber_type", "") != SUBSCRIBER_TYPES.ALL: can_send_digital = subscriber[ "subscriber_type"] == SUBSCRIBER_TYPES.DIGITAL if (target_media_type == SUBSCRIBER_TYPES.WIRE and can_send_digital or target_media_type == SUBSCRIBER_TYPES.DIGITAL and not can_send_digital): continue conforms, skip_filters = self.conforms_subscriber_targets( subscriber, doc) if not conforms: continue if not self.conforms_subscriber_global_filter( subscriber, global_filters): continue product_codes = self._get_codes(subscriber) subscriber_added = False subscriber["api_enabled"] = False # validate against direct products result, codes = self._validate_article_for_subscriber( doc, subscriber.get("products"), existing_products) if result: product_codes.extend(codes) if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if content_api.is_enabled(): # validate against api products result, codes = self._validate_article_for_subscriber( doc, subscriber.get("api_products"), existing_products) if result: product_codes.extend(codes) subscriber["api_enabled"] = True if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if skip_filters and not subscriber_added: # if targeted subscriber and has api products then send it to api. if subscriber.get("api_products"): subscriber["api_enabled"] = True filtered_subscribers.append(subscriber) subscriber_added = True # unify the list of codes by removing duplicates if subscriber_added: subscriber_codes[subscriber[config.ID_FIELD]] = list( set(product_codes)) return filtered_subscribers, subscriber_codes
def filter_subscribers(self, doc, subscribers, target_media_type): """Filter subscribers to whom the current document is going to be delivered. :param doc: Document to publish/kill/correct :param subscribers: List of Subscribers that might potentially get this document :param target_media_type: Valid values are - Wire, Digital. :return: List of of filtered subscribers and list of product codes per subscriber. """ filtered_subscribers = [] subscriber_codes = {} req = ParsedRequest() req.args = {'is_global': True} filter_service = get_resource_service('content_filters') existing_products = {p[config.ID_FIELD]: p for p in list(get_resource_service('products').get(req=req, lookup=None))} global_filters = list(filter_service.get(req=req, lookup=None)) # apply global filters self.conforms_global_filter(global_filters, doc) for subscriber in subscribers: if target_media_type and subscriber.get('subscriber_type', '') != SUBSCRIBER_TYPES.ALL: can_send_digital = subscriber['subscriber_type'] == SUBSCRIBER_TYPES.DIGITAL if target_media_type == SUBSCRIBER_TYPES.WIRE and can_send_digital or \ target_media_type == SUBSCRIBER_TYPES.DIGITAL and not can_send_digital: continue conforms, skip_filters = self.conforms_subscriber_targets(subscriber, doc) if not conforms: continue if not self.conforms_subscriber_global_filter(subscriber, global_filters): continue product_codes = self._get_codes(subscriber) subscriber_added = False subscriber['api_enabled'] = False # validate against direct products result, codes = self._validate_article_for_subscriber(doc, subscriber.get('products'), existing_products) if result: product_codes.extend(codes) if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if content_api.is_enabled(): # validate against api products result, codes = self._validate_article_for_subscriber(doc, subscriber.get('api_products'), existing_products) if result: product_codes.extend(codes) subscriber['api_enabled'] = True if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if skip_filters and not subscriber_added: # if targeted subscriber and has api products then send it to api. if subscriber.get('api_products'): subscriber['api_enabled'] = True filtered_subscribers.append(subscriber) subscriber_added = True # unify the list of codes by removing duplicates if subscriber_added: subscriber_codes[subscriber[config.ID_FIELD]] = list(set(product_codes)) return filtered_subscribers, subscriber_codes
def on_fetched(self, docs): if content_api.is_enabled(): for subscriber in docs["_items"]: if not subscriber.get("content_api_token"): subscriber["content_api_token"] = generate_subscriber_token(subscriber)
def filter_subscribers(self, doc, subscribers, target_media_type): """Filter subscribers to whom the current document is going to be delivered. :param doc: Document to publish/kill/correct :param subscribers: List of Subscribers that might potentially get this document :param target_media_type: dictate if the doc being queued is a Takes Package or an Individual Article. Valid values are - Wire, Digital. If Digital then the doc being queued is a Takes Package and if Wire then the doc being queues is an Individual Article. :return: List of of filtered subscribers and list of product codes per subscriber. """ filtered_subscribers = [] subscriber_codes = {} req = ParsedRequest() req.args = {'is_global': True} filter_service = get_resource_service('content_filters') existing_products = { p[config.ID_FIELD]: p for p in list( get_resource_service('products').get(req=req, lookup=None)) } global_filters = list(filter_service.get(req=req, lookup=None)) # apply global filters self.conforms_global_filter(global_filters, doc) for subscriber in subscribers: if target_media_type and subscriber.get( 'subscriber_type', '') != SUBSCRIBER_TYPES.ALL: can_send_takes_packages = subscriber[ 'subscriber_type'] == SUBSCRIBER_TYPES.DIGITAL if target_media_type == SUBSCRIBER_TYPES.WIRE and can_send_takes_packages or \ target_media_type == SUBSCRIBER_TYPES.DIGITAL and not can_send_takes_packages: continue conforms, skip_filters = self.conforms_subscriber_targets( subscriber, doc) if not conforms: continue if not self.conforms_subscriber_global_filter( subscriber, global_filters): continue product_codes = self._get_codes(subscriber) subscriber_added = False subscriber['api_enabled'] = False # validate against direct products result, codes = self._validate_article_for_subscriber( doc, subscriber.get('products'), existing_products) if result: product_codes.extend(codes) if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if content_api.is_enabled(): # validate against api products result, codes = self._validate_article_for_subscriber( doc, subscriber.get('api_products'), existing_products) if result: product_codes.extend(codes) subscriber['api_enabled'] = True if not subscriber_added: filtered_subscribers.append(subscriber) subscriber_added = True if skip_filters and not subscriber_added: # if targeted subscriber and has api products then send it to api. if subscriber.get('api_products'): subscriber['api_enabled'] = True filtered_subscribers.append(subscriber) subscriber_added = True # unify the list of codes by removing duplicates if subscriber_added: subscriber_codes[subscriber[config.ID_FIELD]] = list( set(product_codes)) return filtered_subscribers, subscriber_codes