lms2
This commit is contained in:
parent
f9a1f1c71e
commit
7b5d9db799
BIN
core/__pycache__/forms.cpython-311.pyc
Normal file
BIN
core/__pycache__/forms.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
25
core/forms.py
Normal file
25
core/forms.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from django import forms
|
||||||
|
from .models import Book, Transaction
|
||||||
|
|
||||||
|
class BookForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Book
|
||||||
|
fields = ['title', 'author', 'isbn', 'total_copies']
|
||||||
|
widgets = {
|
||||||
|
'title': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
'author': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
'isbn': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
'total_copies': forms.NumberInput(attrs={'class': 'form-control'}),
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields['total_copies'].initial = 1
|
||||||
|
|
||||||
|
class IssueBookForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
model = Transaction
|
||||||
|
fields = ['borrower_name']
|
||||||
|
widgets = {
|
||||||
|
'borrower_name': forms.TextInput(attrs={'class': 'form-control'}),
|
||||||
|
}
|
||||||
25
core/migrations/0003_transaction.py
Normal file
25
core/migrations/0003_transaction.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Generated by Django 5.2.7 on 2025-10-25 14:19
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('core', '0002_preload_books'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Transaction',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('borrower_name', models.CharField(max_length=200)),
|
||||||
|
('issue_date', models.DateTimeField(auto_now_add=True)),
|
||||||
|
('return_date', models.DateTimeField(blank=True, null=True)),
|
||||||
|
('is_returned', models.BooleanField(default=False)),
|
||||||
|
('book', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.book')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
BIN
core/migrations/__pycache__/0003_transaction.cpython-311.pyc
Normal file
BIN
core/migrations/__pycache__/0003_transaction.cpython-311.pyc
Normal file
Binary file not shown.
@ -9,3 +9,13 @@ class Book(models.Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
class Transaction(models.Model):
|
||||||
|
book = models.ForeignKey(Book, on_delete=models.CASCADE)
|
||||||
|
borrower_name = models.CharField(max_length=200)
|
||||||
|
issue_date = models.DateTimeField(auto_now_add=True)
|
||||||
|
return_date = models.DateTimeField(null=True, blank=True)
|
||||||
|
is_returned = models.BooleanField(default=False)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.book.title} - {self.borrower_name}"
|
||||||
42
core/templates/core/add_book.html
Normal file
42
core/templates/core/add_book.html
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
Add Book - {{ project_name }}
|
||||||
|
{% endblock title %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-md-8">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h3 class="card-title">Add a New Book</h3>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_title" class="form-label">Title</label>
|
||||||
|
{{ form.title }}
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_author" class="form-label">Author</label>
|
||||||
|
{{ form.author }}
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_isbn" class="form-label">ISBN</label>
|
||||||
|
{{ form.isbn }}
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_total_copies" class="form-label">Total Copies</label>
|
||||||
|
{{ form.total_copies }}
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Add Book</button>
|
||||||
|
<a href="{% url 'index' %}" class="btn btn-secondary">Cancel</a>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock content %}
|
||||||
@ -5,9 +5,15 @@
|
|||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="container mt-5">
|
<div class="container mt-5">
|
||||||
<div class="text-center mb-5">
|
<div class="d-flex justify-content-between align-items-center mb-5">
|
||||||
<h1 class="display-4">Public Library Catalog</h1>
|
<div class="text-center flex-grow-1">
|
||||||
<p class="lead">Browse our collection of available books.</p>
|
<h1 class="display-4">Public Library Catalog</h1>
|
||||||
|
<p class="lead">Browse our collection of available books.</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a href="{% url 'add_book' %}" class="btn btn-primary me-2">Add Book</a>
|
||||||
|
<a href="{% url 'transactions' %}" class="btn btn-info">View Transactions</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
@ -18,6 +24,7 @@
|
|||||||
<th scope="col">Author</th>
|
<th scope="col">Author</th>
|
||||||
<th scope="col">ISBN</th>
|
<th scope="col">ISBN</th>
|
||||||
<th scope="col">Available Copies</th>
|
<th scope="col">Available Copies</th>
|
||||||
|
<th scope="col">Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@ -27,10 +34,17 @@
|
|||||||
<td>{{ book.author }}</td>
|
<td>{{ book.author }}</td>
|
||||||
<td>{{ book.isbn }}</td>
|
<td>{{ book.isbn }}</td>
|
||||||
<td>{{ book.available_copies }} / {{ book.total_copies }}</td>
|
<td>{{ book.available_copies }} / {{ book.total_copies }}</td>
|
||||||
|
<td>
|
||||||
|
{% if book.available_copies > 0 %}
|
||||||
|
<a href="{% url 'issue_book' book.id %}" class="btn btn-success btn-sm">Issue</a>
|
||||||
|
{% else %}
|
||||||
|
<button class="btn btn-secondary btn-sm" disabled>Issue</button>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% empty %}
|
{% empty %}
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="4" class="text-center">No books available at the moment.</td>
|
<td colspan="5" class="text-center">No books available at the moment.</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
30
core/templates/core/issue_book.html
Normal file
30
core/templates/core/issue_book.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
Issue Book - {{ project_name }}
|
||||||
|
{% endblock title %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="row justify-content-center">
|
||||||
|
<div class="col-md-8">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h3 class="card-title">Issue Book: {{ book.title }}</h3>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id_borrower_name" class="form-label">Borrower Name</label>
|
||||||
|
{{ form.borrower_name }}
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Issue Book</button>
|
||||||
|
<a href="{% url 'index' %}" class="btn btn-secondary">Cancel</a>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock content %}
|
||||||
58
core/templates/core/transactions.html
Normal file
58
core/templates/core/transactions.html
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block title %}
|
||||||
|
Transactions - {{ project_name }}
|
||||||
|
{% endblock title %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-5">
|
||||||
|
<div class="text-center flex-grow-1">
|
||||||
|
<h1 class="display-4">Transaction Log</h1>
|
||||||
|
<p class="lead">History of all book issues and returns.</p>
|
||||||
|
</div>
|
||||||
|
<a href="{% url 'index' %}" class="btn btn-primary">Back to Catalog</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-hover">
|
||||||
|
<thead class="table-dark">
|
||||||
|
<tr>
|
||||||
|
<th scope="col">Book Title</th>
|
||||||
|
<th scope="col">Borrower Name</th>
|
||||||
|
<th scope="col">Issue Date</th>
|
||||||
|
<th scope="col">Return Date</th>
|
||||||
|
<th scope="col">Status</th>
|
||||||
|
<th scope="col">Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for transaction in transactions %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ transaction.book.title }}</td>
|
||||||
|
<td>{{ transaction.borrower_name }}</td>
|
||||||
|
<td>{{ transaction.issue_date|date:"Y-m-d H:i" }}</td>
|
||||||
|
<td>{{ transaction.return_date|date:"Y-m-d H:i"|default:"--" }}</td>
|
||||||
|
<td>
|
||||||
|
{% if transaction.is_returned %}
|
||||||
|
<span class="badge bg-success">Returned</span>
|
||||||
|
{% else %}
|
||||||
|
<span class="badge bg-warning text-dark">Issued</span>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{% if not transaction.is_returned %}
|
||||||
|
<a href="{% url 'return_book' transaction.id %}" class="btn btn-info btn-sm">Return</a>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="6" class="text-center">No transactions found.</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock content %}
|
||||||
@ -1,7 +1,11 @@
|
|||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from .views import index
|
from .views import index, add_book, issue_book, transactions, return_book
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", index, name="index"),
|
path("", index, name="index"),
|
||||||
|
path("add_book/", add_book, name="add_book"),
|
||||||
|
path("issue_book/<int:book_id>/", issue_book, name="issue_book"),
|
||||||
|
path("transactions/", transactions, name="transactions"),
|
||||||
|
path("return_book/<int:transaction_id>/", return_book, name="return_book"),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render, redirect, get_object_or_404
|
||||||
from .models import Book
|
from .models import Book, Transaction
|
||||||
|
from .forms import BookForm, IssueBookForm
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
books = Book.objects.all()
|
books = Book.objects.all()
|
||||||
@ -8,3 +10,61 @@ def index(request):
|
|||||||
'project_name': 'Library',
|
'project_name': 'Library',
|
||||||
}
|
}
|
||||||
return render(request, 'core/index.html', context)
|
return render(request, 'core/index.html', context)
|
||||||
|
|
||||||
|
def add_book(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = BookForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
book = form.save(commit=False)
|
||||||
|
book.available_copies = book.total_copies
|
||||||
|
book.save()
|
||||||
|
return redirect('index')
|
||||||
|
else:
|
||||||
|
form = BookForm()
|
||||||
|
context = {
|
||||||
|
'form': form,
|
||||||
|
'project_name': 'Library',
|
||||||
|
}
|
||||||
|
return render(request, 'core/add_book.html', context)
|
||||||
|
|
||||||
|
def issue_book(request, book_id):
|
||||||
|
book = get_object_or_404(Book, id=book_id)
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = IssueBookForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
if book.available_copies > 0:
|
||||||
|
transaction = form.save(commit=False)
|
||||||
|
transaction.book = book
|
||||||
|
transaction.save()
|
||||||
|
book.available_copies -= 1
|
||||||
|
book.save()
|
||||||
|
return redirect('index')
|
||||||
|
else:
|
||||||
|
# Handle case where no copies are available
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
form = IssueBookForm()
|
||||||
|
context = {
|
||||||
|
'form': form,
|
||||||
|
'book': book,
|
||||||
|
'project_name': 'Library',
|
||||||
|
}
|
||||||
|
return render(request, 'core/issue_book.html', context)
|
||||||
|
|
||||||
|
def transactions(request):
|
||||||
|
transactions = Transaction.objects.all().order_by('-issue_date')
|
||||||
|
context = {
|
||||||
|
'transactions': transactions,
|
||||||
|
'project_name': 'Library',
|
||||||
|
}
|
||||||
|
return render(request, 'core/transactions.html', context)
|
||||||
|
|
||||||
|
def return_book(request, transaction_id):
|
||||||
|
transaction = get_object_or_404(Transaction, id=transaction_id)
|
||||||
|
if not transaction.is_returned:
|
||||||
|
transaction.is_returned = True
|
||||||
|
transaction.return_date = timezone.now()
|
||||||
|
transaction.save()
|
||||||
|
transaction.book.available_copies += 1
|
||||||
|
transaction.book.save()
|
||||||
|
return redirect('transactions')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user