views.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from allauth.account.models import EmailAddress
  2. from django.conf import settings
  3. from django.http import Http404, HttpRequest, HttpResponse
  4. from django.views.decorators.csrf import csrf_exempt
  5. from model_bakery import baker
  6. from apps.organizations_ext.models import Organization
  7. from apps.projects.models import Project
  8. from apps.teams.models import Team
  9. from apps.uptime.models import Monitor
  10. from apps.users.models import User
  11. @csrf_exempt
  12. def seed_data(request: HttpRequest):
  13. """
  14. Delete existing data and seed data used in end to end testing
  15. Very destructive. Never enable on production.
  16. """
  17. if settings.ENABLE_TEST_API is not True:
  18. raise Http404("Enable Test API is not enabled")
  19. user_email = "cypresstest@example.com"
  20. other_user_email = "cypresstest-other@example.com"
  21. user_password = "hunter22" # nosec
  22. organization_name = "Business Company, Inc."
  23. team_slug = "cypresstestteam"
  24. project_name = "NicheScrip"
  25. User.objects.filter(email=user_email).delete()
  26. user = User.objects.create_user(email=user_email, password=user_password)
  27. User.objects.filter(email=other_user_email).delete()
  28. other_user = User.objects.create_user(
  29. email=other_user_email, password=user_password
  30. )
  31. EmailAddress.objects.create(
  32. user=user, email=user_email, primary=True, verified=False
  33. )
  34. EmailAddress.objects.create(
  35. user=other_user, email=other_user_email, primary=True, verified=True
  36. )
  37. Organization.objects.filter(name=organization_name).delete()
  38. organization = Organization.objects.create(name=organization_name)
  39. orgUser = organization.add_user(user=user)
  40. Team.objects.filter(slug=team_slug).delete()
  41. team = Team.objects.create(slug=team_slug, organization=organization)
  42. Project.objects.filter(name=project_name).delete()
  43. project = Project.objects.create(name=project_name, organization=organization)
  44. Monitor.objects.filter(name="cytestmonitor").delete()
  45. Monitor.objects.create(
  46. name="cytestmonitor",
  47. organization=organization,
  48. project=project,
  49. url="https://www.google.com",
  50. monitor_type="Ping",
  51. interval=60,
  52. )
  53. if request.GET.get("extras", None):
  54. project_name = "SwitchGrip"
  55. project2 = Project.objects.create(name=project_name, organization=organization)
  56. project_name = "PitchFlip"
  57. project3 = Project.objects.create(
  58. name=project_name, organization=organization, platform="JavaScript"
  59. )
  60. team.projects.add(project)
  61. team.projects.add(project2)
  62. team.projects.add(project3)
  63. team.members.add(orgUser)
  64. if request.GET.get("seedIssues", None):
  65. baker.make(
  66. "issue_events.IssueEvent",
  67. issue__project=project3,
  68. _quantity=55,
  69. _bulk_create=True,
  70. )
  71. return HttpResponse()