Skip to content

Commit 636bec3

Browse files
authored
Initial commit
0 parents  commit 636bec3

File tree

85 files changed

+62876
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+62876
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
#static
7+
lab*.*
8+
containerize*
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
pip-wheel-metadata/
28+
share/python-wheels/
29+
*.egg-info/
30+
.installed.cfg
31+
*.egg
32+
MANIFEST
33+
34+
# PyInstaller
35+
# Usually these files are written by a python script from a template
36+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
37+
*.manifest
38+
*.spec
39+
40+
# Installer logs
41+
pip-log.txt
42+
pip-delete-this-directory.txt
43+
44+
# Unit test / coverage reports
45+
htmlcov/
46+
.tox/
47+
.nox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*.cover
54+
*.py,cover
55+
.hypothesis/
56+
.pytest_cache/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
target/
80+
81+
# Jupyter Notebook
82+
.ipynb_checkpoints
83+
84+
# IPython
85+
profile_default/
86+
ipython_config.py
87+
88+
# pyenv
89+
.python-version
90+
91+
# pipenv
92+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
94+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
95+
# install all needed dependencies.
96+
#Pipfile.lock
97+
98+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
99+
__pypackages__/
100+
101+
# Celery stuff
102+
celerybeat-schedule
103+
celerybeat.pid
104+
105+
# SageMath parsed files
106+
*.sage.py
107+
108+
# Environments
109+
.env
110+
.venv
111+
env/
112+
venv/
113+
ENV/
114+
env.bak/
115+
venv.bak/
116+
117+
# Spyder project settings
118+
.spyderproject
119+
.spyproject
120+
121+
# Rope project settings
122+
.ropeproject
123+
124+
# mkdocs documentation
125+
/site
126+
127+
# mypy
128+
.mypy_cache/
129+
.dmypy.json
130+
dmypy.json
131+
132+
# Pyre type checker
133+
.pyre/

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 1. insert code to python:3.9.16-slim as base image
2+
3+
ENV PYTHONBUFFERED 1
4+
ENV PYTHONWRITEBYTECODE 1
5+
6+
RUN apt-get update \
7+
&& apt-get install -y netcat
8+
9+
ENV APP=/app
10+
11+
# 2. insert code to change the working directory to $APP
12+
13+
# 3. insert code to copy the requirements.txt file to $APP
14+
15+
# 4. insert code to install requirements from requirements.txt
16+
17+
# 5. insert code to copy the rest of the files into $APP
18+
19+
# 6. insert code to expose the port here
20+
21+
RUN chmod +x /app/entrypoint.sh
22+
23+
ENTRYPOINT ["/bin/bash","/app/entrypoint.sh"]
24+
25+
# 7. insert code to set the run command to "python manage.py runserver 0.0.0.0:8000"
26+

