def conforms_subscriber_targets(self, subscriber, article):
        """
        Checks if the given article has any target information and if it does
        it checks if the subscriber satisfies any of the target information
        :param subscriber: Subscriber to test
        :param article: article
        :return:
            bool: True/False if the article conforms the targets
            bool: True if the given subscriber is specifically targeted, False otherwise
        """
        # If not targeted at all then Return true
        if not BasePublishService().is_targeted(article, 'target_subscribers') and \
                not BasePublishService().is_targeted(article, 'target_types'):
            return True, False

        subscriber_type = subscriber.get('subscriber_type')

        for t in article.get('target_subscribers', []):
            if str(t.get('_id')) == str(subscriber['_id']):
                return True, True

        if subscriber_type:
            for t in article.get('target_types', []):
                if t['qcode'] == subscriber_type and t['allow']:
                    return True, False
                if t['qcode'] != subscriber_type and not t['allow']:
                    return True, False

        # If there's a region target then continue with the subscriber to check products
        if BasePublishService().is_targeted(article, 'target_regions'):
            return True, False

        # Nothing matches so this subscriber doesn't conform
        return False, False
Example #2
0
 def test_is_targeted(self):
     doc = {'headline': 'test'}
     self.assertFalse(BasePublishService().is_targeted(doc))
     doc = {'headline': 'test', 'target_regions': []}
     self.assertFalse(BasePublishService().is_targeted(doc))
     doc = {'headline': 'test', 'target_regions': [{'qcode': 'NSW'}]}
     self.assertTrue(BasePublishService().is_targeted(doc))
     doc = {'headline': 'test', 'target_regions': [], 'target_types': []}
     self.assertFalse(BasePublishService().is_targeted(doc))
     doc = {'headline': 'test', 'target_regions': [], 'target_types': [{'qcode': 'digital'}]}
     self.assertTrue(BasePublishService().is_targeted(doc))
    def test_products(self, article, lookup=None):
        req = ParsedRequest()
        results = []
        products = list(get_resource_service('products').get(req=req, lookup=lookup))
        service = get_enqueue_service(article.get(ITEM_OPERATION, 'publish'))
        for product in products:
            result = {'product_id': product['_id'], 'matched': True, 'name': product.get('name', '')}
            reason = ''
            if not service.conforms_product_targets(product, article):
                # Here it fails to match due to geo restriction
                # story has target_region and product has geo restriction
                result['matched'] = False

                if BasePublishService().is_targeted(article, 'target_regions'):
                    reason = 'Story has target_region'

                if product.get('geo_restrictions'):
                    reason = '{} {}'.format(reason, 'Product has target_region')

            if not service.conforms_content_filter(product, article):
                # Here it fails to match due to content filter
                content_filter = product.get('content_filter')
                filter = service.filters.get('content_filters', {}).get(content_filter['filter_id'], {}).get('cf')
                result['matched'] = False
                reason = 'Story does not match the filter: {}'.format(filter.get('name'))

            result['reason'] = reason
            results.append(result)
        return results
Example #4
0
    def test_products(self, article):
        req = ParsedRequest()
        results = []
        products = list(
            get_resource_service('products').get(req=req, lookup=None))
        for product in products:
            result = {}
            result['product_id'] = product['_id']
            result['matched'] = True

            reason = ''
            if not EnqueueService().conforms_product_targets(product, article):
                # Here it fails to match due to geo restriction
                # story has target_region and product has geo restriction
                result['matched'] = False

                if BasePublishService().is_targeted(article, 'target_regions'):
                    reason = 'Story has target_region'

                if product.get('geo_restrictions'):
                    reason = '{} {}'.format(reason,
                                            'Product has target_region')

            if not EnqueueService().conforms_content_filter(product, article):
                # Here it fails to match due to content filter
                content_filter = product.get('content_filter')
                filter = get_resource_service('content_filters').find_one(
                    req=None, _id=content_filter['filter_id'])
                result['matched'] = False
                reason = 'Story does not match the filter: {}'.format(
                    filter.get('name'))

            result['reason'] = reason
            results.append(result)
        return results
    def _publish_package_items(self, package):
        """
        Publishes all items of a package recursively then publishes the package itself
        :param package: package to publish
        :param updates: payload
        """
        items = self.package_service.get_residrefs(package)
        subscriber_items = {}
        queued = False
        removed_items = []
        if self.publish_type in ['correct', 'kill']:
            removed_items, added_items = self._get_changed_items(
                items, package)
            # we raise error if correction is done on a empty package. Kill is fine.
            if len(removed_items) == len(items) and len(
                    added_items) == 0 and self.publish_type == 'correct':
                raise SuperdeskApiError.badRequestError(
                    "Corrected package cannot be empty!")
            items.extend(added_items)

        if items:
            archive_service = get_resource_service('archive')
            for guid in items:
                package_item = archive_service.find_one(req=None, _id=guid)

                if not package_item:
                    raise SuperdeskApiError.badRequestError(
                        "Package item with id: {} has not been published.".
                        format(guid))

                subscribers, subscriber_codes = self._get_subscribers_for_package_item(
                    package_item)
                digital_item_id = BasePublishService(
                ).get_digital_id_for_package_item(package_item)
                self._extend_subscriber_items(subscriber_items, subscribers,
                                              package_item, digital_item_id,
                                              subscriber_codes)

            for removed_id in removed_items:
                package_item = archive_service.find_one(req=None,
                                                        _id=removed_id)
                subscribers, subscriber_codes = self._get_subscribers_for_package_item(
                    package_item)
                digital_item_id = None
                self._extend_subscriber_items(subscriber_items, subscribers,
                                              package_item, digital_item_id,
                                              subscriber_codes)

            queued = self.publish_package(package,
                                          target_subscribers=subscriber_items)

        return queued
    def conforms_product_targets(self, product, article):
        """
        Checks if the given article has any target information and if it does
        it checks if the product satisfies any of the target information
        :param product: Product to test
        :param article: article
        :return:
            bool: True if the article conforms the targets for the given product
        """
        geo_restrictions = product.get('geo_restrictions')

        # If not targeted at all then Return true
        if not BasePublishService().is_targeted(article, 'target_regions'):
            return geo_restrictions is None

        if geo_restrictions:
            for region in article.get('target_regions', []):
                if region['qcode'] == geo_restrictions and region['allow']:
                    return True
                if region['qcode'] != geo_restrictions and not region['allow']:
                    return True
        return False
Example #7
0
    def test_products(self, article, lookup=None):
        req = ParsedRequest()
        results = []
        products = list(
            get_resource_service("products").get(req=req, lookup=lookup))
        service = get_enqueue_service(article.get(ITEM_OPERATION, "publish"))
        for product in products:
            result = {
                "product_id": product["_id"],
                "matched": True,
                "name": product.get("name", "")
            }
            reason = ""
            if not service.conforms_product_targets(product, article):
                # Here it fails to match due to geo restriction
                # story has target_region and product has geo restriction
                result["matched"] = False

                if BasePublishService().is_targeted(article, "target_regions"):
                    reason = "Story has target_region"

                if product.get("geo_restrictions"):
                    reason = "{} {}".format(reason,
                                            "Product has target_region")

            if not service.conforms_content_filter(product, article):
                # Here it fails to match due to content filter
                content_filter = product.get("content_filter")
                filter = service.filters.get("content_filters", {}).get(
                    content_filter["filter_id"], {}).get("cf")
                result["matched"] = False
                reason = "Story does not match the filter: {}".format(
                    filter.get("name"))

            result["reason"] = reason
            results.append(result)
        return results