Commit ba7e9563 authored by impfundev's avatar impfundev

feat: book app shifting to class based view

parent 10c8a8ea
{% if is_paginated %}
<nav aria-label="Page navigation example"> <nav aria-label="Page navigation example">
<ul class="pagination"> <ul class="pagination">
{% if page_obj.has_previous %} {% if page_obj.has_previous %}
...@@ -27,3 +28,4 @@ ...@@ -27,3 +28,4 @@
{% endif %} {% endif %}
</ul> </ul>
</nav> </nav>
{% endif %}
...@@ -19,7 +19,7 @@ class LibrarianForm(forms.ModelForm): ...@@ -19,7 +19,7 @@ class LibrarianForm(forms.ModelForm):
"class": "form-control", "class": "form-control",
} }
), ),
"password": forms.TextInput( "password": forms.PasswordInput(
attrs={ attrs={
"placeholder": "Password", "placeholder": "Password",
"class": "form-control", "class": "form-control",
......
from django.db import models from django.db import models
from django.contrib.auth.models import User
class Librarians(models.Model): class Librarians(models.Model):
......
{% extends "layout.html" %} {% block dashboard %} {% extends "layout.html" %} {% block dashboard %}
<div style="max-width: 80vw" class="w-100 p-4"> <div style="max-width: 80vw" class="w-100 p-4">
{% include "librarians_create_form.html" %}
<div class="d-flex gap-2 mb-4"> <div class="d-flex gap-2 mb-4">
{% include "order_form.html" %} {% include "order_form.html" %}
<button <a type="button" class="btn btn-primary" href="/dashboard/librarians/add/">
type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#librarian_modal_create"
>
<i class="bi bi-plus-circle"></i> Add Librarian <i class="bi bi-plus-circle"></i> Add Librarian
</button> </a>
{% include "search_form.html" %} {% include "search_form.html" %}
</div> </div>
{% include "librarians_table_data.html" %} {% include "librarians_table_data.html" %}
</div> </div>
{% include "librarians_delete_form.html" %} {% endblock dashboard %} {% endblock dashboard %}
<form {% extends "layout.html" %} {% block dashboard %}
class="modal fade" <div style="max-width: 80vw" class="w-100 p-4">
id="librarian_modal_create" <div class="d-flex flex-column gap-2 mb-4">
tabindex="-1" <h1 class="h3">Create Librarian Account</h1>
aria-labelledby="librarian_modal_label" <form
aria-hidden="true" id="librarian_update_modal_{{ librarian_id }}"
action="/dashboard/librarians/" action="/dashboard/librarians/add/"
method="POST" method="POST"
> >
<div class="modal-dialog modal-dialog-centered"> {% csrf_token %}
<div class="modal-content"> <div class="d-flex flex-column gap-1">{{ form }}</div>
<div class="modal-header"> <div class="d-flex gap-2 my-3">
<h1 class="modal-title fs-5" id="exampleModalLabel">Input Data</h1> <a href="/dashboard/librarians/" class="btn btn-secondary">Cancel</a>
<button <button class="btn btn-primary">Submit</button>
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div> </div>
<div class="modal-body"> </form>
{% csrf_token %} {% for field in form %}
<div class="form-outline form-white mb-3">{{ field }}</div>
{% endfor %}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Close
</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div> </div>
</form> </div>
{% endblock dashboard %}
{% for librarian in librarians %} {% extends "layout.html" %} {% block dashboard %}
<form <div style="max-width: 80vw" class="w-100 p-4">
class="modal fade" <div class="d-flex flex-column gap-2 mb-4">
id="librarian_delete_modal_{{ librarian.id }}" <h1 class="h3">Are you sure want to delete this data
tabindex="-1" <form
aria-labelledby="librarian_delete_label" action=""
aria-hidden="true" method="POST"
action="/dashboard/librarians/{{ librarian.id }}/delete/" >
method="POST" {% csrf_token %}
> <div class="d-flex flex-column gap-1">Once data is deleted, it cannot be restored.</div>
{% csrf_token %} <div class="d-flex gap-2 my-3">
<div class="modal-dialog modal-dialog-centered"> <a href="/dashboard/librarians/" class="btn btn-secondary">Cancel</a>
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="exampleModalLabel">
Are you sure want to delete <b>{{ librarian.title }}</b>?
</h1>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
Once data is deleted, it cannot be restored.
<input type="hidden" name="id" id="almacen_id" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
Cancel
</button>
<button type="submit" class="btn btn-danger">Continue</button> <button type="submit" class="btn btn-danger">Continue</button>
</div> </div>
</div> </form>
</div> </div>
</form> </div>
{% endfor %} {% endblock dashboard %}
...@@ -18,17 +18,16 @@ ...@@ -18,17 +18,16 @@
<td class="d-flex gap-2"> <td class="d-flex gap-2">
<a <a
class="btn btn-success" class="btn btn-success"
href="/dashboard/librarians/{{ librarian.id }}/update/" href="/dashboard/librarians/{{ librarian.id }}/"
> >
<i class="bi bi-pencil-square"></i> <i class="bi bi-pencil-square"></i>
</a> </a>
<button <a
class="btn btn-danger" class="btn btn-danger"
data-bs-toggle="modal" href="/dashboard/librarians/{{ librarian.id }}/delete/"
data-bs-target="#librarian_delete_modal_{{ librarian.id }}"
> >
<i class="bi bi-trash3-fill"></i> <i class="bi bi-trash3-fill"></i>
</button> </a>
</td> </td>
</tr> </tr>
{% endfor %} {% else %} {% endfor %} {% else %}
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<h1 class="h3">Update Data</h1> <h1 class="h3">Update Data</h1>
<form <form
id="librarian_update_modal_{{ librarian_id }}" id="librarian_update_modal_{{ librarian_id }}"
action="/dashboard/librarians/{{ librarian_id }}/update/" action=""
method="POST" method="POST"
> >
{% csrf_token %} {% csrf_token %}
......
from django.urls import path from django.urls import path
from librarians.views import index, update, delete from librarians.views import (
LibrarianListView,
LibrarianCreateView,
LibrarianUpdateView,
LibrarianDeleteView,
)
urlpatterns = [ urlpatterns = [
path("", index, name="librarian_lists"), path("", LibrarianListView.as_view(), name="librarian_lists"),
path("<id>/update/", update, name="update_librarian"), path("add/", LibrarianCreateView.as_view(), name="create_librarian"),
path("<id>/delete/", delete, name="delete_librarian"), path("<int:pk>/", LibrarianUpdateView.as_view(), name="update_librarian"),
path("<int:pk>/delete/", LibrarianDeleteView.as_view(), name="delete_librarian"),
] ]
from authentications.utils import Hasher
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.db.models import Q from django.db.models import Q
from django.views import generic
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect, get_object_or_404
from datetime import datetime from datetime import datetime
from authentications.utils import Hasher
from librarians.models import Librarians from librarians.models import Librarians
from librarians.forms import LibrarianForm from librarians.forms import LibrarianForm
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
def index(request):
librarians = Librarians.objects.all()
context = {"librarians": librarians, "form": LibrarianForm()}
default_page = 1
page = request.GET.get("page", default_page)
items_per_page = 5
paginator = Paginator(librarians, items_per_page)
try:
page_obj = paginator.page(page)
context["page_obj"] = page_obj
context["librarians"] = page_obj
except PageNotAnInteger:
page_obj = paginator.page(default_page)
context["page_obj"] = page_obj
context["librarians"] = page_obj
except EmptyPage: class LibrarianListView(generic.ListView):
page_obj = paginator.page(paginator.num_pages) model = Librarians
context["page_obj"] = page_obj template_name = "librarians.html"
context["librarians"] = page_obj paginate_by = 5
context_object_name = "librarians"
if request.method == "POST": def get_queryset(self):
form = LibrarianForm(request.POST) queryset = super().get_queryset()
if form.is_valid: keyword = self.request.GET.get("q")
name = form.data["name"] order = self.request.GET.get("o")
email = form.data["email"]
password = form.data["password"]
hashed_password = Hasher.encode(password=password)
Librarians.objects.create(name=name, email=email, password=hashed_password) if keyword:
queryset = queryset.filter(
if request.method == "GET":
keyword = request.GET.get("q")
order = request.GET.get("o")
if keyword is not None:
filtered_book_list = Librarians.objects.filter(
Q(name__icontains=keyword) | Q(email__icontains=keyword) Q(name__icontains=keyword) | Q(email__icontains=keyword)
).order_by("-created_at") ).order_by("-created_at")
context["librarians"] = filtered_book_list
if order == "new":
context["librarians"] = Librarians.objects.all().order_by("-updated_at")[
:10
]
elif order == "old":
context["librarians"] = Librarians.objects.all().order_by("updated_at")
return render(request, "librarians.html", context)
def update(request, id):
latest_librarian_list = Librarians.objects.order_by("created_at")
context = {"librarians": latest_librarian_list}
librarian = Librarians.objects.get(id=id)
initial = {
"name": librarian.name,
"email": librarian.email,
}
form = LibrarianForm(request.POST or None, initial=initial)
if request.method == "POST":
if form.is_valid:
name = form.data["name"]
email = form.data["email"]
password = form.data["password"]
hashed_password = Hasher.encode(password=password)
librarian = Librarians.objects.filter(id=id)
librarian.update( if order:
name=name, if order == "new":
email=email, queryset = queryset.order_by("-created_at")
password=hashed_password, elif order == "old":
updated_at=datetime.now(), queryset = queryset.order_by("created_at")
)
return HttpResponseRedirect("/dashboard/librarians") return queryset
context["form"] = form
context["librarian_id"] = id
return render(request, "librarians_update_form.html", context)
class LibrarianCreateView(generic.edit.CreateView):
model = Librarians
form_class = LibrarianForm
success_url = "/dashboard/librarians/"
template_name = "librarians_create_form.html"
success_message = "Librarian created successfully!"
def delete(request, id):
context = {}
librarian = get_object_or_404(Librarians, id=id)
if request.method == "POST": class LibrarianUpdateView(generic.edit.UpdateView):
librarian.delete() model = Librarians
form_class = LibrarianForm
success_url = "/dashboard/librarians"
template_name = "librarians_update_form.html"
success_message = "Librarian updated successfully!"
return HttpResponseRedirect("/dashboard/librarians")
return render(request, "librarians.html", context) class LibrarianDeleteView(generic.edit.DeleteView):
model = Librarians
success_url = "/dashboard/librarians"
template_name = "librarians_delete_form.html"
success_message = "Librarian deleted successfully!"
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