diff --git a/wagtail/wagtailcore/migrations/0009_remove_auto_now_add_from_pagerevision_created_at.py b/wagtail/wagtailcore/migrations/0009_remove_auto_now_add_from_pagerevision_created_at.py new file mode 100644 index 000000000..3dbd7e409 --- /dev/null +++ b/wagtail/wagtailcore/migrations/0009_remove_auto_now_add_from_pagerevision_created_at.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailcore', '0008_populate_latest_revision_created_at'), + ] + + operations = [ + migrations.AlterField( + model_name='pagerevision', + name='created_at', + field=models.DateTimeField(), + ), + ] diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 5db4e42af..7b201c352 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -946,7 +946,7 @@ class SubmittedRevisionsManager(models.Manager): class PageRevision(models.Model): page = models.ForeignKey('Page', related_name='revisions') submitted_for_moderation = models.BooleanField(default=False) - created_at = models.DateTimeField(auto_now_add=True) + created_at = models.DateTimeField() user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True) content_json = models.TextField() approved_go_live_at = models.DateTimeField(null=True, blank=True) @@ -955,6 +955,12 @@ class PageRevision(models.Model): submitted_revisions = SubmittedRevisionsManager() def save(self, *args, **kwargs): + # Set default value for created_at to now + # We cannot use auto_now_add as that will override + # any value that is set before saving + if self.created_at is None: + self.created_at = timezone.now() + super(PageRevision, self).save(*args, **kwargs) if self.submitted_for_moderation: # ensure that all other revisions of this page have the 'submitted for moderation' flag unset diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 804b2c4e1..b54b22b4a 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -443,6 +443,23 @@ class TestCopyPage(TestCase): # Check that the new revision is not submitted for moderation self.assertFalse(new_christmas_event.revisions.first().submitted_for_moderation) + def test_copy_page_copies_revisions_and_doesnt_change_created_at(self): + christmas_event = EventPage.objects.get(url_path='/home/events/christmas/') + christmas_event.save_revision(submitted_for_moderation=True) + + # Set the created_at of the revision to a time in the past + revision = christmas_event.get_latest_revision() + revision.created_at = datetime.datetime(2014, 1, 1) + revision.save() + + # Copy it + new_christmas_event = christmas_event.copy(update_attrs={'title': "New christmas event", 'slug': 'new-christmas-event'}) + + # Check that the created_at time is the same + christmas_event_created_at = christmas_event.get_latest_revision().created_at + new_christmas_event_created_at = new_christmas_event.get_latest_revision().created_at + self.assertEqual(christmas_event_created_at, new_christmas_event_created_at) + def test_copy_page_copies_revisions_and_doesnt_schedule(self): christmas_event = EventPage.objects.get(url_path='/home/events/christmas/') christmas_event.save_revision(approved_go_live_at=datetime.datetime(2014, 9, 16, 9, 12, 00, tzinfo=pytz.utc))