From 76484ac1a3fdbdfc8b0bb1da7f6f63228596d6c4 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 13 Oct 2021 14:41:59 +0100 Subject: [PATCH] Ensure that comment data saved to revisions under the key 'comments' is restored --- wagtail/core/models/__init__.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/wagtail/core/models/__init__.py b/wagtail/core/models/__init__.py index e6264e9f4a..2d30f14e9a 100644 --- a/wagtail/core/models/__init__.py +++ b/wagtail/core/models/__init__.py @@ -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