|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | +from octofit_tracker.models import User, Team, Activity, Leaderboard, Workout |
| 3 | +from djongo import models |
| 4 | + |
| 5 | +class Command(BaseCommand): |
| 6 | + help = 'Populate the octofit_db database with test data' |
| 7 | + |
| 8 | + def handle(self, *args, **options): |
| 9 | + # Delete existing data |
| 10 | + Activity.objects.all().delete() |
| 11 | + Workout.objects.all().delete() |
| 12 | + Leaderboard.objects.all().delete() |
| 13 | + User.objects.all().delete() |
| 14 | + Team.objects.all().delete() |
| 15 | + |
| 16 | + # Create teams |
| 17 | + marvel = Team.objects.create(name='Marvel') |
| 18 | + dc = Team.objects.create(name='DC') |
| 19 | + |
| 20 | + # Create users |
| 21 | + users = [ |
| 22 | + User.objects.create(name='Spider-Man', email='spiderman@marvel.com', team=marvel), |
| 23 | + User.objects.create(name='Iron Man', email='ironman@marvel.com', team=marvel), |
| 24 | + User.objects.create(name='Wonder Woman', email='wonderwoman@dc.com', team=dc), |
| 25 | + User.objects.create(name='Batman', email='batman@dc.com', team=dc), |
| 26 | + ] |
| 27 | + |
| 28 | + # Create activities |
| 29 | + Activity.objects.create(user=users[0], type='Running', duration=30) |
| 30 | + Activity.objects.create(user=users[1], type='Cycling', duration=45) |
| 31 | + Activity.objects.create(user=users[2], type='Swimming', duration=60) |
| 32 | + Activity.objects.create(user=users[3], type='Yoga', duration=20) |
| 33 | + |
| 34 | + # Create workouts |
| 35 | + workout1 = Workout.objects.create(name='Hero HIIT', description='High intensity interval training for heroes') |
| 36 | + workout2 = Workout.objects.create(name='Power Yoga', description='Yoga for strength and flexibility') |
| 37 | + workout1.suggested_for.set([users[0], users[1]]) |
| 38 | + workout2.suggested_for.set([users[2], users[3]]) |
| 39 | + |
| 40 | + # Create leaderboard |
| 41 | + Leaderboard.objects.create(team=marvel, points=100) |
| 42 | + Leaderboard.objects.create(team=dc, points=90) |
| 43 | + |
| 44 | + # Ensure unique index on email |
| 45 | + from pymongo import MongoClient |
| 46 | + client = MongoClient('localhost', 27017) |
| 47 | + db = client['octofit_db'] |
| 48 | + db.users.create_index('email', unique=True) |
| 49 | + |
| 50 | + self.stdout.write(self.style.SUCCESS('Test data populated successfully!')) |
0 commit comments