README.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Lab
2+
3+
1. Clone git repo: `git clone https://github.com/ibm-developer-skills-network/sfvih-Back-end-Development-Capstone.git`
4+
2. Get into the directory by `cd sfvih-Back-end-Development-Capstone`
5+
3. Get into Django code `cd djangoserver`
6+
4. Install requirements `pip install -r requirements.txt`
7+
5. Run the server `python manage.py runserver`
8+
6. It will tell you that you have unapplied migrations.
9+
10+
**Migrations** are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into. There are several commands which you will use to interact with migrations and Django’s handling of database schema:
11+
12+
1. **_migrate_**, which is responsible for applying and unapplying migrations.
13+
2. **_makemigrations_**, which is responsible for creating new migrations based on the changes you have made to your models.
14+
3. **_sqlmigrate_**, which displays the SQL statements for a migration.
15+
4. **_showmigrations_**, which lists a project’s migrations and their status.
16+
17+
7. Create the initial migrations and generate the database schema:
18+
19+
```shell
20+
python manage.py makemigrations
21+
python manage.py migrate
22+
```
23+
24+
8. Run server successfully this time: `python manage.py runserver`
25+
9. Launch Application
26+
10. Click on Songs and Photos
27+
11. Click on Concerts, no existing Concert present
28+
12. Let's create admin user `python manage.py createsuperuser`
29+
1. Username: `admin`
30+
2. Email address: _leave blank, simply press enter_
31+
3. Password: Your choice, or simply `qwerty123`
32+
13. Run the server again `python manage.py runserver` and goto admin: `http://localhost:8000/admin/`
33+
14. Enter the admin user details you created in previous step.
34+
15. Now you are in the admin section built by Django.
35+
36+
One of the most powerful parts of Django is the **automatic admin interface**. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.
37+
38+
16. Add a Concert:
39+
1. Concert name: `Coachella 2023`
40+
2. Duration: `72`
41+
3. City: `Indio, California`
42+
4. Date: `2023-04-14`
43+
17. Click on `View Site` meny at the top
44+
18. Now if you visit `Concerts`, you will see Coachella listed.
45+
19. Our Django application is now running, but Songs and Photos are hard coded.
46+
20. Open `concert\views.py`
47+
21. See `songs` and `photos` definition.
48+
49+
1. Retrieve `songs` from a REST endpoint by replacing the code with following:
50+
51+
```python
52+
songs = req.get(
53+
"https://raw.githubusercontent.com/captainfedoraskillup/private-get-songs/main/backend/data/songs.json").json()
54+
return render(request, "songs.html", {"songs": songs})
55+
```
56+
57+
2. Retrieve `photos` from a REST endpoint by replacing the code with following:
58+
59+
```python
60+
photos = req.get(
61+
"https://raw.githubusercontent.com/captainfedoraskillup/private-get-pictures/main/backend/data/pictures.json").json()
62+
return render(request, "photos.html", {"photos": photos})
63+
```
64+
65+
22. Verify Songs and Photos changes. Visit the Songs section, you will see a longer list of songs, clicking on each will show its Lyrics in a modal dialog. While going into Photos, you will see more than two.
66+
23. Now back to Concerts, click on the concert Coachella we created. You will see an RSVP page.
67+
24. RSVP Page shows you details of the Concert along with an option to either: Attend, Not Attend or no Option `-`.
68+
25. If you open `concert_detail.html`, you will see an html form:
69+
70+
```html
71+
<form action="{% url 'concert_attendee' %}" method="POST">
72+
{% csrf_token %}
73+
<input
74+
name="concert_id"
75+
type="number"
76+
value="{{concert_details.id}}"
77+
hidden="hidden"
78+
/>
79+
<div class="input-group mb-3">
80+
<label class="input-group-text" for="attendee_choice">RSVP</label>
81+
<select class="form-select" name="attendee_choice" required>
82+
{% for attending_choice in attending_choices %}
83+
<option {% if attending_choice.0 == status %}selected {% endif %} value="{{ attending_choice.0 }}">{{ attending_choice.1 }}</option>
84+
{% endfor %}
85+
</select>
86+
</div>
87+
<input type="submit" class="btn btn-primary" />
88+
</form>
89+
```
90+
91+
26. On `Submit` of this form, the details are sent to `concert_attendee` in `concert\views.py`.
92+
27. It does two validations:
93+
`if request.user.is_authenticated:` means whether the user is authenticated. Because anonymous users are not allowed to RSVP.
94+
Then `if request.method == "POST":` to check whether it is an `HTTP POST` event.
95+
28. From the `body` of the `POST` method, it takes `concert_id` and `attendee_choice`.
96+
29. Then it checks, whether a selection was made previously, if yes, then update it. Otherwise insert new selection the databaes for this user.
97+
30. Finally, redirect the user.
98+
99+
## Cleanup while testing
100+
101+
```shell
102+
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
103+
find . -path "*/migrations/*.pyc" -delete
104+
find . -path "*/db.sqlite3" -delete
105+
python manage.py makemigrations
106+
python manage.py migrate
107+
python manage.py runserver
108+
```
109+
110+
##  To move the data from SQLite to MySQL
111+
112+
Execute:
113+
114+
`python manage.py dumpdata > datadump.json`
115+
116+
Next, change your `settings.py` to the mysql database.
117+
118+
Finally:
119+
120+
`python manage.py loaddata datadump.json`
121+
122+
##  Containerize the application
123+
1. build a docker image
124+
```
125+
docker build . -t concert
126+
```
127+
1. tag docker image with the correct registry information
128+
```
129+
docker tag concert captainfedora/concert:v1
130+
```
131+
The above command tags to `captainfedora` repository on dockerhub with the image name of `concert` and label of `v1`
132+
133+
1. Run the docker image to validate everything is working
134+
```
135+
docker run -p 8000:8000 captainfedora/concert:v1
136+
```

bin/setup.sh

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
echo "****************************************"
3+
echo " Setting up Capstone Environment"
4+
echo "****************************************"
5+
6+
echo "Installing Python 3.9 and Virtual Environment"
7+
sudo apt-get update
8+
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y python3.9 python3.9-venv
9+
10+
echo "Checking the Python version..."
11+
python3.9 --version
12+
13+
echo "Creating a Python virtual environment"
14+
python3.9 -m venv ~/venv
15+
16+
echo "Configuring the developer environment..."
17+
echo "# DevOps Capstone Project additions" >> ~/.bashrc
18+
echo "export GITHUB_ACCOUNT=$GITHUB_ACCOUNT" >> ~/.bashrc
19+
echo 'export PS1="\[\e]0;\u:\W\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ "' >> ~/.bashrc
20+
echo "source ~/venv/bin/activate" >> ~/.bashrc
21+
22+
echo "Installing Python dependencies..."
23+
source ~/venv/bin/activate && python3.9 -m pip install --upgrade pip wheel
24+
source ~/venv/bin/activate && python3.9 -m pip install -r requirements.txt
25+
26+
echo "Starting the Postgres Docker container..."
27+
make db
28+
29+
echo "Checking the Postgres Docker container..."
30+
docker ps
31+
32+
echo "****************************************"
33+
echo " Capstone Environment Setup Complete"
34+
echo "****************************************"
35+
echo ""
36+
echo "Use 'exit' to close this terminal and open a new one to initialize the environment"
37+
echo ""

concert/__init__.py

Whitespace-only changes.

concert/admin.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
4+
from .models import Concert

concert/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ConcertConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "concert"

0 commit comments

Comments
 (0)