lint create_random_data.py

pull/355/head
Matt Westcott 2022-08-25 17:28:06 +01:00
rodzic ce1df734b8
commit 0824f804cc
1 zmienionych plików z 77 dodań i 59 usunięć

Wyświetl plik

@ -1,45 +1,53 @@
from django.core.management.base import BaseCommand
import random
from pathlib import Path
from bakerydemo.breads.models import BreadPage, BreadsIndexPage, BreadType, Country, BreadIngredient
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import lorem_ipsum, timezone
from django.utils.text import slugify
from wagtail.core.rich_text import RichText
from bakerydemo.locations.models import LocationPage, LocationsIndexPage
from bakerydemo.blog.models import BlogPage, BlogIndexPage
import random
from wagtail.images.models import Image
from pathlib import Path
from willow.image import Image as WillowImage
from django.conf import settings
from bakerydemo.base.models import People, FooterText, StandardPage, HomePage
from bakerydemo.base.models import FooterText, HomePage, People, StandardPage
from bakerydemo.blog.models import BlogIndexPage, BlogPage
from bakerydemo.breads.models import (
BreadIngredient,
BreadPage,
BreadsIndexPage,
BreadType,
Country,
)
from bakerydemo.locations.models import LocationPage, LocationsIndexPage
FIXTURE_MEDIA_DIR = Path(settings.PROJECT_DIR) / "base/fixtures/media/original_images"
class Command(BaseCommand):
help = "Creates random data. Useful for performance or load testing."
def add_arguments(self, parser):
parser.add_argument(
'page_count', type=int,
help='How many pages of each type to create',
"page_count",
type=int,
help="How many pages of each type to create",
)
parser.add_argument(
'snippet_count', type=int,
help='How many snippets of each type to create',
"snippet_count",
type=int,
help="How many snippets of each type to create",
)
parser.add_argument(
'image_count', type=int,
help='How many images to create',
"image_count",
type=int,
help="How many images to create",
)
def fake_stream_field(self):
return [
('paragraph_block', RichText("\n".join(lorem_ipsum.paragraphs(5))))
]
return [("paragraph_block", RichText("\n".join(lorem_ipsum.paragraphs(5))))]
def get_random_model(self, model):
return model.objects.order_by('?').first()
return model.objects.order_by("?").first()
def make_title(self):
return lorem_ipsum.words(4, common=False)
@ -49,63 +57,73 @@ class Command(BaseCommand):
breads_index = BreadsIndexPage.objects.live().first()
for _ in range(page_count):
title = self.make_title()
breads_index.add_child(instance=BreadPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
bread_type=self.get_random_model(BreadType),
body=self.fake_stream_field(),
origin=self.get_random_model(Country),
image=self.get_random_model(Image)
))
breads_index.add_child(
instance=BreadPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
bread_type=self.get_random_model(BreadType),
body=self.fake_stream_field(),
origin=self.get_random_model(Country),
image=self.get_random_model(Image),
)
)
self.stdout.write("Creating location pages...")
locations_index = LocationsIndexPage.objects.live().first()
for _ in range(page_count):
title = self.make_title()
locations_index.add_child(instance=LocationPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
image=self.get_random_model(Image),
address=lorem_ipsum.paragraph(),
body=self.fake_stream_field(),
lat_long='64.144367, -21.939182'
))
locations_index.add_child(
instance=LocationPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
image=self.get_random_model(Image),
address=lorem_ipsum.paragraph(),
body=self.fake_stream_field(),
lat_long="64.144367, -21.939182",
)
)
self.stdout.write("Creating blog pages...")
blog_index = BlogIndexPage.objects.live().first()
for _ in range(page_count):
title = self.make_title()
blog_index.add_child(instance=BlogPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
body=self.fake_stream_field(),
subtitle=lorem_ipsum.words(10, common=False),
date_published=timezone.now()
))
blog_index.add_child(
instance=BlogPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
body=self.fake_stream_field(),
subtitle=lorem_ipsum.words(10, common=False),
date_published=timezone.now(),
)
)
self.stdout.write("Creating standard pages...")
homepage = HomePage.objects.live().first()
title = self.make_title()
# Nest the standard pages under a top level one
top_level_page = homepage.add_child(instance=StandardPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
image=self.get_random_model(Image),
body=self.fake_stream_field(),
))
for _ in range(page_count):
title = self.make_title()
top_level_page.add_child(instance=StandardPage(
top_level_page = homepage.add_child(
instance=StandardPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
image=self.get_random_model(Image),
body=self.fake_stream_field(),
))
)
)
for _ in range(page_count):
title = self.make_title()
top_level_page.add_child(
instance=StandardPage(
title=title,
slug=slugify(title),
introduction=lorem_ipsum.paragraph(),
image=self.get_random_model(Image),
body=self.fake_stream_field(),
)
)
def create_snippets(self, snippet_count):
self.stdout.write("Creating countries...")
@ -126,7 +144,7 @@ class Command(BaseCommand):
first_name=lorem_ipsum.words(1, common=False),
last_name=lorem_ipsum.words(1, common=False),
job_title=lorem_ipsum.words(1, common=False),
image=self.get_random_model(Image)
image=self.get_random_model(Image),
)
self.stdout.write("Creating footer text...")
@ -152,6 +170,6 @@ class Command(BaseCommand):
image.file.save(random_image.name, image_file)
def handle(self, **options):
self.create_images(options['image_count'])
self.create_snippets(options['snippet_count'])
self.create_pages(options['page_count'])
self.create_images(options["image_count"])
self.create_snippets(options["snippet_count"])
self.create_pages(options["page_count"])