Commit 7f25fe9d authored by impfundev's avatar impfundev

feat: stock book

parent 688b1650
......@@ -29,6 +29,10 @@ def index(request):
return_date = form.data["return_date"] or None
notes = form.data["notes"]
book = books.get(id=book_id)
new_stock = book.stock - 1
books.filter(id=book_id).update(stock=new_stock)
auth_session = request.session.get("auth_session", None)
decoded = jwt.decode(
auth_session, settings.JWT_SECRET, algorithms=["HS256"]
......@@ -75,6 +79,7 @@ def update(request, id):
auth_session = request.session.get("auth_session", None)
decoded = jwt.decode(auth_session, settings.JWT_SECRET, algorithms=["HS256"])
librarians_id = decoded["librarian_id"]
context["initial_book_id"] = book_id
if form.is_valid:
loan_date = form.data["loan_date"]
......
......@@ -11,6 +11,15 @@ class BookForm(forms.Form):
}
),
)
stock = forms.IntegerField(
widget=forms.TextInput(
attrs={
"type": "number",
"placeholder": "Stock",
"class": "form-control",
}
),
)
description = forms.CharField(
max_length=255,
widget=forms.Textarea(
......
# Generated by Django 4.2 on 2024-07-03 08:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('books', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='book',
name='stock',
field=models.BigIntegerField(blank=True, null=True),
),
]
......@@ -4,6 +4,7 @@ from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
description = models.CharField(max_length=255)
stock = models.BigIntegerField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
......
......@@ -2,6 +2,7 @@
<thead>
<tr class="table-primary">
<th scope="col">Title</th>
<th scope="col">Stock</th>
<th scope="col">Description</th>
<th scope="col">Created At</th>
<th scope="col">Updated At</th>
......@@ -12,6 +13,7 @@
{% if books %} {% for book in books %}
<tr>
<td>{{ book.title }}</td>
<td>{{ book.stock }}</td>
<td>{{ book.description }}</td>
<td>{{ book.created_at }}</td>
<td>{{ book.updated_at }}</td>
......
......@@ -14,9 +14,10 @@ def index(request):
form = BookForm(request.POST)
if form.is_valid:
title = form.data["title"]
stock = form.data["stock"]
description = form.data["description"]
Book.objects.create(title=title, description=description)
Book.objects.create(title=title, stock=stock, description=description)
return render(request, "book.html", context)
......@@ -25,17 +26,27 @@ def update(request, id):
latest_book_list = Book.objects.order_by("created_at")[:10]
context = {"books": latest_book_list}
book = Book.objects.get(id=id)
initial_dict = {"title": book.title, "description": book.description}
initial_dict = {
"title": book.title,
"stock": book.stock,
"description": book.description,
}
print(book.title)
form = BookForm(request.POST or None, initial=initial_dict)
if request.method == "POST":
if form.is_valid:
title = form.data["title"]
stock = form.data["stock"]
description = form.data["description"]
book = Book.objects.filter(id=id)
book.update(title=title, description=description, updated_at=datetime.now())
book.update(
title=title,
stock=stock,
description=description,
updated_at=datetime.now(),
)
return HttpResponseRedirect("/dashboard/books")
context["form"] = form
......
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