Skip to content

Commit

Permalink
Merge pull request #8 from githubuniverseworkshops/arilivigni/self-se…
Browse files Browse the repository at this point in the history
…rvice-prompting

Update .gitignore, enhance README instructions, and add bson to requi…
  • Loading branch information
bryantson authored Oct 21, 2024
2 parents 56ba626 + 3a00b72 commit 9774087
Show file tree
Hide file tree
Showing 19 changed files with 216 additions and 32 deletions.
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
]}
},
"forwardPorts": [
3000, // React default port
8000, // Django default port
27017 // MongoDB default port
]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ venv/
# octofit tracker app frontend and backend
backend/
frontend/
management/
Binary file modified docs/3_GettingStarted/3_1_AskCopilotProjectCreation.png
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.
Binary file added docs/3_GettingStarted/3_2_StepByStep.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/3_GettingStarted/3_3_OctFit-Tracker-Setup.png
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.
33 changes: 19 additions & 14 deletions docs/3_GettingStarted/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ In this section, we will start by setting up the face of our OctoFit application
## Explain to GitHub Copilot the goals and steps

It is important to lay out a plan and provide details
It is important to lay out a plan and provide details. Type the following prompt:

```text
I want to build an OctoFit Tracker app that will include the following:
* User authentication and profiles
* Activity logging and tracking
* Team creation and management
* Competitive leader boards
* Personalized workout suggestions in one app
* Activity logging and tracking
* Team creation and management
* Competitive leader boards
* Personalized workout suggestions
I want to use React.js for the frontend. I want to use Python with Django REST Framework for the backend and Mongo DB as the database to store data.
It should be in one app
Create the frontend and backend in the octofit-tracker directory of this repository
generate instructions in this order
the octofit-tracker/backend directory will store the django app with no subdirectories
the octofit-tracker/frontend directory will store the react app with no subdirectories
1. Create the frontend and backend in the octofit-tracker directory of this repository in one command
2. the octofit-tracker/backend directory will store the django app with no subdirectories
3. setup backend python venv and install octofit-tracker/requirements.txt first
4. Create the django app directly in the directory octofit_tracker/backend
5. setup the octofit-tracker/frontend directory will store the react app with no subdirectories
6. instlal bootstrap
7. Install and setup mongodb with the 'sudo service mongodb start' and 'sudo service mongodb status'
Use a Python virtual environment and install all python dependencies from file octofit-tracker/requirements.txt in this workspace
Expand All @@ -38,10 +43,10 @@ Let's think about this step by step
Important to avoid using public code and we do NOT need to initialize the git repository
```

![project plan](./3_1_AskCopilotProjectCreation.png)
![create project plan](./3_1_AskCopilotProjectCreation.png)

![directory structure](./3_2_CopilotDirectoryStructure.png)
![step by step](./3_2_StepByStep.png)

![octofit-tracker app setup](./3_3_OctFit-Tracker-Setup.png)
![octofit-tracker app directory tree](./3_3_OctoFitTrackerDirTree.png)

[Back :: Previous: Prerequisites and development environment setup](../2_Prerequisites) | [Next :: MongoDB install and setup - ](../4_MongoDBInstallSetup)
[Back :: Previous: Prerequisites and development environment setup](../2_Prerequisites) | [Next :: OctoFit app backend setup](../4_BackendSettings/)
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.
133 changes: 133 additions & 0 deletions docs/4_BackendSettings/README.md
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 removed docs/4_MongoDBInstallSetup/4_1_mongodbSetup.png
Binary file not shown.
18 changes: 0 additions & 18 deletions docs/4_MongoDBInstallSetup/README.md

This file was deleted.

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.
Binary file added docs/5_PopulateDBwData/5_3_MigratePopulateDb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions docs/5_PopulateDBwData/README.md
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)
1 change: 1 addition & 0 deletions octofit-tracker/requirements.txt
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
Expand Down

0 comments on commit 9774087

Please sign in to comment.