-
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 #8 from githubuniverseworkshops/arilivigni/self-se…
…rvice-prompting Update .gitignore, enhance README instructions, and add bson to requi…
- Loading branch information
Showing
19 changed files
with
216 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ venv/ | |
# octofit tracker app frontend and backend | ||
backend/ | ||
frontend/ | ||
management/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Setup database in settings.py, models, views, serializers, and populate data | ||
|
||
## Use Copilot Chat and paste the following | ||
|
||
```text | ||
In our next steps lets think step by step and setup the following in this order | ||
1. settings.py in our django project for mongodb octofit_db database including localhost and the port | ||
2. settings.py in our django project setup for all installed apps. ex djongo, octofit_tracker | ||
3. In octofit_tracker project setup models, views, and serializers for users, teams, activity, leaderboard, and workouts | ||
``` | ||
|
||
![octofit tracker backend settings](./4_1_OctoFitTrackerBackendSettings.png) | ||
![create models serializers](./4_2_CreateModelsViewsSerializers.png) | ||
![backend urls populate data](./4_3_OctoFitAppBackendUrls.png) | ||
|
||
### Sample code for models.py, serializers.py, and views.py | ||
|
||
#### models.py | ||
|
||
```python | ||
# FILE: octofit-tracker/backend/octofit_tracker/models.py | ||
|
||
from django.db import models | ||
from django.contrib.auth.models import AbstractUser, Group, Permission | ||
from djongo import models as djongo_models | ||
|
||
class User(AbstractUser): | ||
id = djongo_models.ObjectIdField(primary_key=True) | ||
groups = models.ManyToManyField( | ||
Group, | ||
related_name='octofit_users', | ||
blank=True, | ||
help_text='The groups this user belongs to.', | ||
verbose_name='groups', | ||
) | ||
user_permissions = models.ManyToManyField( | ||
Permission, | ||
related_name='octofit_users_permissions', | ||
blank=True, | ||
help_text='Specific permissions for this user.', | ||
verbose_name='user permissions', | ||
) | ||
|
||
class Team(models.Model): | ||
id = djongo_models.ObjectIdField(primary_key=True) | ||
name = models.CharField(max_length=100) | ||
members = models.ManyToManyField(User) | ||
|
||
class Activity(models.Model): | ||
id = djongo_models.ObjectIdField(primary_key=True) | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) | ||
activity_type = models.CharField(max_length=50) | ||
duration = models.DurationField() | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
|
||
class Workout(models.Model): | ||
id = djongo_models.ObjectIdField(primary_key=True) | ||
name = models.CharField(max_length=100) | ||
description = models.TextField() | ||
suggested_duration = models.DurationField() | ||
|
||
class Leaderboard(models.Model): | ||
id = djongo_models.ObjectIdField(primary_key=True) | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) | ||
total_points = models.IntegerField() | ||
``` | ||
|
||
#### serializers.py | ||
|
||
```python | ||
# FILE: octofit-tracker/backend/octofit_tracker/serializers.py | ||
|
||
from rest_framework import serializers | ||
from .models import User, Team, Activity, Leaderboard, Workout | ||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = '__all__' | ||
|
||
class TeamSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Team | ||
fields = '__all__' | ||
|
||
class ActivitySerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Activity | ||
fields = '__all__' | ||
|
||
class LeaderboardSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Leaderboard | ||
fields = '__all__' | ||
|
||
class WorkoutSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Workout | ||
fields = '__all__' | ||
``` | ||
|
||
#### views.py | ||
|
||
```python | ||
# FILE: octofit-tracker/backend/octofit_tracker/views.py | ||
|
||
from rest_framework import viewsets | ||
from .models import User, Team, Activity, Leaderboard, Workout | ||
from .serializers import UserSerializer, TeamSerializer, ActivitySerializer, LeaderboardSerializer, WorkoutSerializer | ||
|
||
class UserViewSet(viewsets.ModelViewSet): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
|
||
class TeamViewSet(viewsets.ModelViewSet): | ||
queryset = Team.objects.all() | ||
serializer_class = TeamSerializer | ||
|
||
class ActivityViewSet(viewsets.ModelViewSet): | ||
queryset = Activity.objects.all() | ||
serializer_class = ActivitySerializer | ||
|
||
class LeaderboardViewSet(viewsets.ModelViewSet): | ||
queryset = Leaderboard.objects.all() | ||
serializer_class = LeaderboardSerializer | ||
|
||
class WorkoutViewSet(viewsets.ModelViewSet): | ||
queryset = Workout.objects.all() | ||
serializer_class = WorkoutSerializer | ||
``` | ||
|
||
[Back :: Previous: Getting started](../3_GettingStarted) | [Next :: Populate database with data via manage.py](../5_PopulateDBwData) |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# Populate database with data via manage.py | ||
|
||
## Example of not being specific | ||
|
||
Type the following prompt: | ||
|
||
```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 | ||
``` | ||
|
||
### Example of not being specfic in prompting Copilot Chat | ||
|
||
![populate db Code your app name](./5_1_PopulateDbCodeYourAppNameFirst.png) | ||
|
||
## Example of being more specific in our prompt | ||
|
||
### Let's be more specific and ask Copilot to update the output with the octofit_tracker app name | ||
|
||
```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 in the octofit_tracker project | ||
``` | ||
|
||
![populate db Code app name octofit_tracker](./5_2_PopulateDbCodeOctoFitAppSecond.png) | ||
|
||
### Sample code for populate_db.py | ||
|
||
```python | ||
# FILE: octofit-tracker/backend/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 datetime import timedelta | ||
|
||
class Command(BaseCommand): | ||
help = 'Populate the database with initial data' | ||
|
||
def handle(self, *args, **kwargs): | ||
# Create some users | ||
user1 = User.objects.create_user(username='user1', email='[email protected]', password='password') | ||
user2 = User.objects.create_user(username='user2', email='[email protected]', password='password') | ||
|
||
# Create a team | ||
team = Team.objects.create(name='Team A') | ||
team.members.set([user1, user2]) | ||
|
||
# Create some activities | ||
Activity.objects.create(user=user1, activity_type='Running', duration=timedelta(minutes=30)) | ||
Activity.objects.create(user=user2, activity_type='Cycling', duration=timedelta(hours=1)) | ||
|
||
# Create some workouts | ||
Workout.objects.create(name='Workout A', description='Description A', suggested_duration=timedelta(minutes=45)) | ||
Workout.objects.create(name='Workout B', description='Description B', suggested_duration=timedelta(hours=1, minutes=15)) | ||
|
||
# Create leaderboard entries | ||
Leaderboard.objects.create(user=user1, total_points=100) | ||
Leaderboard.objects.create(user=user2, total_points=150) | ||
|
||
self.stdout.write(self.style.SUCCESS('Database populated successfully')) | ||
``` | ||
|
||
![Migrate and populate db](./5_3_MigratePopulateDb.png) |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Django==4.1 | ||
#bson==0.5.10 | ||
djangorestframework==3.14.0 | ||
django-allauth==0.51.0 | ||
dj-rest-auth | ||
|