views.py 2.6 KB

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