Skip to content

Commit

Permalink
Migrate to Django v5.0.2 (#137)
Browse files Browse the repository at this point in the history
* Add migration

* Update Python to 5.0.2

* Add generated template info across

* Ran format on settings.py

* Fix up logout form

* Fix typo in create Bookinstance

* Fix up name of bookinstances to copies

* Improve layout of layout form

* move import libraries to right place

* Update bootstrap used

* Add unique constraints

* Genre function ordering to match text

* Improve the All Bookinstances rendering  - not in doc so can do

* Update requirements to latest

* Update urls.py - fix up 4.2 URL to 5.0

---------

Co-authored-by: Brian Thomas Smith <brian@smith.berlin>
  • Loading branch information
hamishwillee and bsmth authored Apr 8, 2024
1 parent b7fa69b commit 45df021
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 5.0.2 on 2024-02-23 01:19

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


class Migration(migrations.Migration):

dependencies = [
('catalog', '0025_auto_20220222_0623'),
]

operations = [
migrations.AlterField(
model_name='book',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.RESTRICT, to='catalog.author'),
),
migrations.AlterField(
model_name='genre',
name='name',
field=models.CharField(help_text='Enter a book genre (e.g. Science Fiction, French Poetry etc.)', max_length=200, unique=True),
),
migrations.AlterField(
model_name='language',
name='name',
field=models.CharField(help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)", max_length=200, unique=True),
),
]
27 changes: 22 additions & 5 deletions catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Create your models here.

from django.urls import reverse # To generate URLS by reversing URL patterns

from django.db.models import UniqueConstraint
from django.db.models.functions import Lower

class Genre(models.Model):
"""Model representing a book genre (e.g. Science Fiction, Non Fiction)."""
Expand All @@ -13,14 +14,22 @@ class Genre(models.Model):
help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)"
)

def get_absolute_url(self):
"""Returns the url to access a particular genre instance."""
return reverse('genre-detail', args=[str(self.id)])

def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name

def get_absolute_url(self):
"""Returns the url to access a particular genre instance."""
return reverse('genre-detail', args=[str(self.id)])

class Meta:
constraints = [
UniqueConstraint(
Lower('name'),
name='genre_name_case_insensitive_unique',
violation_error_message = "Genre already exists (case insensitive match)"
),
]

class Language(models.Model):
"""Model representing a Language (e.g. English, French, Japanese, etc.)"""
Expand All @@ -36,6 +45,14 @@ def __str__(self):
"""String for representing the Model object (in Admin site etc.)"""
return self.name

class Meta:
constraints = [
UniqueConstraint(
Lower('name'),
name='language_name_case_insensitive_unique',
violation_error_message = "Language already exists (case insensitive match)"
),
]

class Book(models.Model):
"""Model representing a book (but not a specific copy of a book)."""
Expand Down
8 changes: 8 additions & 0 deletions catalog/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@
list-style: none;
}

#logout-form {
display: inline;
}
#logout-form button {
padding: 0;
margin: 0;
}

14 changes: 9 additions & 5 deletions catalog/templates/base_generic.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
{% block title %}<title>Local Library</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
Expand All @@ -31,7 +30,12 @@
{% if user.is_authenticated %}
<li>User: {{ user.get_username }}</li>
<li><a href="{% url 'my-borrowed' %}">My borrowed</a></li>
<li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
<li>
<form id="logout-form" method="post" action="{% url 'admin:logout' %}">
{% csrf_token %}
<button type="submit" class="btn btn-link">Logout</button>
</form>
</li>
{% else %}
<li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
{% endif %}
Expand All @@ -55,10 +59,10 @@
<li><a href="{% url 'book-create' %}">Create book</a></li>
{% endif %}
{% if perms.catalog.add_bookinstance %}
<li><a href="{% url 'bookinstance-create' %}">create BookInstance</a></li>
<li><a href="{% url 'bookinstance-create' %}">Create copy of book</a></li>
{% endif %}
</ul>
{% endif %}
{% endif %}

