Commit a61ee99f authored by Ilham Maulana's avatar Ilham Maulana 💻

feat: remaining loan time

parent b6a6e122
from django.utils import timezone
from django.db.models import Q
from django.db.models import Q, F
from django.views.generic import ListView, TemplateView
from loans.models import BookLoan
......@@ -67,6 +67,9 @@ class UpcomingLoanView(ListView):
elif order == "old":
queryset = queryset.order_by("created_at")
today = timezone.now()
queryset = queryset.annotate(remaining_loan_time=(F("due_date") - today))
return queryset
......
......@@ -3,6 +3,7 @@
<tr class="table-primary">
<th scope="col">Book</th>
<th scope="col">Member</th>
<th scope="col">Remaining Loan</th>
<th scope="col">Loan Date</th>
<th scope="col">Due Date</th>
<th scope="col">Return Date</th>
......@@ -14,6 +15,11 @@
<tr>
<td>{{ loan.book.title }}</td>
<td>{{ loan.member.user.username }}</td>
{% if loan.remaining_loan_time %}
<td>
{{ loan.remaining_loan_time.days }} days left
</td>
{% endif %}
<td>{{ loan.loan_date }}</td>
<td>{{ loan.due_date }}</td>
<td>{{ loan.return_date }}</td>
......
from django.db.models import F
from django.utils import timezone
from django.db.models import Q
from django.views import generic
from .models import BookLoan
......@@ -25,6 +27,9 @@ class BookLoanListView(generic.ListView):
elif order == "old":
queryset = queryset.order_by("created_at")
today = timezone.now()
queryset = queryset.annotate(remaining_loan_time=(F("due_date") - today))
return queryset.order_by("-created_at")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment