def forwards(self, mongodb_collection, mongodb_database):
     for resource in ("archive", "published"):
         service = get_resource_service(resource)
         for item in mongodb_database[resource].find(
             {"associations": {
                 "$exists": "true",
                 "$nin": [{}, None]
             }}):
             update = False
             associations = {}
             try:
                 for key, val in item["associations"].items():
                     associations[key] = val
                     if val and is_related_content(key) and val.get(
                             "order", None) is None:
                         update = True
                         order = int(key.split("--")[1])
                         associations[key]["order"] = order
             except AttributeError:
                 pass
             if update:
                 try:
                     _id = ObjectId(item["_id"])
                 except InvalidId:
                     _id = item["_id"]
                 service.system_update(_id, {"associations": associations},
                                       item)
Example #2
0
    def _raise_if_unpublished_related_items(self, original):
        if not request:
            return

        if (
            config.PUBLISH_ASSOCIATED_ITEMS
            or not original.get(ASSOCIATIONS)
            or self.publish_type not in [ITEM_PUBLISH, ITEM_CORRECT]
        ):
            return

        archive_service = get_resource_service("archive")
        publishing_warnings_confirmed = strtobool(request.args.get("publishing_warnings_confirmed") or "False")

        if not publishing_warnings_confirmed:
            for key, associated_item in original.get(ASSOCIATIONS).items():
                if associated_item and is_related_content(key):
                    item = archive_service.find_one(req=None, _id=associated_item.get("_id"))
                    item = item if item else associated_item

                    if item.get("state") not in PUBLISH_STATES:
                        error_msg = json.dumps(
                            {
                                "warnings": [
                                    _(
                                        "There are unpublished related "
                                        + "items that won't be sent out as "
                                        + "related items. Do you want to publish the article anyway?"
                                    )
                                ]
                            }
                        )
                        raise ValidationError(error_msg)
Example #3
0
 def forwards(self, mongodb_collection, mongodb_database):
     related = list(get_resource_service('vocabularies').get(req=None, lookup={'field_type': 'related_content'}))
     archive_service = get_resource_service('archive')
     for resource in ('archive', 'published'):
         service = get_resource_service(resource)
         for item in mongodb_database[resource].find({'type': 'text', 'associations': {'$gt': {}}}):
             update = False
             associations = {}
             for key, val in item['associations'].items():
                 if val and is_related_content(key, related) and len(val.keys()) > 2:
                     update = True
                     associations[key] = {
                         '_id': val['_id'],
                         'type': val.get('type', 'text'),
                     }
                 elif val and val.get('_id') and len(val.keys()) == 1:
                     type_ = mongodb_database[resource].find_one({'_id': val['_id']}, {'type': 1})
                     if type_:
                         update = True
                         associations[key] = val
                         associations[key]['type'] = type_['type']
                 else:
                     associations[key] = val
             if update:
                 try:
                     _id = ObjectId(item['_id'])
                 except InvalidId:
                     _id = item['_id']
                 # must update twice, otherwise it merges the changes
                 service.system_update(_id, {'associations': None}, item)
                 service.system_update(_id, {'associations': associations}, item)
 def forwards(self, mongodb_collection, mongodb_database):
     related = list(get_resource_service("vocabularies").get(req=None, lookup={"field_type": "related_content"}))
     archive_service = get_resource_service("archive")
     for resource in ("archive", "published"):
         service = get_resource_service(resource)
         for item in mongodb_database[resource].find({"type": "text", "associations": {"$gt": {}}}):
             update = False
             associations = {}
             for key, val in item["associations"].items():
                 if val and is_related_content(key, related) and len(val.keys()) > 2:
                     update = True
                     associations[key] = {
                         "_id": val["_id"],
                         "type": val.get("type", "text"),
                     }
                 elif val and val.get("_id") and len(val.keys()) == 1:
                     type_ = mongodb_database[resource].find_one({"_id": val["_id"]}, {"type": 1})
                     if type_:
                         update = True
                         associations[key] = val
                         associations[key]["type"] = type_["type"]
                 else:
                     associations[key] = val
             if update:
                 try:
                     _id = ObjectId(item["_id"])
                 except InvalidId:
                     _id = item["_id"]
                 # must update twice, otherwise it merges the changes
                 service.system_update(_id, {"associations": None}, item)
                 service.system_update(_id, {"associations": associations}, item)