{% endblock %}
</div>
Expand Down
6 changes: 5 additions & 1 deletion catalog/templates/catalog/bookinstance_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ <h1>Book Copies in Library</h1>
<ul>
{% for bookinst in bookinstance_list %}
<li class="{% if bookinst.is_overdue %}text-danger{% endif %}">
<a href="{% url 'bookinstance-detail' bookinst.pk %}">{{bookinst.book.title}}</a> ({{ bookinst.due_back }}) {% if user.is_staff %}- {{ bookinst.borrower }}{% endif %} {% if perms.catalog.can_mark_returned %}- <a href="{% url 'renew-book-librarian' bookinst.id %}">Renew</a> {% endif %}
<a href="{% url 'bookinstance-detail' bookinst.pk %}">{{bookinst.book.title}}</a> ({{ bookinst.get_status_display }})
{% if bookinst.status != 'a' %}: {{ bookinst.due_back }} {% endif %}
{% if bookinst.status == 'o' %}
{% if user.is_staff %}- {{ bookinst.borrower }}{% endif %} {% if perms.catalog.can_mark_returned %}- <a href="{% url 'renew-book-librarian' bookinst.id %}">Renew</a> {% endif %}
{% endif %}
</li>
{% empty %}
<li>There are no book copies available.</li>
Expand Down
16 changes: 16 additions & 0 deletions locallibrary/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for locallibrary 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', 'locallibrary.settings')

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

from pathlib import Path
Expand All @@ -17,27 +17,29 @@


# Add support for env variables from file if defined
import os
from dotenv import load_dotenv
import os
env_path = load_dotenv(os.path.join(BASE_DIR, '.env'))
load_dotenv(env_path)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# 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-&psk#na5l=p3q8_a+-$4w1f^lt3lx1c@d*p4x$ymm_rn7pwb87'
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'django-insecure-&psk#na5l=p3q8_a+-$4w1f^lt3lx1c@d*p4x$ymm_rn7pwb87')
SECRET_KEY = os.environ.get(
'DJANGO_SECRET_KEY', 'django-insecure-&psk#na5l=p3q8_a+-$4w1f^lt3lx1c@d*p4x$ymm_rn7pwb87')

# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'
DEBUG = True
# DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'

# Set hosts to allow any app on Railway and the local testing URL
ALLOWED_HOSTS = ['.railway.app','.pythonanywhere.com','127.0.0.1']
ALLOWED_HOSTS = ['.railway.app', '.pythonanywhere.com', '127.0.0.1']

# Set CSRF trusted origins to allow any app on Railway and the local testing URL
CSRF_TRUSTED_ORIGINS = ['https://*.railway.app','https://*.pythonanywhere.com']
CSRF_TRUSTED_ORIGINS = ['https://*.railway.app',
'https://*.pythonanywhere.com']


# Application definition
Expand All @@ -50,7 +52,7 @@
'django.contrib.messages',
'django.contrib.staticfiles',
# Add our new application
'catalog.apps.CatalogConfig', #This object was created for us in /catalog/apps.py
'catalog.apps.CatalogConfig', # This object was created for us in /catalog/apps.py
]

MIDDLEWARE = [
Expand Down Expand Up @@ -86,7 +88,7 @@


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

DATABASES = {
'default': {
Expand All @@ -97,7 +99,7 @@


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

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -116,7 +118,7 @@


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

LANGUAGE_CODE = 'en-us'

Expand All @@ -127,18 +129,14 @@
USE_TZ = True



# Redirect to home URL after login (Default redirects to /accounts/profile/)
LOGIN_REDIRECT_URL = '/'

# Add to test email:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'



# Update database configuration from $DATABASE_URL environment variable (if defined)
import dj_database_url

if 'DATABASE_URL' in os.environ:
DATABASES['default'] = dj_database_url.config(
conn_max_age=500,
Expand All @@ -147,9 +145,9 @@


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/
# https://docs.djangoproject.com/en/5.0/howto/static-files/
# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = BASE_DIR / 'staticfiles' #. os.path.join(BASE_DIR, 'staticfiles')
STATIC_ROOT = BASE_DIR / 'staticfiles'
# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'

Expand All @@ -164,6 +162,6 @@
}

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
6 changes: 3 additions & 3 deletions locallibrary/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
URL configuration for locallibrary project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
Expand Down Expand Up @@ -39,15 +39,15 @@
urlpatterns+= static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


#Add URL maps to redirect the base URL to our application
# Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns += [
path('', RedirectView.as_view(url='/catalog/', permanent=True)),
]



#Add Django site authentication urls (for login, logout, password management)
# Add Django site authentication urls (for login, logout, password management)
urlpatterns += [
path('accounts/', include('django.contrib.auth.urls')),
]
2 changes: 1 addition & 1 deletion locallibrary/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
"""

import os
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Django==4.2.11
dj-database-url==2.0.0
Django==5.0.2
dj-database-url==2.1.0
gunicorn==21.2.0
psycopg2-binary==2.9.6
psycopg2-binary==2.9.9
wheel==0.38.1
whitenoise==6.5.0
whitenoise==6.6.0
python-dotenv==1.0.1

0 comments on commit 45df021

Please sign in to comment.