Ensure that comment data saved to revisions under the key 'comments' is restored

pull/7596/head
Matt Westcott 2021-10-13 14:41:59 +01:00 zatwierdzone przez Matt Westcott
rodzic 66e5e21b59
commit 76484ac1a3
1 zmienionych plików z 15 dodań i 1 usunięć

Wyświetl plik

@ -2259,7 +2259,21 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
* ``wagtail_admin_comments`` (COMMENTS_RELATION_NAME)
"""
obj = self.specific_class.from_json(content_json)
data = json.loads(content_json)
# Old revisions (pre Wagtail 2.15) may have saved comment data under the name 'comments'
# rather than the current relation name as set by COMMENTS_RELATION_NAME;
# if a 'comments' field exists and looks like our comments model, alter the data to use
# COMMENTS_RELATION_NAME before restoring
if (
COMMENTS_RELATION_NAME not in data and 'comments' in data
and isinstance(data['comments'], list) and len(data['comments'])
and isinstance(data['comments'][0], dict) and 'contentpath' in data['comments'][0]
):
data[COMMENTS_RELATION_NAME] = data['comments']
del data['comments']
obj = self.specific_class.from_serializable_data(data)
# These should definitely never change between revisions
obj.id = self.id