create_random_data.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import random
  2. from pathlib import Path
  3. from django.conf import settings
  4. from django.core.management.base import BaseCommand
  5. from django.utils import lorem_ipsum, timezone
  6. from django.utils.text import slugify
  7. from wagtail.images.models import Image
  8. from wagtail.rich_text import RichText
  9. from willow.image import Image as WillowImage
  10. from bakerydemo.base.models import FooterText, HomePage, Person, StandardPage
  11. from bakerydemo.blog.models import BlogIndexPage, BlogPage
  12. from bakerydemo.breads.models import (
  13. BreadIngredient,
  14. BreadPage,
  15. BreadsIndexPage,
  16. BreadType,
  17. Country,
  18. )
  19. from bakerydemo.locations.models import LocationPage, LocationsIndexPage
  20. FIXTURE_MEDIA_DIR = Path(settings.PROJECT_DIR) / "base/fixtures/media/original_images"
  21. class Command(BaseCommand):
  22. help = "Creates random data. Useful for performance or load testing."
  23. def add_arguments(self, parser):
  24. parser.add_argument(
  25. "page_count",
  26. type=int,
  27. help="How many pages of each type to create",
  28. )
  29. parser.add_argument(
  30. "snippet_count",
  31. type=int,
  32. help="How many snippets of each type to create",
  33. )
  34. parser.add_argument(
  35. "image_count",
  36. type=int,
  37. help="How many images to create",
  38. )
  39. def fake_stream_field(self):
  40. return [("paragraph_block", RichText("\n".join(lorem_ipsum.paragraphs(5))))]
  41. def get_random_model(self, model):
  42. return model.objects.order_by("?").first()
  43. def make_title(self):
  44. return lorem_ipsum.words(4, common=False)
  45. def create_pages(self, page_count):
  46. self.stdout.write("Creating bread pages...")
  47. breads_index = BreadsIndexPage.objects.live().first()
  48. for _ in range(page_count):
  49. title = self.make_title()
  50. breads_index.add_child(
  51. instance=BreadPage(
  52. title=title,
  53. slug=slugify(title),
  54. introduction=lorem_ipsum.paragraph(),
  55. bread_type=self.get_random_model(BreadType),
  56. body=self.fake_stream_field(),
  57. origin=self.get_random_model(Country),
  58. image=self.get_random_model(Image),
  59. )
  60. )
  61. self.stdout.write("Creating location pages...")
  62. locations_index = LocationsIndexPage.objects.live().first()
  63. for _ in range(page_count):
  64. title = self.make_title()
  65. locations_index.add_child(
  66. instance=LocationPage(
  67. title=title,
  68. slug=slugify(title),
  69. introduction=lorem_ipsum.paragraph(),
  70. image=self.get_random_model(Image),
  71. address=lorem_ipsum.paragraph(),
  72. body=self.fake_stream_field(),
  73. lat_long="64.144367, -21.939182",
  74. )
  75. )
  76. self.stdout.write("Creating blog pages...")
  77. blog_index = BlogIndexPage.objects.live().first()
  78. for _ in range(page_count):
  79. title = self.make_title()
  80. blog_index.add_child(
  81. instance=BlogPage(
  82. title=title,
  83. slug=slugify(title),
  84. introduction=lorem_ipsum.paragraph(),
  85. body=self.fake_stream_field(),
  86. subtitle=lorem_ipsum.words(10, common=False),
  87. date_published=timezone.now(),
  88. )
  89. )
  90. self.stdout.write("Creating standard pages...")
  91. homepage = HomePage.objects.live().first()
  92. title = self.make_title()
  93. # Nest the standard pages under a top level one
  94. top_level_page = homepage.add_child(
  95. instance=StandardPage(
  96. title=title,
  97. slug=slugify(title),
  98. introduction=lorem_ipsum.paragraph(),
  99. image=self.get_random_model(Image),
  100. body=self.fake_stream_field(),
  101. )
  102. )
  103. for _ in range(page_count):
  104. title = self.make_title()
  105. top_level_page.add_child(
  106. instance=StandardPage(
  107. title=title,
  108. slug=slugify(title),
  109. introduction=lorem_ipsum.paragraph(),
  110. image=self.get_random_model(Image),
  111. body=self.fake_stream_field(),
  112. )
  113. )
  114. def create_snippets(self, snippet_count):
  115. self.stdout.write("Creating countries...")
  116. for _ in range(snippet_count):
  117. Country.objects.create(title=self.make_title())
  118. self.stdout.write("Creating bread ingredients...")
  119. for _ in range(snippet_count):
  120. BreadIngredient.objects.create(name=self.make_title())
  121. self.stdout.write("Creating bread types...")
  122. for _ in range(snippet_count):
  123. BreadType.objects.create(title=self.make_title())
  124. self.stdout.write("Creating people...")
  125. for _ in range(snippet_count):
  126. Person.objects.create(
  127. first_name=lorem_ipsum.words(1, common=False),
  128. last_name=lorem_ipsum.words(1, common=False),
  129. job_title=lorem_ipsum.words(1, common=False),
  130. image=self.get_random_model(Image),
  131. )
  132. self.stdout.write("Creating footer text...")
  133. for _ in range(snippet_count):
  134. FooterText.objects.create(body=self.fake_stream_field())
  135. def create_images(self, image_count):
  136. image_files = list(FIXTURE_MEDIA_DIR.iterdir())
  137. self.stdout.write("Creating images...")
  138. for _ in range(image_count):
  139. random_image = random.choice(image_files)
  140. with random_image.open(mode="rb") as image_file:
  141. willow_image = WillowImage.open(image_file)
  142. width, height = willow_image.get_size()
  143. image = Image.objects.create(
  144. title=self.make_title(),
  145. width=width,
  146. height=height,
  147. file_size=random_image.stat().st_size,
  148. )
  149. image_file.seek(0)
  150. image.file.save(random_image.name, image_file)
  151. def handle(self, **options):
  152. self.create_images(options["image_count"])
  153. self.create_snippets(options["snippet_count"])
  154. self.create_pages(options["page_count"])