Skip to content

Commit

Permalink
fix updating editor fields removes extra contents (#2335)
Browse files Browse the repository at this point in the history
which don't have a editor state in the payload

SDFID-641
  • Loading branch information
petrjasek authored Apr 8, 2022
1 parent f3605e3 commit 2ef016d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/archive/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
2 changes: 1 addition & 1 deletion apps/publish/content/correct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
69 changes: 69 additions & 0 deletions features/archive.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<p>bar</p>"
}
}
"""
5 changes: 4 additions & 1 deletion superdesk/editor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 2ef016d

Please sign in to comment.