Example #5
0
 def _fix_related_references(self, updated, updates):
     for key, item in updated[ASSOCIATIONS].items():
         if item and item.get("_fetchable", True) and is_related_content(key):
             updated[ASSOCIATIONS][key] = {
                 "_id": item["_id"],
                 "type": item["type"],
                 "order": item.get("order", 1),
             }
             updates.setdefault("associations", {})[key] = updated[ASSOCIATIONS][key]
Example #6
0
 def _fix_related_references(self, updated, updates):
     for key, item in updated[ASSOCIATIONS].items():
         if item and item.get('_fetchable',
                              True) and is_related_content(key):
             updated[ASSOCIATIONS][key] = {
                 '_id': item['_id'],
                 'type': item['type'],
                 'order': item.get('order', 1),
             }
             updates.setdefault('associations',
                                {})[key] = updated[ASSOCIATIONS][key]
 def forwards(self, mongodb_collection, mongodb_database):
     for resource in ('archive', 'published'):
         service = get_resource_service(resource)
         for item in mongodb_database[resource].find({'associations': {'$exists': 'true', '$nin': [{}, None]}}):
             update = False
             associations = {}
             try:
                 for key, val in item['associations'].items():
                     associations[key] = val
                     if val and is_related_content(key) and val.get('order', None) is None:
                         update = True
                         order = int(key.split('--')[1])
                         associations[key]['order'] = order
             except AttributeError:
                 pass
             if update:
                 try:
                     _id = ObjectId(item['_id'])
                 except InvalidId:
                     _id = item['_id']
                 service.system_update(_id, {'associations': associations}, item)
    def _format_related(self, article, subscriber):
        """Format all associated items for simple items (not packages)."""
        associations = OrderedDict()
        extra_items = {}
        media = {}
        content_profile = None
        archive_service = superdesk.get_resource_service("archive")

        article_associations = OrderedDict(
            sorted(article.get(ASSOCIATIONS, {}).items(),
                   key=lambda itm: (itm[1] or {}).get("order", 1)))

        for key, item in article_associations.items():
            if item:
                if is_related_content(key) and "_type" not in item:
                    orig_item = archive_service.find_one(req=None,
                                                         _id=item["_id"])
                    orig_item["order"] = item.get("order", 1)
                    item = orig_item.copy()

                item = self._transform_to_ninjs(item,
                                                subscriber,
                                                recursive=False)

                # Keep original POI and get rid of all other POI.
                renditions = item.get("renditions")
                if renditions:
                    for rendition in renditions.keys():
                        if rendition != "original" and renditions.get(
                                rendition, {}).get("poi"):
                            renditions[rendition].pop("poi", None)

                associations[
                    key] = item  # all items should stay in associations
                match = MEDIA_FIELD_RE.match(key)
                if match:
                    # item id seems to be build from a custom id
                    # we now check content profile to see if it correspond to a custom field
                    if content_profile is None:
                        try:
                            profile = article["profile"]
                        except KeyError:
                            logger.warning(
                                "missing profile in article (guid: {guid})".
                                format(guid=article.get("guid")))
                            content_profile = {"schema": {}}
                        else:
                            content_profile = superdesk.get_resource_service(
                                "content_types").find_one(_id=profile,
                                                          req=None)
                    field_id = match.group("field_id")
                    schema = content_profile["schema"].get(field_id, {})
                    if schema.get("type") == "media" or schema.get(
                            "type") == "related_content":
                        # we want custom media fields in "extra_items", cf. SDESK-2955
                        version = match.group("version")
                        media.setdefault(field_id, []).append((version, item))
                        extra_items[field_id] = {"type": schema.get("type")}

        if media:
            # we have custom media fields, we now order them
            # and add them to "extra_items"
            for field_id, data in media.items():
                default_order = 1
                items_to_sort = [d[1] for d in sorted(data)]

                if extra_items[field_id]["type"] == "media":
                    # for media items default order is 0 and for related-content default order is 1
                    default_order = 0

                extra_items[field_id]["items"] = sorted(
                    items_to_sort,
                    key=lambda item: item.get("order", default_order))
        return associations, extra_items