def create(self, docs, **kwargs):
        doc = docs[0] if len(docs) > 0 else {}
        template_name = doc.get("template_name")
        item = doc.get("item") or {}
        item["desk_name"] = get_resource_service("desks").get_desk_name(
            item.get("task", {}).get("desk"))

        if not template_name:
            SuperdeskApiError.badRequestError(message="Invalid Template Name")

        if not item:
            SuperdeskApiError.badRequestError(message="Invalid Item")

        template = superdesk.get_resource_service(
            "content_templates").get_template_by_name(template_name)
        if not template:
            SuperdeskApiError.badRequestError(message="Invalid Template")

        updates = render_content_template(item, template)
        item.update(updates)

        editor_utils.generate_fields(item, reload=True)

        if template_name == "kill":
            apply_null_override_for_kill(item)

        docs[0] = item
        build_custom_hateoas(CUSTOM_HATEOAS, docs[0])
        return [docs[0].get(config.ID_FIELD)]
示例#2
0
def on_create_item(docs, repo_type=ARCHIVE):
    """Make sure item has basic fields populated."""

    for doc in docs:
        editor_utils.generate_fields(doc)
        update_dates_for(doc)
        set_original_creator(doc)

        if not doc.get(GUID_FIELD):
            doc[GUID_FIELD] = generate_guid(type=GUID_NEWSML)

        if "unique_id" not in doc:
            generate_unique_id_and_name(doc, repo_type)

        if "family_id" not in doc:
            doc["family_id"] = doc[GUID_FIELD]

        if "event_id" not in doc and repo_type != "ingest":
            doc["event_id"] = generate_guid(type=GUID_TAG)

        set_default_state(doc, CONTENT_STATE.DRAFT)
        doc.setdefault(config.ID_FIELD, doc[GUID_FIELD])

        if repo_type == ARCHIVE:
            # set the source for the article
            set_default_source(doc)

        if "profile" not in doc and app.config.get("DEFAULT_CONTENT_TYPE",
                                                   None):
            doc["profile"] = app.config.get("DEFAULT_CONTENT_TYPE", None)

        copy_metadata_from_profile(doc)
        copy_metadata_from_user_preferences(doc, repo_type)

        if "language" not in doc:
            doc["language"] = app.config.get("DEFAULT_LANGUAGE", "en")

            if doc.get("task", None) and doc["task"].get("desk", None):
                desk = superdesk.get_resource_service("desks").find_one(
                    req=None, _id=doc["task"]["desk"])
                if desk and desk.get("desk_language", None):
                    doc["language"] = desk["desk_language"]

        if not doc.get(ITEM_OPERATION):
            doc[ITEM_OPERATION] = ITEM_CREATE

        if doc.get("template"):
            from apps.templates.content_templates import render_content_template_by_id  # avoid circular import

            doc.pop("fields_meta", None)
            render_content_template_by_id(doc, doc["template"], update=True)
            editor_utils.generate_fields(doc)
示例#3
0
    def create(self, docs, **kwargs):
        doc = docs[0] if len(docs) > 0 else {}
        original_id = request.view_args["original_id"]
        update_document = doc.get("update")

        archive_service = get_resource_service(ARCHIVE)
        original = archive_service.find_one(req=None, _id=original_id)
        self._validate_rewrite(original, update_document)

        rewrite = self._create_rewrite_article(original,
                                               existing_item=update_document,
                                               desk_id=doc.get("desk_id"))

        # sync editor state
        copy_fields(original, rewrite, ignore_empty=True)

        if update_document:
            # copy editor state from existing item to preserve those
            copy_fields(update_document, rewrite, ignore_empty=True)

        if rewrite.get("fields_meta"):
            generate_fields(rewrite, force=True)

        update_associations(rewrite)

        # signal
        item_rewrite.send(self, item=rewrite, original=original)

        if update_document:
            # process the existing story
            archive_service.patch(update_document[config.ID_FIELD], rewrite)
            app.on_archive_item_updated(rewrite, update_document, ITEM_LINK)
            rewrite[config.ID_FIELD] = update_document[config.ID_FIELD]
            ids = [update_document[config.ID_FIELD]]
        else:
            # Set the version.
            resolve_document_version(rewrite, ARCHIVE, "POST")
            ids = archive_service.post([rewrite])
            insert_into_versions(doc=rewrite)
            build_custom_hateoas(CUSTOM_HATEOAS, rewrite)

            app.on_archive_item_updated(
                {"rewrite_of": rewrite.get("rewrite_of")}, rewrite, ITEM_LINK)

        self._add_rewritten_flag(original, rewrite)
        get_resource_service("archive_broadcast").on_broadcast_master_updated(
            ITEM_CREATE, item=original, rewrite_id=ids[0])

        doc.clear()
        doc.update(rewrite)
        return ids
