From 568ba42c95c2b10567c2a5362c3cb505a8b5fa90 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 22 Aug 2014 16:20:50 +0100 Subject: [PATCH 1/3] Added logging for most page operations --- wagtail/wagtailcore/models.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index a4d0bed925..a2d23ee664 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -1,3 +1,4 @@ +import logging import warnings import six @@ -37,6 +38,9 @@ from wagtail.wagtailsearch import index from wagtail.wagtailsearch.backends import get_search_backend +logger = logging.getLogger('wagtail.core') + + class SiteManager(models.Manager): def get_by_natural_key(self, hostname, port): return self.get(hostname=hostname, port=port) @@ -316,8 +320,9 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed @transaction.atomic # ensure that changes are only committed when we have updated all descendant URL paths, to preserve consistency def save(self, *args, **kwargs): update_descendant_url_paths = False + is_new = self.id is None - if self.id is None: + if is_new: # we are creating a record. If we're doing things properly, this should happen # through a treebeard method like add_child, in which case the 'path' field # has been set and so we can safely call get_parent @@ -341,6 +346,11 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed if Site.objects.filter(root_page=self).exists(): cache.delete('wagtail_site_root_paths') + # Log + if is_new: + cls = type(self) + logger.info("Page created: \"%s\" id=%d content_type=%s.%s path=%s", self.title, self.id, cls._meta.app_label, cls.__name__, self.url_path) + return result def _update_descendant_url_paths(self, old_url_path, new_url_path): @@ -412,6 +422,7 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed raise Http404 def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None): + # Create revision revision = self.revisions.create( content_json=self.to_json(), user=user, @@ -422,6 +433,12 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed self.latest_revision_created_at = revision.created_at self.save(update_fields=['latest_revision_created_at']) + # Log + logger.info("Page edited: \"%s\" id=%d revision_id=%d", self.title, self.id, revision.id) + + if submitted_for_moderation: + logger.info("Page submitted for moderation: \"%s\" id=%d revision_id=%d", self.title, self.id, revision.id) + return revision def get_latest_revision(self): @@ -448,6 +465,8 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed page_unpublished.send(sender=self.specific_class, instance=self.specific) + logger.info("Page unpublished: \"%s\" id=%d", self.title, self.id) + self.revisions.update(approved_go_live_at=None) def get_context(self, request, *args, **kwargs): @@ -650,6 +669,9 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed new_self.save() new_self._update_descendant_url_paths(old_url_path, new_url_path) + # Log + logger.info("Page moved: \"%s\" id=%d path=%s", self.title, self.id, self.url_path) + def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True): # Make a copy page_copy = Page.objects.get(id=self.id).specific @@ -689,6 +711,9 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed revision.page = page_copy revision.save() + # Log + logger.info("Page copied: \"%s\" id=%d from=%d", page_copy.title, page_copy.id, self.id) + # Copy child pages if recursive: for child_page in self.get_children(): @@ -995,6 +1020,10 @@ class PageRevision(models.Model): if page.live: page_published.send(sender=page.specific_class, instance=page.specific) + logger.info("Page published: \"%s\" id=%d revision_id=%d", page.title, page.id, self.id) + elif page.go_live_at: + logger.info("Page scheduled for publish: \"%s\" id=%d revision_id=%d go_live_at=%s", page.title, page.id, self.id, page.go_live_at.isoformat()) + def __str__(self): return '"' + unicode(self.page) + '" at ' + unicode(self.created_at) From 4027262433862e59603a05137a9465cc761b388b Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 26 Aug 2014 10:57:29 +0100 Subject: [PATCH 2/3] Log page deletions --- wagtail/wagtailcore/models.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index a2d23ee664..1c0467acb0 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -10,7 +10,7 @@ from modelcluster.models import ClusterableModel, get_all_child_relations from django.db import models, connection, transaction from django.db.models import Q -from django.db.models.signals import pre_delete +from django.db.models.signals import pre_delete, post_delete from django.dispatch.dispatcher import receiver from django.http import Http404 from django.core.cache import cache @@ -924,6 +924,11 @@ def unpublish_page_before_delete(sender, instance, **kwargs): instance.unpublish(commit=False) +@receiver(post_delete, sender=Page) +def log_page_deletion(sender, instance, **kwargs): + logger.info("Page deleted: \"%s\" id=%d", instance.title, instance.id) + + class Orderable(models.Model): sort_order = models.IntegerField(null=True, blank=True, editable=False) sort_order_field = 'sort_order' From df7110625ac751b11c7da1711a0c5710e3e5823a Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 27 Aug 2014 14:55:13 +0100 Subject: [PATCH 3/3] Added logging for moderation approval/rejection --- wagtail/wagtailcore/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 1c0467acb0..c2fe814316 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -985,10 +985,12 @@ class PageRevision(models.Model): def approve_moderation(self): if self.submitted_for_moderation: + logger.info("Page moderation approved: \"%s\" id=%d revision_id=%d", self.page.title, self.page.id, self.id) self.publish() def reject_moderation(self): if self.submitted_for_moderation: + logger.info("Page moderation rejected: \"%s\" id=%d revision_id=%d", self.page.title, self.page.id, self.id) self.submitted_for_moderation = False self.save(update_fields=['submitted_for_moderation'])