-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from githubuniverseworkshops/arilivigni/self-s…
…ervice-prompting Refactor README and backend setup instructions
- Loading branch information
Showing
5 changed files
with
154 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
> NOTE: This is an example of not being specific skip this prompt and go to the next one | ||
```text | ||
Let's use manage.py to get everything setup we need to create init.py for populate_db command include steps to migrate as well | ||
Let's use manage.py to get everything setup we need to populate_db command include steps to migrate as well | ||
``` | ||
|
||
### Example of not being specfic in prompting Copilot Chat | ||
|
@@ -19,10 +19,11 @@ Let's use manage.py to get everything setup we need to create init.py for popula | |
Type the following prompt in GitHub Copilot Chat: | ||
|
||
```text | ||
Let's use manage.py to get the database setup and populated | ||
Let's use manage.py to get the database setup and populated based on fields in models.py | ||
- Populate the database with super hero users | ||
- Create full setup for a command populate_db.py | ||
- Create populate_db.py so it initializes and deletes previous data and recreates it | ||
- populate_db.py creates users, teams, activities, leaderboards, and workouts | ||
- users will be super hero users | ||
- Include steps to migrate in the octofit_tracker project | ||
``` | ||
|
||
|
@@ -33,29 +34,112 @@ Let's use manage.py to get the database setup and populated | |
```python | ||
# FILE: octofit-tracker/backend/octofit_tracker/management/commands/populate_db.py | ||
|
||
# octofit_tracker/management/commands/populate_db.py | ||
|
||
from django.core.management.base import BaseCommand | ||
from octofit_tracker.models import User, Team, Activity, Workout, Leaderboard | ||
from octofit_tracker.models import User, Team, Activity, Leaderboard, Workout | ||
from django.conf import settings | ||
from pymongo import MongoClient | ||
from datetime import timedelta | ||
|
||
class Command(BaseCommand): | ||
help = 'Populate the database with initial data' | ||
help = 'Populate the database with superhero users, teams, activities, leaderboards, and workouts' | ||
|
||
def handle(self, *args, **kwargs): | ||
# Create Superhero Users | ||
# Connect to MongoDB | ||
client = MongoClient(settings.DATABASES['default']['HOST'], settings.DATABASES['default']['PORT']) | ||
db = client[settings.DATABASES['default']['NAME']] | ||
|
||
# Drop existing collections | ||
db.users.drop() | ||
db.teams.drop() | ||
db.activities.drop() | ||
db.leaderboards.drop() | ||
db.workouts.drop() | ||
|
||
# Populate users | ||
users = [ | ||
{'username': 'superman', 'email': '[email protected]', 'password': 'superpassword'}, | ||
{'username': 'batman', 'email': '[email protected]', 'password': 'batpassword'}, | ||
{'username': 'wonderwoman', 'email': '[email protected]', 'password': 'wonderpassword'}, | ||
{'username': 'flash', 'email': '[email protected]', 'password': 'flashpassword'}, | ||
{'username': 'aquaman', 'email': '[email protected]', 'password': 'aquapassword'}, | ||
] | ||
|
||
user_objects = [] | ||
for user_data in users: | ||
user, created = User.objects.get_or_create(**user_data) | ||
user, created = User.objects.get_or_create(email=user_data['email'], defaults=user_data) | ||
user_objects.append(user) | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f"User {user.username} created")) | ||
self.stdout.write(self.style.SUCCESS(f'Successfully created user {user.username}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'User {user.username} already exists')) | ||
|
||
# Ensure all user objects are saved | ||
for user in user_objects: | ||
user.save() | ||
|
||
# Add more data population logic here if needed | ||
# Populate teams | ||
teams = [ | ||
{'name': 'Justice League', 'members': [user_objects[0], user_objects[1], user_objects[2], user_objects[3], user_objects[4]]}, | ||
] | ||
|
||
self.stdout.write(self.style.SUCCESS('Database populated successfully')) | ||
for team_data in teams: | ||
team, created = Team.objects.get_or_create(name=team_data['name']) | ||
if created: | ||
for member in team_data['members']: | ||
team.members.add(member) | ||
self.stdout.write(self.style.SUCCESS(f'Successfully created team {team.name}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'Team {team.name} already exists')) | ||
|
||
# Populate activities | ||
activities = [ | ||
{'user': user_objects[0], 'activity_type': 'Flying', 'duration': timedelta(hours=1)}, | ||
{'user': user_objects[1], 'activity_type': 'Martial Arts', 'duration': timedelta(hours=2)}, | ||
{'user': user_objects[2], 'activity_type': 'Training', 'duration': timedelta(hours=1, minutes=30)}, | ||
{'user': user_objects[3], 'activity_type': 'Running', 'duration': timedelta(minutes=30)}, | ||
{'user': user_objects[4], 'activity_type': 'Swimming', 'duration': timedelta(hours=1, minutes=15)}, | ||
] | ||
|
||
for activity_data in activities: | ||
activity, created = Activity.objects.get_or_create(**activity_data) | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f'Successfully created activity for {activity.user.username}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'Activity for {activity.user.username} already exists')) | ||
|
||
# Populate leaderboards | ||
leaderboards = [ | ||
{'user': user_objects[0], 'score': 100}, | ||
{'user': user_objects[1], 'score': 90}, | ||
{'user': user_objects[2], 'score': 95}, | ||
{'user': user_objects[3], 'score': 85}, | ||
{'user': user_objects[4], 'score': 80}, | ||
] | ||
|
||
for leaderboard_data in leaderboards: | ||
leaderboard, created = Leaderboard.objects.get_or_create(**leaderboard_data) | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f'Successfully created leaderboard entry for {leaderboard.user.username}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'Leaderboard entry for {leaderboard.user.username} already exists')) | ||
|
||
# Populate workouts | ||
workouts = [ | ||
{'name': 'Super Strength Training', 'description': 'Training for super strength'}, | ||
{'name': 'Martial Arts Training', 'description': 'Training for martial arts'}, | ||
{'name': 'Amazonian Training', 'description': 'Training for Amazonian warriors'}, | ||
{'name': 'Speed Training', 'description': 'Training for super speed'}, | ||
{'name': 'Aquatic Training', 'description': 'Training for underwater activities'}, | ||
] | ||
|
||
for workout_data in workouts: | ||
workout, created = Workout.objects.get_or_create(**workout_data) | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f'Successfully created workout {workout.name}')) | ||
else: | ||
self.stdout.write(self.style.WARNING(f'Workout {workout.name} already exists')) | ||
``` | ||
|
||
![Migrate and populate db](./5_3_MigratePopulateDb.png) |