From a73956dc91c94b824f7a553bd4dba97aa0723ff1 Mon Sep 17 00:00:00 2001 From: Mavis Ou Date: Tue, 23 Apr 2024 10:42:30 -0700 Subject: [PATCH] Updated how factory generates publish dates for article pages (#12246) * Updated how factory generates publish dates for article pages --- network-api/networkapi/utility/faker/helpers.py | 13 +++++++++++++ .../networkapi/wagtailpages/factory/publication.py | 14 +++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/network-api/networkapi/utility/faker/helpers.py b/network-api/networkapi/utility/faker/helpers.py index b7a627e32a1..5b99fc4ab0d 100644 --- a/network-api/networkapi/utility/faker/helpers.py +++ b/network-api/networkapi/utility/faker/helpers.py @@ -1,4 +1,5 @@ import random +from datetime import datetime, timedelta, timezone from itertools import chain, combinations from typing import Union @@ -97,3 +98,15 @@ def get_random_objects( primary_keys_slice = primary_keys[:return_max] return queryset.filter(pk__in=primary_keys_slice) + + +def get_random_date(): + base = datetime(2024, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc) + random_date = base - timedelta( + days=random.randint(0, 365 * 3), + hours=random.randint(0, 24), + minutes=random.randint(0, 60), + seconds=random.randint(0, 60), + ) + + return random_date diff --git a/network-api/networkapi/wagtailpages/factory/publication.py b/network-api/networkapi/wagtailpages/factory/publication.py index 6dbc052f5c9..c709ce7df2e 100644 --- a/network-api/networkapi/wagtailpages/factory/publication.py +++ b/network-api/networkapi/wagtailpages/factory/publication.py @@ -1,11 +1,11 @@ -from datetime import timezone +from datetime import timedelta from random import randint, random, shuffle from django.conf import settings from factory import Faker, SubFactory, post_generation from wagtail_factories import ImageFactory, PageFactory -from networkapi.utility.faker.helpers import get_homepage, reseed +from networkapi.utility.faker.helpers import get_homepage, get_random_date, reseed from networkapi.wagtailpages.factory.documents import DocumentFactory from networkapi.wagtailpages.models import ArticlePage, Profile, PublicationPage from networkapi.wagtailpages.pagemodels.publications.article import ArticleAuthors @@ -27,7 +27,7 @@ class PublicationPageFactory(PageFactory): title = Faker("text", max_nb_chars=120) subtitle = Faker("text", max_nb_chars=250) secondary_subtitle = Faker("text", max_nb_chars=250) - publication_date = Faker("date_object") + publication_date = get_random_date() hero_image = SubFactory(ImageFactory) publication_file = SubFactory(DocumentFactory) intro_notes = Faker("sentence") @@ -50,14 +50,10 @@ class Meta: hero_image = SubFactory(ImageFactory) subtitle = Faker("text", max_nb_chars=250) secondary_subtitle = Faker("text", max_nb_chars=250) - publication_date = Faker("date_object") + publication_date = get_random_date() article_file = SubFactory(DocumentFactory) body = Faker("streamfield", fields=article_body_streamfield_fields) - first_published_at = ( - Faker("date_time", tzinfo=timezone.utc) - if RANDOM_SEED and not TESTING - else Faker("past_datetime", start_date="-30d", tzinfo=timezone.utc) - ) + first_published_at = publication_date - timedelta(days=10) search_description = Faker("paragraph", nb_sentences=5, variable_nb_sentences=True) live = True