window.location.replace("/books/loaned");
}
else{
+ console.log(data['message']);
alert("Unable to issue this book");
}
},
diff --git a/store/templates/store/loaned_books.html b/store/templates/store/loaned_books.html
index d6f2044..c09ced3 100644
--- a/store/templates/store/loaned_books.html
+++ b/store/templates/store/loaned_books.html
@@ -37,7 +37,25 @@
Loaned Books list
{% endblock %}
\ No newline at end of file
diff --git a/store/views.py b/store/views.py
index dc411b9..c132672 100644
--- a/store/views.py
+++ b/store/views.py
@@ -2,8 +2,9 @@
from django.shortcuts import get_object_or_404
from store.models import *
from django.contrib.auth.decorators import login_required
-from django.http import JsonResponse
+from django.http import JsonResponse, Http404
from django.views.decorators.csrf import csrf_exempt
+from django.utils import timezone
# Create your views here.
@@ -12,9 +13,11 @@ def index(request):
def bookDetailView(request, bid):
template_name = 'store/book_detail.html'
+ book = get_object_or_404(Book, id=bid)
+ num_available = BookCopy.objects.filter(book=book, status=True).count()
context = {
- 'book': None, # set this to an instance of the required book
- 'num_available': None, # set this to the number of copies of the book available, or 0 if the book isn't available
+ 'book': book, # set this to an instance of the required book
+ 'num_available': num_available, # set this to the number of copies of the book available, or 0 if the book isn't available
}
# START YOUR CODE HERE
@@ -25,11 +28,16 @@ def bookDetailView(request, bid):
@csrf_exempt
def bookListView(request):
template_name = 'store/book_list.html'
+ get_data = request.GET
+ books = Book.objects.filter(
+ title__icontains = get_data.get('title',''),
+ author__icontains = get_data.get('author',''),
+ genre__icontains = get_data.get('genre',''),
+ )
context = {
- 'books': None, # set this to the list of required books upon filtering using the GET parameters
+ 'books': books, # set this to the list of required books upon filtering using the GET parameters
# (i.e. the book search feature will also be implemented in this view)
}
- get_data = request.GET
# START YOUR CODE HERE
@@ -38,8 +46,9 @@ def bookListView(request):
@login_required
def viewLoanedBooks(request):
template_name = 'store/loaned_books.html'
+ books = BookCopy.objects.filter(borrower=request.user)
context = {
- 'books': None,
+ 'books': books,
}
'''
The above key 'books' in the context dictionary should contain a list of instances of the
@@ -62,7 +71,23 @@ def loanBookView(request):
If yes, then set the message to 'success', otherwise 'failure'
'''
# START YOUR CODE HERE
- book_id = None # get the book id from post data
+ book_id = request.POST['bid'] # get the book id from post data
+
+ try:
+ book = Book.objects.get(id=book_id)
+ except:
+ raise Http404('Book not available')
+ else:
+ books = BookCopy.objects.filter(book=book, status=True)
+
+ if books:
+ books[0].borrower = request.user
+ books[0].borrow_date = timezone.datetime.today().date()
+ books[0].status = False
+ books[0].save()
+ response_data['message'] = 'success'
+ else:
+ response_data['message'] = 'failure'
return JsonResponse(response_data)
@@ -77,6 +102,20 @@ def loanBookView(request):
@csrf_exempt
@login_required
def returnBookView(request):
- pass
+ response_data = {
+ 'message': None,
+ }
+
+ book_copy_id = request.POST['bid']
+ book = get_object_or_404(BookCopy, id=book_copy_id)
+ if book and book.borrower == request.user:
+ book.borrower = None
+ book.borrow_date = None
+ book.status = True
+ book.save()
+ response_data['message'] = 'success'
+ else:
+ response_data['message'] = 'failure'
+ return JsonResponse(response_data)
From 69c62156d846d55ac347638bd6c5d2e1571b90cb Mon Sep 17 00:00:00 2001
From: Sanskar Santosh Totla
Date: Thu, 22 Jul 2021 22:13:26 +0530
Subject: [PATCH 2/4] book ratings added
---
authentication/templates/login.html | 2 +-
authentication/views.py | 1 -
library/settings.py | 2 +-
library/urls.py | 1 -
store/admin.py | 1 +
store/migrations/0004_bookrating.py | 26 +++++++++++++++++
store/models.py | 8 ++++++
store/templates/store/book_detail.html | 40 +++++++++++++++++++++++++-
store/urls.py | 1 +
store/views.py | 35 ++++++++++++++++++++++
10 files changed, 112 insertions(+), 5 deletions(-)
create mode 100644 store/migrations/0004_bookrating.py
diff --git a/authentication/templates/login.html b/authentication/templates/login.html
index f626461..30a685c 100644
--- a/authentication/templates/login.html
+++ b/authentication/templates/login.html
@@ -15,7 +15,7 @@
{% endblock %}
\ No newline at end of file
diff --git a/authentication/templates/register.html b/authentication/templates/register.html
index 6c79dfb..cb5ef03 100644
--- a/authentication/templates/register.html
+++ b/authentication/templates/register.html
@@ -22,5 +22,7 @@