Repository: wagtail/wagtail

Issue:
Move setting of has_unpublished_changes and live into Page.save_revision
See:
https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailadmin/views/pages.py#L194
https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailadmin/views/pages.py#L312

I think this should be moved here:
https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailcore/models.py#L431

Two reasons:
- It improves the encapsulation of the `Page` model
- It removes a query
- Simplifies code a bit and makes it easier for people to save revisions outside of wagtailadmin



Discussion:
Agreed that this makes sense in weekly meeting. @kaedroho to address.


Resolving patch:
diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py
--- a/wagtail/wagtailadmin/views/pages.py
+++ b/wagtail/wagtailadmin/views/pages.py
@@ -201,10 +201,8 @@ def clean():
             is_publishing = bool(request.POST.get('action-publish')) and parent_page_perms.can_publish_subpage()
             is_submitting = bool(request.POST.get('action-submit'))
 
-            # Set live to False and has_unpublished_changes to True if we are not publishing
             if not is_publishing:
                 page.live = False
-                page.has_unpublished_changes = True
 
             # Save page
             parent_page.add_child(instance=page)
@@ -339,15 +337,6 @@ def clean():
             # Publish
             if is_publishing:
                 revision.publish()
-            else:
-                # Set has_unpublished_changes flag
-                if page.live:
-                    # To avoid overwriting the live version, we only save the page
-                    # to the revisions table
-                    Page.objects.filter(id=page.id).update(has_unpublished_changes=True)
-                else:
-                    page.has_unpublished_changes = True
-                    page.save()
 
             # Notifications
             if is_publishing:
diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py
--- a/wagtail/wagtailcore/models.py
+++ b/wagtail/wagtailcore/models.py
@@ -481,7 +481,7 @@ def route(self, request, path_components):
             else:
                 raise Http404
 
-    def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None):
+    def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None, changed=True):
         # Create revision
         revision = self.revisions.create(
             content_json=self.to_json(),
@@ -490,8 +490,17 @@ def save_revision(self, user=None, submitted_for_moderation=False, approved_go_l
             approved_go_live_at=approved_go_live_at,
         )
 
+        update_fields = []
+
         self.latest_revision_created_at = revision.created_at
-        self.save(update_fields=['latest_revision_created_at'])
+        update_fields.append('latest_revision_created_at')
+
+        if changed:
+            self.has_unpublished_changes = True
+            update_fields.append('has_unpublished_changes')
+
+        if update_fields:
+            self.save(update_fields=update_fields)
 
         # Log
         logger.info("Page edited: \"%s\" id=%d revision_id=%d", self.title, self.id, revision.id)
@@ -823,7 +832,7 @@ def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True,
             for field, value in update_attrs.items():
                 setattr(latest_revision, field, value)
 
-        latest_revision.save_revision(user=user)
+        latest_revision.save_revision(user=user, changed=False)
 
         # Log
         logger.info("Page copied: \"%s\" id=%d from=%d", page_copy.title, page_copy.id, self.id)


Test patch: