No Description

seed_demo.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import random
  2. from django.core.management.base import BaseCommand
  3. from django.contrib.auth.models import User
  4. from django.db import transaction
  5. from django.utils.text import slugify
  6. from api.models import Profile, Opportunity, IntroductionRequest
  7. class Command(BaseCommand):
  8. help = "Seed demo data: users, profiles, opportunities, and intro requests."
  9. def add_arguments(self, parser):
  10. parser.add_argument(
  11. "--reset",
  12. action="store_true",
  13. help="Delete existing demo data (profiles, opportunities, intro requests) before seeding",
  14. )
  15. parser.add_argument(
  16. "--users",
  17. type=int,
  18. default=6,
  19. help="Approx number of non-admin demo users to ensure exist (default: 6)",
  20. )
  21. @transaction.atomic
  22. def handle(self, *args, **options):
  23. reset = options["--reset"] if "--reset" in options else options.get("reset")
  24. target_users = max(1, int(options.get("users", 6)))
  25. if reset:
  26. IntroductionRequest.objects.all().delete()
  27. Opportunity.objects.all().delete()
  28. Profile.objects.all().delete()
  29. self.stdout.write(self.style.WARNING("Cleared Profiles, Opportunities, and IntroductionRequests."))
  30. # Ensure an admin user exists
  31. admin_user, created = User.objects.get_or_create(username="admin")
  32. if created:
  33. admin_user.set_password("admin123")
  34. admin_user.is_staff = True
  35. admin_user.is_superuser = True
  36. admin_user.email = "admin@example.com"
  37. admin_user.save()
  38. self.stdout.write(self.style.SUCCESS("Created superuser 'admin' (password: admin123)"))
  39. else:
  40. if not admin_user.is_staff or not admin_user.is_superuser:
  41. admin_user.is_staff = True
  42. admin_user.is_superuser = True
  43. admin_user.save(update_fields=["is_staff", "is_superuser"])
  44. # Demo usernames to create/ensure
  45. default_names = [
  46. "alice",
  47. "bob",
  48. "charlie",
  49. "diana",
  50. "eve",
  51. "frank",
  52. "grace",
  53. "heidi",
  54. "ivan",
  55. "judy",
  56. ]
  57. usernames = default_names[: target_users]
  58. created_users = 0
  59. ensured_users = []
  60. for i, uname in enumerate(usernames):
  61. user, u_created = User.objects.get_or_create(username=uname)
  62. if u_created:
  63. user.set_password("password123")
  64. # Make first user staff to access admin_frontend
  65. if i == 0:
  66. user.is_staff = True
  67. user.email = f"{uname}@example.com"
  68. user.save()
  69. created_users += 1
  70. ensured_users.append(user)
  71. # Ensure Profiles for each non-admin user
  72. created_profiles = 0
  73. bios = [
  74. "Passionate about building products and communities.",
  75. "Full-stack developer and coffee enthusiast.",
  76. "Marketer focused on growth and analytics.",
  77. "Product designer who loves simplicity.",
  78. "Data scientist exploring ML and AI.",
  79. "Entrepreneur connecting people and ideas.",
  80. ]
  81. industries_list = [
  82. "Technology",
  83. "Finance",
  84. "Healthcare",
  85. "Education",
  86. "E-commerce",
  87. "Media",
  88. ]
  89. for idx, user in enumerate(ensured_users):
  90. profile, p_created = Profile.objects.get_or_create(
  91. user=user,
  92. defaults={
  93. "bio": bios[idx % len(bios)],
  94. "interests": ", ".join(random.sample(["AI", "Web", "Cloud", "UX", "Data", "DevOps"], 3)),
  95. "industry": industries_list[idx % len(industries_list)],
  96. "is_verified": idx % 2 == 0,
  97. },
  98. )
  99. # Also set taggit tags to mirror interests (best-effort)
  100. try:
  101. if profile and profile.interests:
  102. tokens = [t.strip() for t in profile.interests.split(",") if t.strip()]
  103. if tokens:
  104. profile.tags.set(tokens, clear=True)
  105. except Exception:
  106. pass
  107. if p_created:
  108. created_profiles += 1
  109. # Ensure a few Opportunities
  110. opp_titles = [
  111. "Build Analytics Dashboard",
  112. "Launch Marketing Campaign",
  113. "Design Mobile App UI",
  114. "Migrate to Cloud Infrastructure",
  115. "Implement Recommendation Engine",
  116. ]
  117. created_opps = 0
  118. for title in opp_titles:
  119. opp, o_created = Opportunity.objects.get_or_create(
  120. title=title,
  121. defaults={
  122. "description": f"Opportunity to {title.lower()}. Help wanted from motivated contributors.",
  123. },
  124. )
  125. if o_created:
  126. created_opps += 1
  127. # Ensure a few IntroductionRequests between different users
  128. created_intros = 0
  129. if len(ensured_users) >= 2:
  130. pairs = []
  131. for i in range(0, min(4, len(ensured_users) - 1)):
  132. a = ensured_users[i]
  133. b = ensured_users[-(i + 1)]
  134. if a != b:
  135. pairs.append((a, b))
  136. for (a, b) in pairs:
  137. msg = f"Hi {b.username}, would love to connect about {random.choice(['AI', 'design', 'growth', 'data'])}."
  138. exists = IntroductionRequest.objects.filter(from_user=a, to_user=b, message=msg).exists()
  139. if not exists:
  140. IntroductionRequest.objects.create(
  141. from_user=a, to_user=b, message=msg, is_accepted=random.choice([False, True])
  142. )
  143. created_intros += 1
  144. self.stdout.write(
  145. self.style.SUCCESS(
  146. f"Seed complete: users(+{created_users}), profiles(+{created_profiles}), opportunities(+{created_opps}), intro_requests(+{created_intros})"
  147. )
  148. )