From 2ef016d19ecd59d28bb6b5951c561c7f909f999d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Fri, 8 Apr 2022 12:24:22 +0200 Subject: [PATCH] fix updating editor fields removes extra contents (#2335) which don't have a editor state in the payload SDFID-641 --- apps/archive/archive.py | 2 +- apps/publish/content/correct.py | 2 +- features/archive.feature | 69 +++++++++++++++++++++++++++++++++ superdesk/editor_utils.py | 5 ++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/apps/archive/archive.py b/apps/archive/archive.py index 6a083c3873..a3f39b4d20 100644 --- a/apps/archive/archive.py +++ b/apps/archive/archive.py @@ -426,7 +426,7 @@ def on_update(self, updates, original): """ user = get_user() - editor_utils.generate_fields(updates) + editor_utils.generate_fields(updates, original=original) if ITEM_TYPE in updates: del updates[ITEM_TYPE] diff --git a/apps/publish/content/correct.py b/apps/publish/content/correct.py index 757f64aaf1..7c482a1994 100644 --- a/apps/publish/content/correct.py +++ b/apps/publish/content/correct.py @@ -104,7 +104,7 @@ def on_update(self, updates, original): self.change_being_corrected_to_published(updates, original) def update(self, id, updates, original): - editor_utils.generate_fields(updates) + editor_utils.generate_fields(updates, original=original) get_resource_service("archive")._handle_media_updates(updates, original, get_user()) super().update(id, updates, original) diff --git a/features/archive.feature b/features/archive.feature index 379f549364..76a2931d2b 100644 --- a/features/archive.feature +++ b/features/archive.feature @@ -1216,3 +1216,72 @@ Feature: News Items Archive } } """ + + @auth + Scenario: Fix updating editor state removing other keys from extra dict (SDFID-641) + + Given "archive" + """ + [{"_id": "test_editor_gen_2", "guid": "test_editor_gen_2", "headline": "test", "extra": {"foo": "foo"}}] + """ + + When we patch given + """ + { + "fields_meta" : { + "headline" : { + "draftjsState" : [ + { + "blocks" : [ + { + "key" : "dphij", + "text" : "editor 3 headline test", + "type" : "unstyled", + "depth" : 0, + "inlineStyleRanges" : [ ], + "entityRanges" : [ ], + "data" : { + "MULTIPLE_HIGHLIGHTS" : {} + } + } + ], + "entityMap" : {} + } + ] + }, + "extra>bar" : { + "draftjsState" : [ + { + "blocks" : [ + { + "key" : "dphij", + "text" : "bar", + "type" : "unstyled", + "depth" : 0, + "inlineStyleRanges" : [ ], + "entityRanges" : [ ], + "data" : { + "MULTIPLE_HIGHLIGHTS" : {} + } + } + ], + "entityMap" : {} + } + ] + } + } + } + """ + When we get "/archive/test_editor_gen_2" + Then we get existing resource + """ + { + "_id": "test_editor_gen_2", + "guid": "test_editor_gen_2", + "headline": "editor 3 headline test", + "extra": { + "foo": "foo", + "bar": "

bar

" + } + } + """ diff --git a/superdesk/editor_utils.py b/superdesk/editor_utils.py index 5d265d0c70..3da2b30d7a 100644 --- a/superdesk/editor_utils.py +++ b/superdesk/editor_utils.py @@ -777,7 +777,7 @@ def filter_blocks(item, field, filter, is_html=True): editor.update_item() -def generate_fields(item, fields=None, force=False, reload=False): +def generate_fields(item, fields=None, force=False, reload=False, original=None): """Generate item fields from editor states :param item: item containing Draft.js ContentState @@ -788,6 +788,9 @@ def generate_fields(item, fields=None, force=False, reload=False): if fields is None: fields = get_content_state_fields(item) + if original is not None and original.get("extra"): + item.setdefault("extra", original["extra"]) + for field in fields: client_value = get_field_value(item, field) editor = Editor3Content(item, field, is_html=is_html(field), reload=reload)