My Borrowed Books
Relevant source files
The following files were used as context for generating this wiki page:
The "My Borrowed Books" feature provides authenticated library users with a personalized view of all book copies they currently have on loan. This page lists each borrowed book, its due date, and visually indicates if a book is overdue. It is a user-specific feature that requires a user to be logged in to access.
This functionality is implemented using a combination of a Django class-based view, a database model query, and a template for rendering the final HTML page. Access is restricted to authenticated users, and the list is paginated for better readability if a user has many books on loan.
Architecture and Data Flow
The process begins when a logged-in user navigates to the "My borrowed" link. The request is handled by Django's URL dispatcher, which routes it to the appropriate view. The view then queries the database for all book instances loaned to that specific user and renders a template with this data.
The following sequence diagram illustrates the complete request-response cycle for viewing the "My Borrowed Books" page.
URL Routing
The application's URL configuration maps a specific path to the view that handles the logic for this feature. The link to this page is available in the sidebar for authenticated users.
- URL Path:
/catalog/mybooks/ - URL Name:
my-borrowed - View:
LoanedBooksByUserListView
This is defined in the catalog application's urls.py file.
Sources: catalog/urls.py:15, catalog/templates/base_generic.html:35
View Implementation
The core logic is encapsulated in the LoanedBooksByUserListView class-based view. This view is responsible for access control, data retrieval, and template rendering.
Sources: catalog/views.py:91-103
Class Definition
The view inherits from two key Django mixins:
LoginRequiredMixin: Ensures that only authenticated users can access this page. Unauthenticated requests are redirected to the login page.generic.ListView: A generic Django view that simplifies the process of displaying a list of objects.
Class Attributes
| Attribute | Value | Description |
|---|---|---|
model | BookInstance | The Django model that this view will display a list of. |
template_name | catalog/bookinstance_list_borrowed_user.html | The path to the template file used to render the page. |
paginate_by | 10 | The number of items to display per page. |
Sources: catalog/views.py:93-95, catalog/tests/test_views.py:169-176
Data Querying
The get_queryset method is overridden to customize the database query. It filters the BookInstance objects to retrieve only those that meet specific criteria.
The following flowchart shows the logic within the get_queryset method.
The implementation is as follows:
# catalog/views.py:97-103
def get_queryset(self):
return (
BookInstance.objects.filter(borrower=self.request.user)
.filter(status__exact='o')
.order_by('due_back')
)
Data Model
The "My Borrowed Books" feature relies primarily on the BookInstance model, which is related to the Book and User models.
The entity-relationship diagram below shows the connection between these models. A BookInstance has a one-to-many relationship with Book and a one-to-many relationship with the User model (as the borrower).
Sources: catalog/models.py:60-62, 113-151
BookInstance Model Fields
The following fields from the BookInstance model are crucial for this feature:
| Field | Type | Description |
|---|---|---|
borrower | ForeignKey to settings.AUTH_USER_MODEL | The user who has borrowed the book copy. This is the key field for filtering. |
status | CharField with choices | The availability of the book. For this feature, it is filtered to 'o' (On loan). |
due_back | DateField | The date when the book is expected to be returned. Used for display and sorting. |
is_overdue | property (boolean) | A calculated property that returns True if the due_back date is in the past. |
Sources: catalog/models.py:122-130, 139-147
Template and Rendering
The bookinstance_list_borrowed_user.html template is responsible for the presentation of the borrowed books list.
Key features of the template:
- It extends a base template (
base_generic.html). - It iterates through the
bookinstance_listcontext variable provided by the view. - For each book instance, it displays the book's title, which is a link to the book's detail page, and its due date.
- It conditionally applies a
text-dangerclass to the list item if theis_overdueproperty of the book instance is true, highlighting overdue books. - If the
bookinstance_listis empty, it displays a message: "There are no books borrowed."
<!-- catalog/templates/catalog/bookinstance_list_borrowed_user.html:7-11 -->
{% 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 }})
</li>
{% endfor %}
Sources: catalog/templates/catalog/bookinstance_list_borrowed_user.html:1-18
Testing
The feature's functionality is validated through a series of unit tests in LoanedBookInstancesByUserListViewTest. These tests ensure the view behaves as expected under various conditions.
Key test cases include:
test_redirect_if_not_logged_in: Verifies that an unauthenticated user is redirected to the login page.test_logged_in_uses_correct_template: Confirms that the correct template is rendered for a logged-in user.test_only_borrowed_books_in_list: Ensures the list contains only books borrowed by the current user with a status of 'On loan'.test_pages_paginated_to_ten: Checks that pagination limits the number of items per page to 10.test_pages_ordered_by_due_date: Validates that the books are sorted by theirdue_backdate.
Sources: catalog/tests/test_views.py:72-202