Viewing All Borrowed Books (Librarian)
Relevant source files
The following files were used as context for generating this wiki page:
This feature provides a specialized view for library staff to see a comprehensive list of all book copies that are currently on loan across all users. Access to this page is restricted to users with the specific permission to manage book returns, ensuring that only authorized personnel can view this information. The list is sorted by the due date, allowing librarians to easily identify overdue books.
From this view, librarians can also access the functionality to renew a borrowed book. This is a key part of the library's staff-facing management tools, distinct from the My Borrowed page which only shows books borrowed by the currently logged-in user.
Feature Architecture
The "All Borrowed Books" feature is implemented using Django's class-based views, a specific URL route, a permission check, and a dedicated template for rendering the list.
The following diagram illustrates the overall flow from a user request to the final rendered page.
This diagram shows the request lifecycle for viewing all borrowed books.
Sources: catalog/urls.py:14, catalog/views.py:108-118, catalog/templates/base_generic.html:51
Access and Navigation
Authorized staff members can access this page via a link in the sidebar menu. The link is only rendered if the logged-in user is marked as staff (user.is_staff).
{% if user.is_staff %}
<hr>
<ul class="sidebar-nav">
<li>Staff</li>
<li><a href="{% url 'all-borrowed' %}">All borrowed</a></li>
...
</ul>
{% endif %}
Sources: catalog/templates/base_generic.html:49-57
URL Routing
The feature is accessible through a dedicated URL. The catalog/urls.py file maps the path to the corresponding view class.
| Property | Value | Source File |
|---|---|---|
| URL Path | borrowed/ | catalog/urls.py:14 |
| View Class | views.LoanedBooksAllListView | catalog/urls.py:14 |
| URL Name | all-borrowed | catalog/urls.py:14 |
The specific URL pattern is defined as:
# catalog/urls.py:14
path(r'borrowed/', views.LoanedBooksAllListView.as_view(), name='all-borrowed'),
Sources: catalog/urls.py:14
View Implementation
The core logic is encapsulated in the LoanedBooksAllListView class, a generic ListView provided by Django.
Sources: catalog/views.py:108-118
Class Definition
The view class uses mixins to provide permission checking and inherits from generic.ListView to handle the object listing.
# catalog/views.py:108-118
class LoanedBooksAllListView(PermissionRequiredMixin, generic.ListView):
"""Generic class-based view listing all books on loan. Only visible to users with can_mark_returned permission."""
model = BookInstance
permission_required = 'catalog.can_mark_returned'
template_name = 'catalog/bookinstance_list_borrowed_all.html'
paginate_by = 10
def get_queryset(self):
return BookInstance.objects.filter(status__exact='o').order_by('due_back')
Key Properties
| Attribute | Value | Description |
|---|---|---|
model | BookInstance | Specifies that the view will list objects from the BookInstance model. |
permission_required | 'catalog.can_mark_returned' | Restricts access to users who have this specific permission. This is handled by the PermissionRequiredMixin. |
template_name | 'catalog/bookinstance_list_borrowed_all.html' | The template file used to render the list. |
paginate_by | 10 | Enables pagination and displays 10 items per page. |
Sources: catalog/views.py:110-114
Data Retrieval
The get_queryset method is overridden to customize the data retrieval. It fetches all BookInstance objects where the status is 'o' (On loan) and orders them by the due_back date in ascending order.
Sources: catalog/views.py:116-118
Data Model and Permissions
The functionality relies on the BookInstance model and a custom permission defined within it.
BookInstance Model
The BookInstance model represents a specific physical copy of a book that can be borrowed.
This ER diagram shows the relationship between BookInstance, Book, and the User model.
Sources: catalog/models.py:105-144
Key fields for this feature are:
| Field | Type | Description |
|---|---|---|
status | CharField | The availability of the book copy. It uses a set of choices, where 'o' signifies 'On loan'. |
due_back | DateField | The date when the book is expected to be returned. |
borrower | ForeignKey | A link to the User model, indicating who has borrowed the book. |
is_overdue | property | A computed property that returns True if the book's due_back date is in the past. |
Sources: catalog/models.py:116-137
Permissions
Access to the view is controlled by the can_mark_returned permission. This permission is defined in the Meta class of the BookInstance model.
# catalog/models.py:139-140
class Meta:
...
permissions = (("can_mark_returned", "Set book as returned"),)
This custom permission is essential for securing the "All Borrowed Books" view and the associated "Renew" functionality.
Sources: catalog/models.py:138-141
User Interface
The user interface is rendered by the bookinstance_list_borrowed_all.html template.
Template Logic
The template iterates through the bookinstance_list provided by the view. For each borrowed book, it displays key information.
{% for bookinst in bookinstance_list %}
<li class="{% if bookinst.is_overdue %}text-danger{% endif %}">
<a href="{% url 'book-detail' bookinst.book.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 %}
</li>
{% endfor %}
Sources: catalog/templates/catalog/bookinstance_list_borrowed_all.html:8-12
Key display logic includes:
- Overdue Status: If a book is overdue (
bookinst.is_overdueis true), the list item is assigned atext-dangerclass for visual highlighting. - Book Details: It displays the book's title, which links to the book's detail page, and the due date.
- Borrower Information: If the user is staff, the borrower's username is displayed.
- Renew Link: A "Renew" link is shown only if the user has the
catalog.can_mark_returnedpermission. This link directs the librarian to the renewal page for that specific book instance.
Request-Response Sequence
This sequence diagram details the interactions between the user, server, and database when a librarian views the page.