make_sample_issues.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import random
  2. from django.core.management.base import BaseCommand
  3. from model_bakery import baker
  4. from model_bakery.random_gen import gen_json, gen_slug
  5. from events.test_data import event_generator
  6. from events.views import EventStoreAPIView
  7. from projects.models import Project
  8. baker.generators.add("organizations.fields.SlugField", gen_slug)
  9. baker.generators.add("django.db.models.JSONField", gen_json)
  10. class Command(BaseCommand):
  11. help = "Create sample issues and events for dev and demonstration purposes"
  12. def add_arguments(self, parser):
  13. parser.add_argument("quantity", nargs="?", type=int)
  14. parser.add_argument(
  15. "--only-real", action="store_true", help="Only include real sample events",
  16. )
  17. parser.add_argument(
  18. "--only-fake",
  19. action="store_true",
  20. help="Only include faked generated events",
  21. )
  22. def generate_real_event(self, project, unique_issue=False):
  23. """ Generate an event based on real sample data """
  24. data = event_generator.generate_random_event(unique_issue)
  25. serializer = EventStoreAPIView().get_event_serializer_class(data)(
  26. data=data, context={"project": project}
  27. )
  28. serializer.is_valid()
  29. serializer.save()
  30. def handle(self, *args, **options):
  31. project = Project.objects.first()
  32. if not project:
  33. project = baker.make("projects.Project")
  34. if options["quantity"] is None:
  35. options["quantity"] = 1
  36. quantity = options["quantity"]
  37. only_real = options["only_real"]
  38. only_fake = options["only_fake"]
  39. if only_real:
  40. for _ in range(quantity):
  41. self.generate_real_event(project)
  42. elif only_fake:
  43. baker.make("events.Event", issue__project=project, _quantity=quantity)
  44. else:
  45. for _ in range(quantity):
  46. if random.choice([0, 1]): # nosec
  47. baker.make("events.Event", issue__project=project)
  48. else:
  49. self.generate_real_event(project)
  50. self.stdout.write(
  51. self.style.SUCCESS('Successfully created "%s" events' % quantity)
  52. )