Commit 08ab2928 authored by justin's avatar justin

Migrated and populated all models, fixed ISBN validator for Books

parent d0c4552a
from django.contrib import admin from django.contrib import admin
from models import Author, Books from .models import Author, Books
class AuthorAdmin(admin.ModelAdmin): class AuthorAdmin(admin.ModelAdmin):
model = Author model = Author
list_display = ( list_display = (
"first_name",
"last_name", "last_name",
"first_name",
"age", "age",
"nationality", "nationality",
"bio", "shortened_bio",
) )
search_fields = ( search_fields = (
"first_name", "first_name",
...@@ -17,6 +17,11 @@ class AuthorAdmin(admin.ModelAdmin): ...@@ -17,6 +17,11 @@ class AuthorAdmin(admin.ModelAdmin):
) )
list_filter = ("nationality",) list_filter = ("nationality",)
def shortened_bio(self, obj):
return obj.bio[:50] + "..."
shortened_bio.short_description = "Biography"
class BooksAdmin(admin.ModelAdmin): class BooksAdmin(admin.ModelAdmin):
model = Books model = Books
......
# Generated by Django 4.1.7 on 2023-03-27 06:15
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=255)),
('last_name', models.CharField(max_length=255)),
('age', models.PositiveIntegerField()),
('nationality', models.CharField(max_length=255)),
('bio', models.TextField(max_length=700)),
],
),
migrations.CreateModel(
name='Books',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('publisher', models.CharField(default='Unknown', max_length=255)),
('year_published', models.IntegerField(default=0)),
('isbn', models.PositiveBigIntegerField(validators=[django.core.validators.MaxLengthValidator(13)])),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='bookshelf.author')),
],
),
]
# Generated by Django 4.1.7 on 2023-03-27 06:55
import bookshelf.models
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookshelf', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='books',
name='isbn',
field=models.PositiveBigIntegerField(validators=[bookshelf.models.validate_isbn]),
),
]
from django.db import models from django.db import models
from django.core.validators import MaxLengthValidator from django.core.exceptions import ValidationError
def validate_isbn(value):
if len(str(value)) != 13:
raise ValidationError("ISBN must be exactly 13 digits long.")
class Author(models.Model): class Author(models.Model):
...@@ -21,7 +26,7 @@ class Books(models.Model): ...@@ -21,7 +26,7 @@ class Books(models.Model):
default="Unknown", default="Unknown",
) )
year_published = models.IntegerField(default=0) year_published = models.IntegerField(default=0)
isbn = models.PositiveBigIntegerField(validators=[MaxLengthValidator(13)]) isbn = models.PositiveBigIntegerField(validators=[validate_isbn])
def __str__(self): def __str__(self):
return self.title return self.title
...@@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent ...@@ -20,7 +20,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-#$_y3fdp-jxw7@e54b&d)b1966h7g!@$#8#mk$@lqnq0_or((v' SECRET_KEY = "django-insecure-#$_y3fdp-jxw7@e54b&d)b1966h7g!@$#8#mk$@lqnq0_or((v"
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = True
...@@ -31,52 +31,53 @@ ALLOWED_HOSTS = [] ...@@ -31,52 +31,53 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'django.contrib.admin', "django.contrib.admin",
'django.contrib.auth', "django.contrib.auth",
'django.contrib.contenttypes', "django.contrib.contenttypes",
'django.contrib.sessions', "django.contrib.sessions",
'django.contrib.messages', "django.contrib.messages",
'django.contrib.staticfiles', "django.contrib.staticfiles",
"bookshelf",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', "django.middleware.security.SecurityMiddleware",
'django.contrib.sessions.middleware.SessionMiddleware', "django.contrib.sessions.middleware.SessionMiddleware",
'django.middleware.common.CommonMiddleware', "django.middleware.common.CommonMiddleware",
'django.middleware.csrf.CsrfViewMiddleware', "django.middleware.csrf.CsrfViewMiddleware",
'django.contrib.auth.middleware.AuthenticationMiddleware', "django.contrib.auth.middleware.AuthenticationMiddleware",
'django.contrib.messages.middleware.MessageMiddleware', "django.contrib.messages.middleware.MessageMiddleware",
'django.middleware.clickjacking.XFrameOptionsMiddleware', "django.middleware.clickjacking.XFrameOptionsMiddleware",
] ]
ROOT_URLCONF = 'justinreyes_reading.urls' ROOT_URLCONF = "justinreyes_reading.urls"
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', "BACKEND": "django.template.backends.django.DjangoTemplates",
'DIRS': [], "DIRS": [],
'APP_DIRS': True, "APP_DIRS": True,
'OPTIONS': { "OPTIONS": {
'context_processors': [ "context_processors": [
'django.template.context_processors.debug', "django.template.context_processors.debug",
'django.template.context_processors.request', "django.template.context_processors.request",
'django.contrib.auth.context_processors.auth', "django.contrib.auth.context_processors.auth",
'django.contrib.messages.context_processors.messages', "django.contrib.messages.context_processors.messages",
], ],
}, },
}, },
] ]
WSGI_APPLICATION = 'justinreyes_reading.wsgi.application' WSGI_APPLICATION = "justinreyes_reading.wsgi.application"
# Database # Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases # https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = { DATABASES = {
'default': { "default": {
'ENGINE': 'django.db.backends.sqlite3', "ENGINE": "django.db.backends.sqlite3",
'NAME': BASE_DIR / 'db.sqlite3', "NAME": BASE_DIR / "db.sqlite3",
} }
} }
...@@ -86,16 +87,16 @@ DATABASES = { ...@@ -86,16 +87,16 @@ DATABASES = {
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [
{ {
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
}, },
{ {
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
}, },
] ]
...@@ -103,9 +104,9 @@ AUTH_PASSWORD_VALIDATORS = [ ...@@ -103,9 +104,9 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/ # https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = "en-us"
TIME_ZONE = 'UTC' TIME_ZONE = "Asia/Manila"
USE_I18N = True USE_I18N = True
...@@ -115,9 +116,9 @@ USE_TZ = True ...@@ -115,9 +116,9 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/ # https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/' STATIC_URL = "static/"
# Default primary key field type # Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment