add reverse book-bookshelf relationship + nav

adam
aundus 2021-02-16 19:04:53 -05:00
parent 0275e13a96
commit afe81a2e91
8 changed files with 136 additions and 10 deletions

View File

@ -13,3 +13,7 @@
background: green;
margin-right: 20%;
}
.nav-item.left {
float: left;
}

View File

@ -2,11 +2,10 @@
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h2>All Bookshelves</h2>
<h1>All Bookshelves</h1>
<strong>Number of Bookshelves:</strong> <p>{{ total }}</p>
<h1>Book List</h1>
{% if bookshelves %}
<ul>
{% for bookshelf in bookshelves %}

View File

@ -0,0 +1,26 @@
{% extends "generic_template.html" %}
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h1>{{bookName}}</h1>
<h2>Bookshelves this book belongs to:</h2>
<strong>Number of bookshelves it belongs to:</strong> <p>{{ total }}</p>
{% if bookshelves %}
<ul>
{% for bookshelf in bookshelves %}
<li>
<a href="/bookshelves/{{ bookshelf.id }}">{{ bookshelf.bookshelf }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>This book is not in any bookshelves.</p>
{% endif %}
{% load static %}
<script src="{% static 'booklist.js' %}"></script>
<link rel="stylesheet" href="{% static 'style.css' %}"></script>
{% endblock %}

View File

@ -1,16 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
{% block title %}<title>Local Library</title>{% endblock %}
{% block title %}<title>Gutenberg - Bookshelf Managment</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Add additional CSS in static file -->
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
<div class="container-fluid">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Bookshelf Management</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="/"> <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/books">Lookup Books</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/bookshelves">Bookshelves</a>
</li>
<li class="nav-item left">
<a class="nav-link" href="/login">Login</a>
</li>
</ul>
</div>
</nav>
<div class="row">
<div class="col-sm-2">
</div>

View File

@ -5,7 +5,7 @@
<h1>Search Books</h1>
<strong>Number of Books found:</strong> <p>{{ total }}</p>
<form action="/bookshelves/{{bookshelfId}}/search" method="post">
<form action="/books" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
@ -16,7 +16,7 @@
<ul>
{% for book in books %}
<li class="bookshelf-entry" id="book-{{ book.id }}">
<a href="https://www.gutenberg.org/ebooks/{{ book.id }}">{{ book.title }}</a>
<a href="/books/{{ book.id }}">{{ book.title }}</a>
</li>
{% endfor %}
</ul>

View File

@ -0,0 +1,31 @@
{% extends "generic_template.html" %}
{% block content %}
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<h1>Search Books</h1>
<strong>Number of Books found:</strong> <p>{{ total }}</p>
<form action="/bookshelves/{{bookshelfId}}/search" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Search">
</form>
<h1>Book List</h1>
{% if books %}
<ul>
{% for book in books %}
<li class="bookshelf-entry" id="book-{{ book.id }}">
<a href="https://www.gutenberg.org/ebooks/{{ book.id }}">{{ book.title }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>There are no books found.</p>
{% endif %}
{% load static %}
<!-- <script src="{% static 'booklist.js' %}"></script> -->
<link rel="stylesheet" href="{% static 'style.css' %}"></script>
{% endblock %}

View File

@ -52,13 +52,55 @@ def booksInBookshelf(request, bookshelfId):
return render(request, 'bookshelf.html', context=context)
def searchBooks(request, bookshelfId):
def bookshelvesOfBook(request, bookId):
bookName = Book.objects.get(id=bookId).title
idList = BookshelfToBook.objects.filter(fk_books=bookId).values_list('fk_bookshelves', flat=True)
bookshelves = Bookshelf.objects.filter(id__in=idList)
context = {
'bookshelves': bookshelves,
'total': len(bookshelves),
'bookName': bookName,
'bookId': bookId
}
return render(request, 'bookshelvesOfBook.html', context=context)
def searchBooksToAdd(request, bookshelfId):
context = {
'books': None,
'total': 0,
'bookshelfId': bookshelfId
}
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = (request.POST)
# print(form)
# check whether it's valid:
if form.get('searchTerm') != None:
foundBooks = getBooksMatchingTitle(form.get('searchTerm'))
context['books'] = foundBooks
context['total'] = len(foundBooks)
context['form'] = BookSearchForm()
# redirect to a new URL:
return render(request, 'search.html', context=context)
# if a GET (or any other method) we'll create a blank form
else:
form = BookSearchForm()
context['form'] = form
return render(request, 'searchToAdd.html', context=context)
def searchBooks(request):
context = {
'books': None,
'total': 0,
}
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = (request.POST)
@ -82,10 +124,10 @@ def searchBooks(request, bookshelfId):
def getBooksMatchingTitle(term):
return Book.objects.filter(title__contains=term)
return Book.objects.filter(title__contains=term).order_by('title')
def bookshelfList(request):
bookshelves = Bookshelf.objects.all()
bookshelves = Bookshelf.objects.all().order_by('bookshelf')
context = {
'bookshelves': bookshelves,

View File

@ -23,7 +23,9 @@ from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('bookshelves', views.bookshelfList, name='detail'),
path('bookshelves/<int:bookshelfId>/search', views.searchBooks, name='detail'),
path('books', views.searchBooks, name='detail'),
path('books/<int:bookId>', views.bookshelvesOfBook, name='detail'),
path('bookshelves/<int:bookshelfId>/search', views.searchBooksToAdd, name='detail'),
path('bookshelves/<int:bookshelfId>', views.booksInBookshelf, name='detail')
]