Skip to content

Commit

Permalink
Merge pull request #11 from matagus/example-project
Browse files Browse the repository at this point in the history
Added working example project
  • Loading branch information
matagus authored Feb 14, 2024
2 parents 03444b6 + 4c7d686 commit 0a673f1
Show file tree
Hide file tree
Showing 19 changed files with 445 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ env/
build/
.coverage
coverage.json
db.sqlite3
Empty file.
16 changes: 16 additions & 0 deletions example_project/example_project/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for example_project project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings")

application = get_asgi_application()
126 changes: 126 additions & 0 deletions example_project/example_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""
Django settings for example_project project.
Generated by 'django-admin startproject' using Django 5.0.2.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-4jj$diq88j*0l75y*h+dy)a5k*t!ns^lj)gt#fl5$uwg512t1p"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"pagination",
"music_app",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"pagination.middleware.PaginationMiddleware",
]

ROOT_URLCONF = "example_project.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "example_project.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
12 changes: 12 additions & 0 deletions example_project/example_project/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
URL configuration for example_project project.
"""

from django.contrib import admin
from django.urls import path, include


urlpatterns = [
path("", include("music_app.urls", namespace="music_app")),
path("admin/", admin.site.urls),
]
16 changes: 16 additions & 0 deletions example_project/example_project/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for example_project project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings")

application = get_wsgi_application()
22 changes: 22 additions & 0 deletions example_project/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example_project.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == "__main__":
main()
Empty file.
18 changes: 18 additions & 0 deletions example_project/music_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin

from music_app.models import Album, Artist


@admin.register(Album)
class AlbumAdmin(admin.ModelAdmin):
list_display = ["title", "artist", "release_date", "created_at", "updated_at"]
list_filter = ["artist"]
search_fields = ["title", "artist__name"]
list_per_page = 10


@admin.register(Artist)
class ArtistAdmin(admin.ModelAdmin):
list_display = ["name", "created_at", "updated_at"]
search_fields = ["name"]
list_per_page = 10
6 changes: 6 additions & 0 deletions example_project/music_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MusicAppConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "music_app"
34 changes: 34 additions & 0 deletions example_project/music_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 5.0.1 on 2024-02-07 02:14

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


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Artist",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("name", models.CharField(max_length=100)),
("bio", models.TextField()),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name="Album",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("title", models.CharField(max_length=100)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("artist", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="music_app.artist")),
],
),
]
18 changes: 18 additions & 0 deletions example_project/music_app/migrations/0002_album_release_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.1 on 2024-02-07 02:19

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("music_app", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="album",
name="release_date",
field=models.DateField(blank=True, null=True),
),
]
54 changes: 54 additions & 0 deletions example_project/music_app/migrations/0003_auto_20240214_0058.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 5.0.2 on 2024-02-14 00:58

from django.db import migrations


def generate_thirty_artists(apps, schema_editor):
Artist = apps.get_model("music_app", "Artist")

some_well_known_artist_names = [
"The Beatles",
"The Rolling Stones",
"The Who",
"The Kinks",
"The Animals",
"The Yardbirds",
"The Byrds",
"The Hollies",
"The Mars Volta",
"The Strokes",
"The White Stripes",
"The Black Keys",
"The Raconteurs",
"The Dead Weather",
"The Last Shadow Puppets",
"Nirvana",
"Pearl Jam",
"Soundgarden",
"Alice in Chains",
"Mudhoney",
"Badly Drawn Boy",
"The Libertines",
"Björk",
"Sigur Rós",
"Broadcast",
"The Knife",
"The Cardigans",
"The Hives",
"The Vines",
"The Kills",
]

for name in some_well_known_artist_names:
Artist.objects.create(name=name)


class Migration(migrations.Migration):

dependencies = [
("music_app", "0002_album_release_date"),
]

operations = [
migrations.RunPython(generate_thirty_artists),
]
Empty file.
24 changes: 24 additions & 0 deletions example_project/music_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.db import models


class Artist(models.Model):
name = models.CharField(max_length=100)
bio = models.TextField()

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.name}"


class Album(models.Model):
title = models.CharField(max_length=100)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
release_date = models.DateField(null=True, blank=True)

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return f"{self.title} - {self.artist.name}"
Loading

0 comments on commit 0a673f1

Please sign in to comment.