Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Replacing auto_now and auto_now_add with default
Browse files Browse the repository at this point in the history
This makes it possible to override the field value in tests.

Refs:
  - FactoryBoy/factory_boy#102
  • Loading branch information
sergeyklay committed Apr 18, 2021
1 parent 16864e2 commit e7026f9
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
59 changes: 59 additions & 0 deletions apps/blog/migrations/0010_auto_20210418_1114.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright (C) 2021 Serghei Iakovlev <egrep@protonmail.ch>
#
# 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 <https://www.gnu.org/licenses/>.

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',
),
),
]
43 changes: 43 additions & 0 deletions apps/pages/migrations/0005_auto_20210418_1114.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright (C) 2021 Serghei Iakovlev <egrep@protonmail.ch>
#
# 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 <https://www.gnu.org/licenses/>.

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',
),
),
]
15 changes: 13 additions & 2 deletions branch/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)

Expand All @@ -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."""
Expand Down

0 comments on commit e7026f9

Please sign in to comment.