Skip to content

Commit

Permalink
Initial web DB
Browse files Browse the repository at this point in the history
Update readme
  • Loading branch information
octaflop committed Sep 3, 2017
1 parent 15b56d7 commit 708c7b4
Show file tree
Hide file tree
Showing 17 changed files with 296 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ mkvirtualenv djmusic && pip install -r requirements.txt && add2virtualenv `pwd`/
4. Create a new template and view function: `dynamic.html`.
5. Add a `<form>` to this template using [django forms](https://docs.djangoproject.com/en/1.11/topics/forms/)
6. Don't forget about `csrf`!

## 3. `web_db`

Now we're going to play with Django's ORM and admin: One of the main selling-points of Django vs other Web Libraries.

1. Repeat steps 1-8 in `web_pages`
2. Let's create a few DB models in `models.py`
3. Now we need migrations for them
1. We create the migrations with `./manage.py makemigrations web_db`
2. And migrate the db with `./manage.py migrate`. Note, this will migrate all apps if you haven't done so already.
3. Note, if you're using the repo, that we added a `0002` migration to populate with initial data
4. We make things easier by using `include` for our URLS and adding our own urls.py definitions in the app, instead of the site.
5. We also use [Generic Views](https://docs.djangoproject.com/en/1.10/topics/class-based-views/generic-display/) for listing.
6. And we also add an `admin.py` file for easy management of the DB.
7. We can visit `localhost:8000/admin` and `localhost:8000/db/songs`; etc for more views.
8. Because our urls are named, we can use the `{% url %}` django template tag to directly link to a model's page (or the list page).
1 change: 1 addition & 0 deletions apps/django_music/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
OUR_APPS = [
'web_pages',
'web_app',
'web_db'
]

INSTALLED_APPS = DJANGO_APPS + OUR_APPS
Expand Down
4 changes: 3 additions & 1 deletion apps/django_music/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.conf.urls import url, include
from django.contrib import admin

from web_pages.views import basic_web_page
Expand All @@ -25,4 +25,6 @@
# the web_app app.
url(r'^app/$', basic_web_app),
url(r'^app/song/$', dynamic_web_app),
# the web_db app. In this case, we'll include instead of specifying each view
url(r'^db/', include('web_db.urls', namespace='db')),
]
Empty file added apps/web_db/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions apps/web_db/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.contrib import admin

from web_db import models


class SongAdmin(admin.ModelAdmin):
search_fields = ('title',)


class SongInline(admin.TabularInline):
model = models.Song


class AlbumAdmin(admin.ModelAdmin):
search_fields = ('title', 'artist', 'year',)
inlines = (SongInline,)

admin.site.register(models.Album, AlbumAdmin)
admin.site.register(models.Song, SongAdmin)
5 changes: 5 additions & 0 deletions apps/web_db/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class WebDbConfig(AppConfig):
name = 'web_db'
36 changes: 36 additions & 0 deletions apps/web_db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-03 00:16
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Album',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('year', models.PositiveIntegerField()),
('artist', models.CharField(max_length=255)),
],
),
migrations.CreateModel(
name='Song',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('track', models.PositiveIntegerField(blank=True)),
('duration', models.CharField(blank=True, max_length=15)),
('album', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web_db.Album')),
],
),
]
84 changes: 84 additions & 0 deletions apps/web_db/migrations/0002_auto_20170903_0052.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-03 00:52
from __future__ import unicode_literals
from collections import namedtuple

from django.db import migrations


MUSIC = [
{
'artist': 'Les Gordon',
'title': 'Abyss',
'year': '2016',
'songs': [
{
'track': 1,
'title': 'Abyss',
'duration': '3:13'
},
{
'track': 2,
'title': 'Shiho & Kyoko',
'duration': '2:58'
},
]
},
{
'artist': 'Same Gellaitry',
'title': 'Escapism II',
'year': '2016',
'songs': [
{
'track': 1,
'title': 'The Gateway',
'duration': '3:12'
},
{
'track': 2,
'title': 'Desert Mirage',
'duration': '5:00'
},
{
'track': 3,
'title': 'Jacket Weather',
'duration': None
},
{
'track': 4,
'title': 'Static Sleep',
'duration': ''
}
]
}
]


class Migration(migrations.Migration):

dependencies = [
('web_db', '0001_initial'),
]

