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

feat: book loans overview

parent 8e91d0ef
......@@ -26,6 +26,7 @@ urlpatterns = [
path("dashboard/", include("dashboard.urls")),
path("books/", include("book.urls")),
path("users/", include("users.urls")),
path("book-loans/", include("loans.urls")),
# api
path("api/v1/", include("api.urls"), name="API_V1"),
# 3rd party
......
from django import forms
from loans.models import BookLoan
class BookLoanForm(forms.ModelForm):
class Meta:
model = BookLoan
fields = "__all__"
widgets = {
"book": forms.Select(
attrs={
"class": "form-control",
}
),
"member": forms.Select(
attrs={
"class": "form-control",
}
),
"librarian": forms.Select(
attrs={
"class": "form-control",
}
),
"loan_date": forms.DateTimeInput(
attrs={
"type": "datetime-local",
"class": "form-control",
}
),
"due_date": forms.DateTimeInput(
attrs={
"type": "datetime-local",
"class": "form-control",
}
),
"return_date": forms.DateTimeInput(
attrs={
"type": "datetime-local",
"class": "form-control",
}
),
"notes": forms.Textarea(
attrs={
"placeholder": "Note",
"class": "form-control",
}
),
}
<table class="table table-hover">
<thead>
<tr class="table-primary">
<th scope="col">Book</th>
<th scope="col">Member</th>
<th scope="col">Loan Date</th>
<th scope="col">Due Date</th>
<th scope="col">Return Date</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% if object_list %} {% for loan in object_list %}
<tr>
<td>{{ loan.book.title }}</td>
<td>{{ loan.member.name }}</td>
<td>{{ loan.loan_date }}</td>
<td>{{ loan.due_date }}</td>
<td>{{ loan.return_date }}</td>
<td class="d-flex gap-2 py-4">
<a class="btn btn-success" href="/book-loans/{{ loan.id }}/">
<i class="bi bi-pencil-square"></i>
</a>
<a
class="btn btn-danger"
href="/book-loans/{{ loan.id }}/delete/"
>
<i class="bi bi-trash3-fill"></i>
</a>
</td>
</tr>
{% endfor %} {% else %}
<tr class="w-100">
<td></td>
<td></td>
<td></td>
<td>
<p>Data Empty</p>
</td>
<td></td>
<td></td>
</tr>
{% endif %}
</tbody>
</table>
{% include "pagination.html" %}
{% extends "layout.html" %} {% block dashboard %}
<div class="w-100 p-4">
<div class="d-flex gap-2 mb-4">
{% include "order_form.html" %}
<a type="button" class="btn btn-primary" href="/book-loans/add/">
<i class="bi bi-plus-circle"></i> Add Book Loan
</a>
{% include "search_form.html" %}
</div>
{% include "book_loan_table_data.html" %}
</div>
{% endblock dashboard %}
from django.urls import path
from .views import (
BookLoanListView,
BookLoanCreateView,
BookLoanUpdateView,
BookLoanDeleteView,
)
urlpatterns = [
path("", BookLoanListView.as_view(), name="book_loan_lists"),
path("add/", BookLoanCreateView.as_view(), name="add_book_loan"),
path("<int:pk>/", BookLoanUpdateView.as_view(), name="update_book_loan"),
path("<int:pk>/delete/", BookLoanDeleteView.as_view(), name="delete_book_loan"),
]
from django.shortcuts import render
from django.db.models import Q
from django.views import generic
from .models import BookLoan
from .forms import BookLoanForm
# Create your views here.
class BookLoanListView(generic.ListView):
model = BookLoan
template_name = "loans.html"
paginate_by = 5
def get_queryset(self):
queryset = super().get_queryset()
keyword = self.request.GET.get("q")
order = self.request.GET.get("o")
if keyword:
queryset = queryset.filter(
Q(book__title__icontains=keyword) | Q(member__name__icontains=keyword)
).order_by("-loan_date")
if order:
if order == "new":
queryset = queryset.order_by("-loan_date")
elif order == "old":
queryset = queryset.order_by("loan_date")
return queryset.order_by("-loan_date")
class BookLoanCreateView(generic.edit.CreateView):
model = BookLoan
form_class = BookLoanForm
success_url = "/dashboard/book-loans/"
template_name = "form/create_form.html"
class BookLoanUpdateView(generic.edit.UpdateView):
model = BookLoan
form_class = BookLoanForm
success_url = "/dashboard/book-loans"
template_name = "form/update_form.html"
class BookLoanDeleteView(generic.edit.DeleteView):
model = BookLoan
success_url = "/dashboard/book-loans"
template_name = "form/delete_form.html"
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