In this assignment you will create the software required for a small library to manage its inventory. You have been provided the general application setup, tests, and templates.
Almost all of the material for this assignment should be familiar,
but there is 1 new component. Similar to other assignments
you will develop the required urls, views, and models.
New to this assignment, you will use Django's messages
framework to provide 1-time messages to your users.
To make sure your solution is correct, you can run the tests with the following command:
python3 manage.py test
documentation: https://docs.djangoproject.com/en/2.2/ref/contrib/messages/
The basic idea of the messages
framework is to provide
a tool for developers to render 1-time messages inside
a template. It is commonly used for alerts to your user
after redirecting. We will generally constrain our use
of the framework to the following lines:
# import messages
from django.contrib import messages
# add a success message
messages.success(request, "You did it! Hooray!")
# add an error message
messages.error(request, "Oh no! Something bad happened!")
You will use this code inside your views to add messages. The provided templates are already set up to use the messages that your views will add.
from django.contrib import messages
def example_view(request):
messages.error(request, "Fooey")
return redirect("somewhere_else")
Book
should have the following fields:
title
- the title of the bookauthor
- the name of the author of the bookpublished
- the date the book was publishedgenre
- the genre of the bookin_stock
- whether or not the book is currently in stockdescription
- a description of the book
Transaction
should have the following fields:
datetime
- the datetimewhen the transaction was createdaction
- either"CHECKIN"
for a return or"CHECKOUT"
for a borrowbook
- the book being borrowed or returned
""
should route tohome
book/<id>/borrow
should route toborrow_book
book/<id>/return
should route toreturn_book
home
renders book_list.html
with all the books in the database
using the context key "books"
.
borrow_book
has two cases:
- If the book is in stock, the book is updated for
in_stock
to beFalse
and a success message ofBorrowed <title> by <author>
is added. Also, a"CHECKOUT"
Transaction
should be created. - if the book is not in stock, the an error message of
<title> by <author> is unavailable
is added
either way, the user is redirected to home
return_book
has two cases:
- if the book is not in stock, the book is updated for
in_stock
to beTrue
and a success message ofReturned <title> by <author>
is added. Also, a"CHECKIN"
Transaction
should be created. - if the book is in stock, the an error message of
<title> by <author> is already here
is added
either way, the user is redirected to home