|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | +from django.utils import timezone |
| 3 | +from datetime import timedelta |
| 4 | +import random |
| 5 | +from pymongo import MongoClient |
| 6 | +from octofit_tracker.models import User, Team, Activity, Leaderboard, Workout |
| 7 | + |
| 8 | + |
| 9 | +class Command(BaseCommand): |
| 10 | + help = 'Populate the octofit_db database with test data' |
| 11 | + |
| 12 | + def handle(self, *args, **options): |
| 13 | + # Clear existing data directly via pymongo to avoid ORM issues with |
| 14 | + # malformed documents that may have non-integer primary keys. |
| 15 | + self.stdout.write('Clearing existing data...') |
| 16 | + _client = MongoClient('localhost', 27017) |
| 17 | + _db = _client['octofit_db'] |
| 18 | + for col in ['users', 'teams', 'activities', 'leaderboard', 'workouts']: |
| 19 | + _db[col].delete_many({}) |
| 20 | + _client.close() |
| 21 | + |
| 22 | + # Create Teams using Django ORM |
| 23 | + self.stdout.write('Inserting teams...') |
| 24 | + Team.objects.create( |
| 25 | + id=1, |
| 26 | + name='Team Marvel', |
| 27 | + description="Earth's Mightiest Heroes", |
| 28 | + ) |
| 29 | + Team.objects.create( |
| 30 | + id=2, |
| 31 | + name='Team DC', |
| 32 | + description='Justice League United', |
| 33 | + ) |
| 34 | + # Re-fetch to get clean integer PKs (djongo can return ObjectId on save) |
| 35 | + team_marvel = Team.objects.get(pk=1) |
| 36 | + team_dc = Team.objects.get(pk=2) |
| 37 | + self.stdout.write(self.style.SUCCESS('Inserted 2 teams')) |
| 38 | + |
| 39 | + # Create Users using Django ORM |
| 40 | + self.stdout.write('Inserting users...') |
| 41 | + users_info = [ |
| 42 | + # Team Marvel |
| 43 | + (1, 'Tony Stark', 'ironman@avengers.com', team_marvel, '🦾', 'advanced'), |
| 44 | + (2, 'Steve Rogers', 'captain@avengers.com', team_marvel, '🛡️', 'advanced'), |
| 45 | + (3, 'Natasha Romanoff', 'blackwidow@avengers.com', team_marvel, '🕷️', 'advanced'), |
| 46 | + (4, 'Bruce Banner', 'hulk@avengers.com', team_marvel, '💚', 'advanced'), |
| 47 | + (5, 'Thor Odinson', 'thor@asgard.com', team_marvel, '⚡', 'god-tier'), |
| 48 | + # Team DC |
| 49 | + (6, 'Clark Kent', 'superman@justiceleague.com', team_dc, '🦸', 'god-tier'), |
| 50 | + (7, 'Bruce Wayne', 'batman@gotham.com', team_dc, '🦇', 'advanced'), |
| 51 | + (8, 'Diana Prince', 'wonderwoman@themyscira.com', team_dc, '⭐', 'god-tier'), |
| 52 | + (9, 'Barry Allen', 'flash@speedforce.com', team_dc, '⚡', 'advanced'), |
| 53 | + (10, 'Arthur Curry', 'aquaman@atlantis.com', team_dc, '🔱', 'advanced'), |
| 54 | + ] |
| 55 | + |
| 56 | + users = [] |
| 57 | + for uid, name, email, team, avatar, fitness_level in users_info: |
| 58 | + User.objects.create( |
| 59 | + id=uid, |
| 60 | + name=name, |
| 61 | + email=email, |
| 62 | + team=team, |
| 63 | + avatar=avatar, |
| 64 | + fitness_level=fitness_level, |
| 65 | + ) |
| 66 | + # Re-fetch users with clean integer PKs |
| 67 | + users = list(User.objects.all().order_by('id')) |
| 68 | + self.stdout.write(self.style.SUCCESS(f'Inserted {len(users)} users')) |
| 69 | + |
| 70 | + # Create Activities using Django ORM |
| 71 | + self.stdout.write('Inserting activities...') |
| 72 | + activity_types = ['running', 'cycling', 'swimming', 'strength_training', 'yoga'] |
| 73 | + activities_created = 0 |
| 74 | + activity_id = 1 |
| 75 | + for user in users: |
| 76 | + num_activities = random.randint(5, 10) |
| 77 | + for j in range(num_activities): |
| 78 | + days_ago = random.randint(0, 30) |
| 79 | + activity_date = timezone.now() - timedelta(days=days_ago) |
| 80 | + Activity.objects.create( |
| 81 | + id=activity_id, |
| 82 | + user=user, |
| 83 | + activity_type=random.choice(activity_types), |
| 84 | + duration=random.randint(15, 120), |
| 85 | + date=activity_date, |
| 86 | + notes=f'Great workout session #{j + 1}', |
| 87 | + ) |
| 88 | + activity_id += 1 |
| 89 | + activities_created += 1 |
| 90 | + self.stdout.write(self.style.SUCCESS(f'Inserted {activities_created} activities')) |
| 91 | + |
| 92 | + # Create Leaderboard entries using Django ORM |
| 93 | + self.stdout.write('Calculating leaderboard...') |
| 94 | + leaderboard_entries = [] |
| 95 | + for user in users: |
| 96 | + total_duration = sum( |
| 97 | + a.duration for a in Activity.objects.filter(user=user) |
| 98 | + ) |
| 99 | + leaderboard_entries.append((user, total_duration)) |
| 100 | + |
| 101 | + # Sort by total duration descending and assign ranks |
| 102 | + leaderboard_entries.sort(key=lambda x: x[1], reverse=True) |
| 103 | + for rank, (user, score) in enumerate(leaderboard_entries, start=1): |
| 104 | + Leaderboard.objects.create(id=rank, user=user, score=score, rank=rank) |
| 105 | + self.stdout.write(self.style.SUCCESS(f'Inserted {len(leaderboard_entries)} leaderboard entries')) |
| 106 | + |
| 107 | + # Create Workout suggestions using Django ORM |
| 108 | + self.stdout.write('Inserting workout suggestions...') |
| 109 | + workouts_data = [ |
| 110 | + { |
| 111 | + 'name': 'Super Soldier Circuit', |
| 112 | + 'description': "Captain America's legendary training routine", |
| 113 | + 'difficulty': 'advanced', |
| 114 | + 'exercises': [ |
| 115 | + {'name': 'Push-ups', 'reps': 50, 'sets': 4}, |
| 116 | + {'name': 'Pull-ups', 'reps': 20, 'sets': 4}, |
| 117 | + {'name': 'Squats', 'reps': 50, 'sets': 4}, |
| 118 | + {'name': 'Burpees', 'reps': 30, 'sets': 3}, |
| 119 | + ], |
| 120 | + }, |
| 121 | + { |
| 122 | + 'name': 'Speedster Sprint Training', |
| 123 | + 'description': "Barry Allen's speed-building workout", |
| 124 | + 'difficulty': 'intermediate', |
| 125 | + 'exercises': [ |
| 126 | + {'name': 'Sprint Intervals', 'duration': '30 sec', 'sets': 10}, |
| 127 | + {'name': 'High Knees', 'duration': '1 min', 'sets': 5}, |
| 128 | + {'name': 'Mountain Climbers', 'reps': 40, 'sets': 4}, |
| 129 | + ], |
| 130 | + }, |
| 131 | + { |
| 132 | + 'name': 'Amazonian Warrior Training', |
| 133 | + 'description': "Wonder Woman's combat conditioning", |
| 134 | + 'difficulty': 'advanced', |
| 135 | + 'exercises': [ |
| 136 | + {'name': 'Sword Swings (weighted)', 'reps': 30, 'sets': 5}, |
| 137 | + {'name': 'Shield Holds', 'duration': '2 min', 'sets': 3}, |
| 138 | + {'name': 'Battle Rope', 'duration': '1 min', 'sets': 5}, |
| 139 | + {'name': 'Box Jumps', 'reps': 25, 'sets': 4}, |
| 140 | + ], |
| 141 | + }, |
| 142 | + { |
| 143 | + 'name': 'Atlantean Swim Power', |
| 144 | + 'description': "Aquaman's underwater endurance training", |
| 145 | + 'difficulty': 'intermediate', |
| 146 | + 'exercises': [ |
| 147 | + {'name': 'Freestyle Swimming', 'distance': '1000m', 'sets': 3}, |
| 148 | + {'name': 'Underwater Breath Hold', 'duration': '2 min', 'sets': 5}, |
| 149 | + {'name': 'Treading Water', 'duration': '5 min', 'sets': 3}, |
| 150 | + ], |
| 151 | + }, |
| 152 | + { |
| 153 | + 'name': 'Zen Master Flow', |
| 154 | + 'description': "Black Widow's flexibility and balance routine", |
| 155 | + 'difficulty': 'beginner', |
| 156 | + 'exercises': [ |
| 157 | + {'name': 'Sun Salutations', 'reps': 10, 'sets': 3}, |
| 158 | + {'name': 'Warrior Poses', 'duration': '1 min each', 'sets': 3}, |
| 159 | + {'name': 'Tree Pose', 'duration': '1 min', 'sets': 3}, |
| 160 | + {'name': 'Cobra Stretch', 'duration': '30 sec', 'sets': 4}, |
| 161 | + ], |
| 162 | + }, |
| 163 | + { |
| 164 | + 'name': 'Dark Knight Conditioning', |
| 165 | + 'description': "Batman's stealth and agility training", |
| 166 | + 'difficulty': 'advanced', |
| 167 | + 'exercises': [ |
| 168 | + {'name': 'Parkour Drills', 'duration': '10 min', 'sets': 1}, |
| 169 | + {'name': 'Rope Climbing', 'reps': 10, 'sets': 3}, |
| 170 | + {'name': 'Handstand Push-ups', 'reps': 15, 'sets': 3}, |
| 171 | + {'name': 'Ninja Rolls', 'reps': 20, 'sets': 4}, |
| 172 | + ], |
| 173 | + }, |
| 174 | + { |
| 175 | + 'name': 'Arc Reactor Cardio', |
| 176 | + 'description': "Iron Man's heart-healthy workout", |
| 177 | + 'difficulty': 'intermediate', |
| 178 | + 'exercises': [ |
| 179 | + {'name': 'Cycling', 'duration': '20 min', 'sets': 1}, |
| 180 | + {'name': 'Jumping Jacks', 'reps': 50, 'sets': 3}, |
| 181 | + {'name': 'Step-ups', 'reps': 30, 'sets': 4}, |
| 182 | + ], |
| 183 | + }, |
| 184 | + ] |
| 185 | + |
| 186 | + for w_id, w in enumerate(workouts_data, start=1): |
| 187 | + Workout.objects.create( |
| 188 | + id=w_id, |
| 189 | + name=w['name'], |
| 190 | + description=w['description'], |
| 191 | + difficulty=w['difficulty'], |
| 192 | + exercises=w['exercises'], |
| 193 | + ) |
| 194 | + self.stdout.write(self.style.SUCCESS(f'Inserted {len(workouts_data)} workout suggestions')) |
| 195 | + |
| 196 | + # Summary |
| 197 | + self.stdout.write('\n' + '=' * 50) |
| 198 | + self.stdout.write(self.style.SUCCESS('DATABASE POPULATION COMPLETE!')) |
| 199 | + self.stdout.write('=' * 50 + '\n') |
| 200 | + self.stdout.write('Collection counts:') |
| 201 | + self.stdout.write(f' Users: {User.objects.count()}') |
| 202 | + self.stdout.write(f' Teams: {Team.objects.count()}') |
| 203 | + self.stdout.write(f' Activities: {Activity.objects.count()}') |
| 204 | + self.stdout.write(f' Leaderboard: {Leaderboard.objects.count()}') |
| 205 | + self.stdout.write(f' Workouts: {Workout.objects.count()}') |
| 206 | + |
0 commit comments