From 59e8a9fd36a94642a082c31d97d570ba238a2b42 Mon Sep 17 00:00:00 2001 From: jacobtoppm Date: Wed, 2 Dec 2020 11:56:13 +0000 Subject: [PATCH] Extract comment instances from the form, not the page instance, so unsaved comments are not lost in case the form was invalid and didn't save --- wagtail/admin/edit_handlers.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/wagtail/admin/edit_handlers.py b/wagtail/admin/edit_handlers.py index 882863267e..b24abb5ddf 100644 --- a/wagtail/admin/edit_handlers.py +++ b/wagtail/admin/edit_handlers.py @@ -794,6 +794,10 @@ class PrivacyModalPanel(EditHandler): class CommentPanel(EditHandler): + def __init__(self, *args, **kwargs): + self.comments = [] + super().__init__(*args, **kwargs) + def required_formsets(self): # add the comments formset # we need to pass in the current user for validation on the formset @@ -828,23 +832,30 @@ class CommentPanel(EditHandler): def html_declarations(self): return render_to_string(self.declarations_template) + def on_form_bound(self): + self.comments = [] + comment_forms = self.form.formsets['comments'].forms + for form in comment_forms: + instance = form.instance + instance.replies = [reply_form.instance for reply_form in form.formsets['replies'].forms] + self.comments.append(instance) + + def render(self): user = getattr(self.request, 'user', None) authors = {user.pk: user_display_name(user)} if user else {} - - comments = self.instance.comments.select_related('user').prefetch_related('replies__user') - for comment in comments: + for comment in self.comments: # iterate over comments to retrieve user display names as we're already going to need to serialise the queryset # also to keep in sync with the modelcluster version of comments - if comment.user.pk not in authors: + if comment.user_id not in authors: authors.update({comment.user.pk: user_display_name(comment.user)}) for reply in comment.replies.all(): - if reply.user.pk not in authors: + if reply.user_id not in authors: authors.update({reply.user.pk: user_display_name(reply.user)}) comments_data = { - 'comments': [comment.serializable_data() for comment in comments], + 'comments': [comment.serializable_data() for comment in self.comments], 'user': user.pk, 'authors': authors }