Skip to content

Commit

Permalink
Updated how factory generates publish dates for article pages (#12246)
Browse files Browse the repository at this point in the history
* Updated how factory generates publish dates for article pages
  • Loading branch information
mmmavis authored Apr 23, 2024
1 parent d68b335 commit a73956d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
13 changes: 13 additions & 0 deletions network-api/networkapi/utility/faker/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from datetime import datetime, timedelta, timezone
from itertools import chain, combinations
from typing import Union

Expand Down Expand Up @@ -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
14 changes: 5 additions & 9 deletions network-api/networkapi/wagtailpages/factory/publication.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")
Expand All @@ -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

Expand Down

0 comments on commit a73956d

Please sign in to comment.