diff --git a/README.md b/README.md index ad7474a9..abc096bc 100644 --- a/README.md +++ b/README.md @@ -10,21 +10,33 @@ This web application creates an online catalog for a small local library, where The main features that have currently been implemented are: -* There are models for books, book copies, genre, language and authors. -* Users can view list and detail information for books and authors. -* Admin users can create and manage models. The admin has been optimised (the basic registration is present in admin.py, but commented out). -* Librarians can renew reserved books. +- There are models for books, book copies, genre, language and authors. +- Users can view list and detail information for books and authors. +- Admin users can create and manage models. The admin has been optimised (the basic registration is present in admin.py, but commented out). +- Librarians can renew reserved books. ![Local Library Model](https://raw.githubusercontent.com/mdn/django-locallibrary-tutorial/master/catalog/static/images/local_library_model_uml.png) - ## Quick Start To get this project up and running locally on your computer: -1. Set up the [Python development environment](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment). - We recommend using a Python virtual environment. - > **Note:** This has been tested against Django 3.10 (and may not work or be "optimal" for other versions). -1. Assuming you have Python setup, run the following commands (if you're on Windows you may use `py` or `py -3` instead of `python` to start Python): + +1. Set up the [Python development environment](https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Server-side/Django/development_environment). + > **Note:** This has been tested against Python 3.10 (and may not work or be "optimal" for other versions). +2. Create and activate a Python virtual environment for the project using [venv](https://docs.python.org/3/library/venv.html), the virtual environment tool built into Python: + + ``` + # Linux/macOS + python3 -m venv django6_env + source django6_env/bin/activate + + # Windows + py -3 -m venv django6_env + django6_env\Scripts\activate + ``` + +3. Assuming your virtual environment is active, run the following commands (if you're on Windows you may use `py` or `py -3` instead of `python` to start Python): + ``` pip3 install -r requirements.txt python3 manage.py makemigrations @@ -34,6 +46,7 @@ To get this project up and running locally on your computer: python3 manage.py createsuperuser # Create a superuser python3 manage.py runserver ``` -1. Open a browser to `http://127.0.0.1:8000/admin/` to open the admin site -1. Create a few test objects of each type. -1. Open tab to `http://127.0.0.1:8000` to see the main site, with your new objects. + +4. Open a browser to `http://127.0.0.1:8000/admin/` to open the admin site +5. Create a few test objects of each type. +6. Open tab to `http://127.0.0.1:8000` to see the main site, with your new objects. diff --git a/catalog/admin.py b/catalog/admin.py index b7d14731..5e0cc70c 100644 --- a/catalog/admin.py +++ b/catalog/admin.py @@ -4,14 +4,9 @@ from .models import Author, Genre, Book, BookInstance, Language -"""Minimal registration of Models. -admin.site.register(Book) -admin.site.register(Author) -admin.site.register(BookInstance) -admin.site.register(Genre) -admin.site.register(Language) -""" - +# admin.site.register(Book) +# admin.site.register(Author) +# admin.site.register(BookInstance) admin.site.register(Genre) admin.site.register(Language) @@ -21,7 +16,6 @@ class BooksInline(admin.TabularInline): model = Book -@admin.register(Author) class AuthorAdmin(admin.ModelAdmin): """Administration object for Author models. Defines: @@ -36,11 +30,15 @@ class AuthorAdmin(admin.ModelAdmin): inlines = [BooksInline] +admin.site.register(Author, AuthorAdmin) + + class BooksInstanceInline(admin.TabularInline): """Defines format of inline book instance insertion (used in BookAdmin)""" model = BookInstance +@admin.register(Book) class BookAdmin(admin.ModelAdmin): """Administration object for Book models. Defines: @@ -51,9 +49,6 @@ class BookAdmin(admin.ModelAdmin): inlines = [BooksInstanceInline] -admin.site.register(Book, BookAdmin) - - @admin.register(BookInstance) class BookInstanceAdmin(admin.ModelAdmin): """Administration object for BookInstance models. diff --git a/catalog/apps.py b/catalog/apps.py index a5993c68..ac42aa1b 100644 --- a/catalog/apps.py +++ b/catalog/apps.py @@ -2,5 +2,4 @@ class CatalogConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' name = 'catalog' diff --git a/catalog/migrations/0028_alter_bookinstance_status.py b/catalog/migrations/0028_alter_bookinstance_status.py new file mode 100644 index 00000000..4d939617 --- /dev/null +++ b/catalog/migrations/0028_alter_bookinstance_status.py @@ -0,0 +1,18 @@ +# Generated by Django 6.1.1 on 2026-09-04 06:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalog', '0027_genre_genre_name_case_insensitive_unique_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='bookinstance', + name='status', + field=models.CharField(blank=True, choices=[('m', 'Maintenance'), ('o', 'On loan'), ('a', 'Available'), ('r', 'Reserved')], default='m', help_text='Book availability', max_length=1), + ), + ] diff --git a/catalog/models.py b/catalog/models.py index c55bb95c..1eda562f 100644 --- a/catalog/models.py +++ b/catalog/models.py @@ -112,18 +112,17 @@ def is_overdue(self): """Determines if the book is overdue based on due date and current date.""" return bool(self.due_back and date.today() > self.due_back) - LOAN_STATUS = ( - ('d', 'Maintenance'), - ('o', 'On loan'), - ('a', 'Available'), - ('r', 'Reserved'), - ) + class LoanStatus(models.TextChoices): + MAINTENANCE = 'm', 'Maintenance' + ON_LOAN = 'o', 'On loan' + AVAILABLE = 'a', 'Available' + RESERVED = 'r', 'Reserved' status = models.CharField( max_length=1, - choices=LOAN_STATUS, + choices=LoanStatus, blank=True, - default='d', + default=LoanStatus.MAINTENANCE, help_text='Book availability') class Meta: diff --git a/catalog/templates/catalog/book_detail.html b/catalog/templates/catalog/book_detail.html index e1f3ed7e..8446f333 100644 --- a/catalog/templates/catalog/book_detail.html +++ b/catalog/templates/catalog/book_detail.html @@ -15,8 +15,8 @@

Copies

{% for copy in book.bookinstance_set.all %}
-

{{ copy.get_status_display }}

- {% if copy.status != 'a' %}

Due to be returned: {{copy.due_back}}

{% endif %} +

{{ copy.get_status_display }}

+ {% if copy.status != copy.LoanStatus.AVAILABLE %}

Due to be returned: {{copy.due_back}}

{% endif %}

Imprint: {{copy.imprint}}

Id: {{copy.id}}

{% empty %} diff --git a/catalog/templates/catalog/bookinstance_detail.html b/catalog/templates/catalog/bookinstance_detail.html index 6e003db2..8859fa24 100644 --- a/catalog/templates/catalog/bookinstance_detail.html +++ b/catalog/templates/catalog/bookinstance_detail.html @@ -7,7 +7,7 @@

BookInstance: {{ bookinstance.book.title }}

Author: {{ bookinstance.book.author }}

Imprint: {{ bookinstance.imprint }}

-

Status: {{ bookinstance.get_status_display }} {% if bookinstance.status != 'a' %} (Due: {{bookinstance.due_back}}){% endif %}

+

Status: {{ bookinstance.get_status_display }} {% if bookinstance.status != bookinstance.LoanStatus.AVAILABLE %} (Due: {{bookinstance.due_back}}){% endif %}