def migrate_music(apps, schema_editor):
Album = apps.get_model("web_db", "Album")
Song = apps.get_model("web_db", "Song")
for m in MUSIC:
album = namedtuple('album', m.keys())(*m.values())
new_album = Album.objects.create(
title=album.title,
year=album.year,
artist=album.artist
)
for s in album.songs:
song = namedtuple('song', s.keys())(*s.values())
Song.objects.create(
title=song.title,
duration=song.duration or '',
track=song.track,
album=new_album
)

operations = [
migrations.RunPython(migrate_music, migrations.RunPython.noop),
]
Empty file.
21 changes: 21 additions & 0 deletions apps/web_db/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models


class Album(models.Model):
title = models.CharField(max_length=255)
year = models.PositiveIntegerField()
artist = models.CharField(max_length=255)

def __str__(self):
return "{} by {} ({})".format(self.title, self.artist, self.year)


class Song(models.Model):
title = models.CharField(max_length=255)
track = models.PositiveIntegerField(blank=True)
duration = models.CharField(max_length=15, blank=True)

album = models.ForeignKey(Album)

def __str__(self):
return "{} ({})".format(self.title, self.track)
19 changes: 19 additions & 0 deletions apps/web_db/templates/web_db/album_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Album {{ album.title }}</title>
</head>
<body>
<h3>{{ album.title }} by {{ album.artist }} ({{ album.year }})</h3>
<h4>Songs:</h4>
<ul>
{% for song in album.song_set.all %}
<li>
<a href="{% url 'db:song-detail' song.pk %}">{{ song.track }} — {{ song.title }} {% if song.duration %}({{ song.duration }}){% endif %}</a>
</li>
{% endfor %}
</ul>
<a href="{% url 'db:album-list' %}">← back to album list</a>
</body>
</html>
25 changes: 25 additions & 0 deletions apps/web_db/templates/web_db/album_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Albums</title>
</head>
<body>
<h2>All Albums</h2>
{% for album in albums %}
<h3>
<a href='{% url "db:album-detail" album.pk %}'>
{{ album.title }} by {{ album.artist }} ({{ album.year }})
</a>
</h3>
<h4>Songs:</h4>
<ul>
{% for song in album.song_set.all %}
<li>
{{ song.track }} — {{ song.title }} {% if song.duration %}({{ song.duration }}){% endif %}
</li>
{% endfor %}
</ul>
{% endfor %}
</body>
</html>
12 changes: 12 additions & 0 deletions apps/web_db/templates/web_db/song_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Song {{ song.title }}</title>
</head>
<body>
<h3>{{ song.track }}. {{ song.title }} {% if song.duration %}({{ song.duration }}){% endif %}</h3>
<h4>album: <a href="{% url 'db:album-detail' song.album.pk %}">{{ song.album.title }}</a></h4>
<a href="{% url 'db:song-list' %}">← back to song list</a>
</body>
</html>
18 changes: 18 additions & 0 deletions apps/web_db/templates/web_db/song_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Song {{ song.title }}</title>
</head>
<body>
<h3>All Songs</h3>
<ul>
{% for song in songs %}
<li>
<h3><a href="{% url 'db:song-detail' song.pk %}">{{ song.track }}. {{ song.title }} {% if song.duration %}({{ song.duration }}){% endif %}</a></h3>
<h4>album: <a href="{% url 'db:album-detail' song.album.pk %}">{{ song.album.title }}</a></h4>
</li>
{% endfor %}
</ul>
</body>
</html>
3 changes: 3 additions & 0 deletions apps/web_db/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions apps/web_db/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf.urls import url

from web_db import views

urlpatterns = [
url(r'^albums/$', views.AlbumListView.as_view(), name='album-list'),
url(r'^albums/(?P<pk>\d+)/$', views.AlbumDetailView.as_view(), name='album-detail'),
url(r'^songs/$', views.SongListView.as_view(), name='song-list'),
url(r'^songs/(?P<pk>\d+)/$', views.SongDetailView.as_view(), name='song-detail'),
]
24 changes: 24 additions & 0 deletions apps/web_db/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from web_db.models import Album, Song


class AlbumDetailView(DetailView):
model = Album
context_object_name = 'album'


class AlbumListView(ListView):
model = Album
context_object_name = 'albums'


class SongDetailView(DetailView):
model = Song
context_object_name = 'song'


class SongListView(ListView):
model = Song
context_object_name = 'songs'

0 comments on commit 708c7b4

Please sign in to comment.