From c84c4751e2933de28dc2718f1469330fcba82dc1 Mon Sep 17 00:00:00 2001 From: Nicolas ROCHE Date: Thu, 23 Jan 2020 16:24:45 +0100 Subject: [PATCH] data: add creation timestamp on Page object (#39091) --- .../0042_page_creation_timestamp.py | 22 +++++++++++++++++++ combo/data/models.py | 7 +++++- tests/test_pages.py | 10 +++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 combo/data/migrations/0042_page_creation_timestamp.py diff --git a/combo/data/migrations/0042_page_creation_timestamp.py b/combo/data/migrations/0042_page_creation_timestamp.py new file mode 100644 index 00000000..7214a1ad --- /dev/null +++ b/combo/data/migrations/0042_page_creation_timestamp.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.18 on 2020-01-31 15:40 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('data', '0041_auto_20200130_1619'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='creation_timestamp', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + ] diff --git a/combo/data/models.py b/combo/data/models.py index 358043ee..0565b4f3 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -16,6 +16,7 @@ import collections import copy +import datetime import feedparser import hashlib import html @@ -40,7 +41,7 @@ from django.dispatch import receiver from django.forms import models as model_forms from django import forms from django import template -from django.utils import six +from django.utils import six, timezone from django.utils.encoding import python_2_unicode_compatible, force_text, smart_bytes from django.utils.html import strip_tags from django.utils.safestring import mark_safe @@ -140,6 +141,7 @@ class Page(models.Model): public = models.BooleanField(_('Public'), default=True) groups = models.ManyToManyField(Group, verbose_name=_('Groups'), blank=True) + creation_timestamp = models.DateTimeField(auto_now_add=True) last_update_timestamp = models.DateTimeField(auto_now=True) picture = models.ImageField(_('Picture'), upload_to='page-pictures/', null=True) @@ -434,6 +436,9 @@ class Page(models.Model): cells = CellBase.get_cells(page_id=self.id) return max([self.last_update_timestamp] + [x.last_update_timestamp for x in cells]) + def is_new(self): + return self.creation_timestamp > timezone.now() - datetime.timedelta(days=7) + def duplicate(self): # clone current page new_page = copy.deepcopy(self) diff --git a/tests/test_pages.py b/tests/test_pages.py index 2eb108fc..087bfd8a 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -409,3 +409,13 @@ def test_render_cell_having_href_template_error(app): cell.save() response = app.get(page.get_online_url()) assert "{{e-service_url}}backoffice/..." in response.text # href not rendered + +def test_page_is_new(freezer): + freezer.move_to('2020-01-01') + page = Page(title='page-1', slug='page-1') + page.save() + assert page.is_new() + + freezer.move_to('2020-01-08') + page = Page.objects.get(slug='page-1') + assert not page.is_new()