base_commands.py 1.6 KB

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