|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | +from django.utils import timezone |
| 3 | +from datetime import timedelta |
| 4 | +from octofit_tracker.models import User, Team, Activity, Leaderboard, Workout |
| 5 | +from bson import ObjectId |
| 6 | + |
| 7 | + |
| 8 | +class Command(BaseCommand): |
| 9 | + help = 'Populate the octofit_db database with test data' |
| 10 | + |
| 11 | + def handle(self, *args, **kwargs): |
| 12 | + self.stdout.write('Clearing existing data...') |
| 13 | + |
| 14 | + # Delete all existing data using Django ORM |
| 15 | + User.objects.all().delete() |
| 16 | + Team.objects.all().delete() |
| 17 | + Activity.objects.all().delete() |
| 18 | + Leaderboard.objects.all().delete() |
| 19 | + Workout.objects.all().delete() |
| 20 | + |
| 21 | + self.stdout.write(self.style.SUCCESS('Existing data cleared')) |
| 22 | + |
| 23 | + # Create Teams |
| 24 | + self.stdout.write('Creating teams...') |
| 25 | + team_marvel = Team.objects.create( |
| 26 | + name='Team Marvel', |
| 27 | + description='Earth\'s Mightiest Heroes' |
| 28 | + ) |
| 29 | + team_dc = Team.objects.create( |
| 30 | + name='Team DC', |
| 31 | + description='Justice League United' |
| 32 | + ) |
| 33 | + self.stdout.write(self.style.SUCCESS(f'Created teams: {team_marvel.name}, {team_dc.name}')) |
| 34 | + |
| 35 | + # Create Users (Superheroes) |
| 36 | + self.stdout.write('Creating users...') |
| 37 | + |
| 38 | + # Team Marvel users |
| 39 | + iron_man = User.objects.create( |
| 40 | + name='Tony Stark', |
| 41 | + email='ironman@marvel.com', |
| 42 | + password='arc_reactor_2024', |
| 43 | + team_id=str(team_marvel._id) |
| 44 | + ) |
| 45 | + |
| 46 | + captain_america = User.objects.create( |
| 47 | + name='Steve Rogers', |
| 48 | + email='captainamerica@marvel.com', |
| 49 | + password='shield_forever', |
| 50 | + team_id=str(team_marvel._id) |
| 51 | + ) |
| 52 | + |
| 53 | + thor = User.objects.create( |
| 54 | + name='Thor Odinson', |
| 55 | + email='thor@marvel.com', |
| 56 | + password='worthy_mjolnir', |
| 57 | + team_id=str(team_marvel._id) |
| 58 | + ) |
| 59 | + |
| 60 | + hulk = User.objects.create( |
| 61 | + name='Bruce Banner', |
| 62 | + email='hulk@marvel.com', |
| 63 | + password='gamma_smash', |
| 64 | + team_id=str(team_marvel._id) |
| 65 | + ) |
| 66 | + |
| 67 | + black_widow = User.objects.create( |
| 68 | + name='Natasha Romanoff', |
| 69 | + email='blackwidow@marvel.com', |
| 70 | + password='red_room_spy', |
| 71 | + team_id=str(team_marvel._id) |
| 72 | + ) |
| 73 | + |
| 74 | + # Team DC users |
| 75 | + superman = User.objects.create( |
| 76 | + name='Clark Kent', |
| 77 | + email='superman@dc.com', |
| 78 | + password='krypton_power', |
| 79 | + team_id=str(team_dc._id) |
| 80 | + ) |
| 81 | + |
| 82 | + batman = User.objects.create( |
| 83 | + name='Bruce Wayne', |
| 84 | + email='batman@dc.com', |
| 85 | + password='dark_knight', |
| 86 | + team_id=str(team_dc._id) |
| 87 | + ) |
| 88 | + |
| 89 | + wonder_woman = User.objects.create( |
| 90 | + name='Diana Prince', |
| 91 | + email='wonderwoman@dc.com', |
| 92 | + password='amazon_warrior', |
| 93 | + team_id=str(team_dc._id) |
| 94 | + ) |
| 95 | + |
| 96 | + flash = User.objects.create( |
| 97 | + name='Barry Allen', |
| 98 | + email='flash@dc.com', |
| 99 | + password='speed_force', |
| 100 | + team_id=str(team_dc._id) |
| 101 | + ) |
| 102 | + |
| 103 | + aquaman = User.objects.create( |
| 104 | + name='Arthur Curry', |
| 105 | + email='aquaman@dc.com', |
| 106 | + password='atlantis_king', |
| 107 | + team_id=str(team_dc._id) |
| 108 | + ) |
| 109 | + |
| 110 | + marvel_users = [iron_man, captain_america, thor, hulk, black_widow] |
| 111 | + dc_users = [superman, batman, wonder_woman, flash, aquaman] |
| 112 | + all_users = marvel_users + dc_users |
| 113 | + |
| 114 | + self.stdout.write(self.style.SUCCESS(f'Created {len(all_users)} users')) |
| 115 | + |
| 116 | + # Create Activities |
| 117 | + self.stdout.write('Creating activities...') |
| 118 | + activity_types = ['Running', 'Cycling', 'Swimming', 'Weight Training', 'Boxing', 'Yoga'] |
| 119 | + |
| 120 | + activities_created = 0 |
| 121 | + for i, user in enumerate(all_users): |
| 122 | + # Each user has 3-5 activities |
| 123 | + num_activities = 3 + (i % 3) |
| 124 | + for j in range(num_activities): |
| 125 | + activity_type = activity_types[j % len(activity_types)] |
| 126 | + duration = 30 + (j * 15) |
| 127 | + calories = duration * 8 |
| 128 | + distance = (duration / 60) * 10 if activity_type in ['Running', 'Cycling', 'Swimming'] else None |
| 129 | + |
| 130 | + Activity.objects.create( |
| 131 | + user_id=str(user._id), |
| 132 | + activity_type=activity_type, |
| 133 | + duration=duration, |
| 134 | + calories_burned=calories, |
| 135 | + distance=distance, |
| 136 | + date=timezone.now() - timedelta(days=i, hours=j), |
| 137 | + notes=f'{user.name} doing {activity_type}' |
| 138 | + ) |
| 139 | + activities_created += 1 |
| 140 | + |
| 141 | + self.stdout.write(self.style.SUCCESS(f'Created {activities_created} activities')) |
| 142 | + |
| 143 | + # Create Leaderboard entries |
| 144 | + self.stdout.write('Creating leaderboard entries...') |
| 145 | + |
| 146 | + for user in all_users: |
| 147 | + user_activities = Activity.objects.filter(user_id=str(user._id)) |
| 148 | + total_activities = user_activities.count() |
| 149 | + total_calories = sum(a.calories_burned for a in user_activities) |
| 150 | + total_distance = sum(a.distance for a in user_activities if a.distance) |
| 151 | + |
| 152 | + team = team_marvel if user.team_id == str(team_marvel._id) else team_dc |
| 153 | + |
| 154 | + Leaderboard.objects.create( |
| 155 | + user_id=str(user._id), |
| 156 | + user_name=user.name, |
| 157 | + team_id=str(team._id), |
| 158 | + team_name=team.name, |
| 159 | + total_activities=total_activities, |
| 160 | + total_calories=total_calories, |
| 161 | + total_distance=total_distance, |
| 162 | + rank=0 # Will be calculated based on total_calories |
| 163 | + ) |
| 164 | + |
| 165 | + # Update rankings based on total_calories |
| 166 | + leaderboard_entries = Leaderboard.objects.all().order_by('-total_calories') |
| 167 | + for rank, entry in enumerate(leaderboard_entries, start=1): |
| 168 | + entry.rank = rank |
| 169 | + entry.save() |
| 170 | + |
| 171 | + self.stdout.write(self.style.SUCCESS(f'Created {leaderboard_entries.count()} leaderboard entries')) |
| 172 | + |
| 173 | + # Create Workouts |
| 174 | + self.stdout.write('Creating workouts...') |
| 175 | + |
| 176 | + workouts_data = [ |
| 177 | + { |
| 178 | + 'name': 'Super Soldier Training', |
| 179 | + 'description': 'High-intensity workout inspired by Captain America', |
| 180 | + 'category': 'Strength', |
| 181 | + 'difficulty_level': 'Advanced', |
| 182 | + 'estimated_duration': 60, |
| 183 | + 'estimated_calories': 500 |
| 184 | + }, |
| 185 | + { |
| 186 | + 'name': 'Asgardian Power Lift', |
| 187 | + 'description': 'Weightlifting routine fit for a god', |
| 188 | + 'category': 'Strength', |
| 189 | + 'difficulty_level': 'Expert', |
| 190 | + 'estimated_duration': 45, |
| 191 | + 'estimated_calories': 400 |
| 192 | + }, |
| 193 | + { |
| 194 | + 'name': 'Speed Force Cardio', |
| 195 | + 'description': 'Lightning-fast cardio workout', |
| 196 | + 'category': 'Cardio', |
| 197 | + 'difficulty_level': 'Intermediate', |
| 198 | + 'estimated_duration': 30, |
| 199 | + 'estimated_calories': 350 |
| 200 | + }, |
| 201 | + { |
| 202 | + 'name': 'Bat Cave HIIT', |
| 203 | + 'description': 'High-intensity interval training in the shadows', |
| 204 | + 'category': 'HIIT', |
| 205 | + 'difficulty_level': 'Advanced', |
| 206 | + 'estimated_duration': 40, |
| 207 | + 'estimated_calories': 450 |
| 208 | + }, |
| 209 | + { |
| 210 | + 'name': 'Amazonian Warrior Yoga', |
| 211 | + 'description': 'Flexibility and strength from Themyscira', |
| 212 | + 'category': 'Flexibility', |
| 213 | + 'difficulty_level': 'Beginner', |
| 214 | + 'estimated_duration': 50, |
| 215 | + 'estimated_calories': 200 |
| 216 | + }, |
| 217 | + { |
| 218 | + 'name': 'Arc Reactor Endurance', |
| 219 | + 'description': 'Tony Stark\'s personal endurance routine', |
| 220 | + 'category': 'Endurance', |
| 221 | + 'difficulty_level': 'Intermediate', |
| 222 | + 'estimated_duration': 55, |
| 223 | + 'estimated_calories': 380 |
| 224 | + }, |
| 225 | + { |
| 226 | + 'name': 'Hulk Smash Circuit', |
| 227 | + 'description': 'Power-packed circuit training', |
| 228 | + 'category': 'Circuit', |
| 229 | + 'difficulty_level': 'Expert', |
| 230 | + 'estimated_duration': 35, |
| 231 | + 'estimated_calories': 480 |
| 232 | + }, |
| 233 | + { |
| 234 | + 'name': 'Atlantean Swimming', |
| 235 | + 'description': 'Under the sea swimming workout', |
| 236 | + 'category': 'Cardio', |
| 237 | + 'difficulty_level': 'Intermediate', |
| 238 | + 'estimated_duration': 45, |
| 239 | + 'estimated_calories': 320 |
| 240 | + } |
| 241 | + ] |
| 242 | + |
| 243 | + for workout_data in workouts_data: |
| 244 | + Workout.objects.create(**workout_data) |
| 245 | + |
| 246 | + self.stdout.write(self.style.SUCCESS(f'Created {len(workouts_data)} workouts')) |
| 247 | + |
| 248 | + # Summary |
| 249 | + self.stdout.write(self.style.SUCCESS('\n=== Database Population Complete ===')) |
| 250 | + self.stdout.write(f'Teams: {Team.objects.count()}') |
| 251 | + self.stdout.write(f'Users: {User.objects.count()}') |
| 252 | + self.stdout.write(f'Activities: {Activity.objects.count()}') |
| 253 | + self.stdout.write(f'Leaderboard Entries: {Leaderboard.objects.count()}') |
| 254 | + self.stdout.write(f'Workouts: {Workout.objects.count()}') |
0 commit comments