From e7026f9672fdefd1f923325858efdacb2ed7bb1a Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 18 Apr 2021 14:18:09 +0300 Subject: [PATCH] Replacing auto_now and auto_now_add with default This makes it possible to override the field value in tests. Refs: - FactoryBoy/factory_boy#102 --- .../migrations/0010_auto_20210418_1114.py | 59 +++++++++++++++++++ .../migrations/0005_auto_20210418_1114.py | 43 ++++++++++++++ branch/mixins.py | 15 ++++- 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 apps/blog/migrations/0010_auto_20210418_1114.py create mode 100644 apps/pages/migrations/0005_auto_20210418_1114.py diff --git a/apps/blog/migrations/0010_auto_20210418_1114.py b/apps/blog/migrations/0010_auto_20210418_1114.py new file mode 100644 index 00000000..e1a889c6 --- /dev/null +++ b/apps/blog/migrations/0010_auto_20210418_1114.py @@ -0,0 +1,59 @@ +# Copyright (C) 2021 Serghei Iakovlev +# +# This file is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this file. If not, see . + +import django.utils.timezone +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog', '0009_auto_20210414_1352'), + ] + + operations = [ + migrations.AlterField( + model_name='comment', + name='created_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date created', + ), + ), + migrations.AlterField( + model_name='comment', + name='updated_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date updated', + ), + ), + migrations.AlterField( + model_name='post', + name='created_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date created', + ), + ), + migrations.AlterField( + model_name='post', + name='updated_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date updated', + ), + ), + ] diff --git a/apps/pages/migrations/0005_auto_20210418_1114.py b/apps/pages/migrations/0005_auto_20210418_1114.py new file mode 100644 index 00000000..e98bc609 --- /dev/null +++ b/apps/pages/migrations/0005_auto_20210418_1114.py @@ -0,0 +1,43 @@ +# Copyright (C) 2021 Serghei Iakovlev +# +# This file is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 3 +# of the License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this file. If not, see . + +import django.utils.timezone +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('pages', '0004_alter_page_no_index'), + ] + + operations = [ + migrations.AlterField( + model_name='page', + name='created_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date created', + ), + ), + migrations.AlterField( + model_name='page', + name='updated_at', + field=models.DateTimeField( + default=django.utils.timezone.now, + verbose_name='Date updated', + ), + ), + ] diff --git a/branch/mixins.py b/branch/mixins.py index 59b0c69d..044cb866 100644 --- a/branch/mixins.py +++ b/branch/mixins.py @@ -16,22 +16,28 @@ """Project wide mixins lives here.""" from django.db import models +from django.utils import timezone from django.utils.translation import gettext_lazy as _ class ModelTimestampsMixin(models.Model): """ Timestamps aware model mixin. + Specify created_at and updated_at fields for a model. """ + # Do not use 'auto_now_add=True' here to be + # able override created_at field easily in tests. created_at = models.DateTimeField( - auto_now_add=True, + default=timezone.now, verbose_name=_('Date created'), ) + # Do not use 'auto_now=True' here to be + # able override updated_at field easily in tests. updated_at = models.DateTimeField( - auto_now=True, + default=timezone.now, verbose_name=_('Date updated'), ) @@ -40,6 +46,11 @@ class Meta: abstract = True + def save(self, *args, **kwargs): + """On save, update updated_at timestamp""" + self.updated_at = timezone.now + return super().save(*args, **kwargs) + class PageDetailsMixin: """Specify page details to the template context."""