base_commands.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.core.management.base import BaseCommand
  2. from model_bakery import baker
  3. from model_bakery.random_gen import gen_json, gen_slug
  4. from organizations_ext.models import Organization
  5. from projects.models import Project
  6. baker.generators.add("organizations.fields.SlugField", gen_slug)
  7. baker.generators.add("django.db.models.JSONField", gen_json)
  8. class MakeSampleCommand(BaseCommand):
  9. organization = None
  10. project = None
  11. batch_size = 10000
  12. def add_org_project_arguments(self, parser):
  13. parser.add_argument("--org", type=str, help="Organization slug")
  14. parser.add_argument("--project", type=str, help="Project slug")
  15. def add_arguments(self, parser):
  16. parser.add_argument("--quantity", type=int, default=1000)
  17. self.add_org_project_arguments(parser)
  18. def handle(self, *args, **options):
  19. self.organization = self.get_organization(options.get("org"))
  20. self.project = self.get_project(options.get("project"))
  21. def get_organization(self, org: str):
  22. if org:
  23. return Organization.objects.get(slug=org)
  24. organization = Organization.objects.first()
  25. if not organization:
  26. organization = baker.make("organizations_ext.Organization")
  27. return organization
  28. def get_project(self, project: str):
  29. if project:
  30. return Project.objects.get(slug=project, organization=self.organization)
  31. project = Project.objects.filter(organization=self.organization).first()
  32. if not project:
  33. project = baker.make("projects.Project", organization=self.organization)
  34. return project
  35. def progress_tick(self):
  36. self.stdout.write(self.style.NOTICE("."), ending="")
  37. def success_message(self, message: str):
  38. self.stdout.write(self.style.SUCCESS(message))