示例#4
0
    def get_updates(self, item, prefix, hrefs):
        updates = {}

        # check renditions
        renditions = item.get("renditions") or {}
        for rendition in renditions.values():
            if rendition and rendition.get(
                    "href") and rendition["href"].startswith(prefix):
                media = rendition.get("media")
                if not media:
                    print("Can't fix rendition", rendition)
                    continue
                _href = rendition["href"]
                if _href not in hrefs:
                    hrefs[_href] = app.media.url_for_media(
                        media, rendition.get("mimetype"))
                rendition["href"] = hrefs[_href]
                updates["renditions"] = renditions

        # check associations - recursive
        associations = item.get("associations") or {}
        for assoc in associations.values():
            if assoc:
                assoc_updates = self.get_updates(assoc, prefix, hrefs)
                if assoc_updates:
                    updates["associations"] = associations

        # check editor fields - recursive
        fields_meta = item.get("fields_meta") or {}
        for field in fields_meta.keys():
            content_state = get_field_content_state(item, field) or {}
            entity_map = content_state.get("entityMap") or {}
            for entity in entity_map.values():
                if entity.get("type") == "MEDIA" and entity.get(
                        "data") and entity["data"].get("media"):
                    entity_updates = self.get_updates(entity["data"]["media"],
                                                      prefix, hrefs)
                    if entity_updates:
                        generate_fields(item, [field], force=True)
                        updates["fields_meta"] = fields_meta
                        if item.get(field):
                            updates[field] = item[field]

        body_html = item.get("body_html")
        if item.get("body_html") and hrefs:
            for old_url, new_url in hrefs.items():
                item["body_html"] = item["body_html"].replace(old_url, new_url)
            if body_html != item["body_html"]:
                updates["body_html"] = item["body_html"]

        return updates
示例#5
0
def on_create_item(docs, repo_type=ARCHIVE):
    """Make sure item has basic fields populated."""

    for doc in docs:
        editor_utils.generate_fields(doc)
        update_dates_for(doc)
        set_original_creator(doc)

        if not doc.get(GUID_FIELD):
            doc[GUID_FIELD] = generate_guid(type=GUID_NEWSML)

        if 'unique_id' not in doc:
            generate_unique_id_and_name(doc, repo_type)

        if 'family_id' not in doc:
            doc['family_id'] = doc[GUID_FIELD]

        if 'event_id' not in doc and repo_type != 'ingest':
            doc['event_id'] = generate_guid(type=GUID_TAG)

        set_default_state(doc, CONTENT_STATE.DRAFT)
        doc.setdefault(config.ID_FIELD, doc[GUID_FIELD])

        if repo_type == ARCHIVE:
            # set the source for the article
            set_default_source(doc)

        if 'profile' not in doc and app.config.get('DEFAULT_CONTENT_TYPE',
                                                   None):
            doc['profile'] = app.config.get('DEFAULT_CONTENT_TYPE', None)

        copy_metadata_from_profile(doc)
        copy_metadata_from_user_preferences(doc, repo_type)

        if 'language' not in doc:
            doc['language'] = app.config.get('DEFAULT_LANGUAGE', 'en')

            if doc.get('task', None) and doc['task'].get('desk', None):
                desk = superdesk.get_resource_service('desks').find_one(
                    req=None, _id=doc['task']['desk'])
                if desk and desk.get('desk_language', None):
                    doc['language'] = desk['desk_language']

        if not doc.get(ITEM_OPERATION):
            doc[ITEM_OPERATION] = ITEM_CREATE

        if doc.get('template'):
            from apps.templates.content_templates import render_content_template_by_id  # avoid circular import
            doc.pop('fields_meta', None)
            render_content_template_by_id(doc, doc['template'], update=True)
            editor_utils.generate_fields(doc)
示例#6
0
 def update(self, id, updates, original):
     editor_utils.generate_fields(updates)
     get_resource_service("archive")._handle_media_updates(
         updates, original, get_user())
     super().update(id, updates, original)