search lots function and sortable table

This commit is contained in:
Thomas Nahuel Rusiecki 2025-02-21 14:40:05 -03:00
parent 29f08a633b
commit 5ea174d553
2 changed files with 112 additions and 50 deletions

View file

@ -1,43 +1,63 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load i18n %} {% load i18n paginacion %}
{% load render_table from django_tables2 %}
{% block content %} {% block content %}
<div class="row mb-3">
<div class="col">
<h3>{{ subtitle }}</h3>
</div>
<div class="col text-center">
{% if show_closed %}
<a href="?show_closed=false" class="btn btn-green-admin">
{% trans 'Hide closed lots' %}
</a>
{% else %}
<a href="?show_closed=true" class="btn btn-green-admin">
{% trans 'Show closed lots' %}
</a>
{% endif %}
<a href="{% url 'lot:add' %}" type="button" class="btn btn-green-admin"> <a href="{% url 'lot:add' %}" type="button" class="btn btn-green-admin">
<i class="bi bi-plus"></i> <i class="bi bi-plus"></i>
{% trans 'Add new lot' %} {% trans 'Add new lot' %}
</a> </a>
<h3>{{ subtitle }}</h3>
<!-- Search and Filter Section -->
<div class="row mb-4">
<!-- Search Input -->
<div class="col-md-6">
<form method="get" class="d-flex">
<input
type="text"
name="q"
class="form-control me-2"
placeholder="{% trans 'Search lots...' %}"
value="{{ search_query }}">
<button type="submit" class="btn btn-outline-secondary">
<i class="bi bi-search"></i>
</button>
</form>
</div>
<!-- Filter Dropdown -->
<div class="col-md-6 text-md-end">
<div class="dropdown">
<button class="btn btn-outline-secondary dropdown-toggle" type="button" id="filterDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-filter me-2"></i>
{% trans 'Filter' %}
</button>
<ul class="dropdown-menu" aria-labelledby="filterDropdown">
<li>
<a class="dropdown-item {% if show_closed %}border border-info rounded{% endif %} " href="?show_closed={% if show_closed %}false{% else %}true{% endif %}{% if search_query %}&q={{ search_query }}{% endif %}">
{% trans 'Closed Lots' %}
{% if show_closed %}<i class="bi bi-check"></i>{% endif %}
</a>
</li>
<li>
<a class="dropdown-item" href="?{% if search_query %}q={{ search_query }}{% endif %}">
{% trans 'Clear Filters' %}
</a>
</li>
</ul>
</div>
</div> </div>
</div> </div>
<div class="row"> <!-- Lots Table -->
<table class= "table table-striped table-sm"> {% render_table table %}
{% for lot in lots %} <div class="mt-5 d-flex align-items-center justify-content-center">
<tr> {% if table.page and table.paginator.num_pages > 1 %}
<td><a href="{% url 'dashboard:lot' lot.id %}">{{ lot.name }}</a></td> {% include "django_tables2/pagination.html" %}
<td> {% endif %}
<a href="{% url 'lot:edit' lot.id %}"><i class="bi bi-pen"></i></a> </div>{% endblock %}
</td>
<td>
<a href="{% url 'lot:delete' lot.id %}"><i class="bi bi-trash text-danger"></i></a>
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}

View file

@ -4,12 +4,14 @@ from django.shortcuts import get_object_or_404, redirect, Http404
from django.contrib import messages from django.contrib import messages
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.db.models import Q
from django.views.generic.edit import ( from django.views.generic.edit import (
CreateView, CreateView,
DeleteView, DeleteView,
UpdateView, UpdateView,
FormView, FormView,
) )
import django_tables2 as tables
from dashboard.mixins import DashboardView from dashboard.mixins import DashboardView
from lot.models import Lot, LotTag, LotProperty from lot.models import Lot, LotTag, LotProperty
from lot.forms import LotsForm from lot.forms import LotsForm
@ -137,31 +139,71 @@ class DelToLotView(AddToLotView):
return response return response
class LotsTagsView(DashboardView, TemplateView): class LotTable(tables.Table):
name = tables.Column(linkify=("dashboard:lot", {"pk": tables.A("id")}), verbose_name=_("Lot Name"))
description = tables.Column(verbose_name=_("Description"), default="No description")
closed = tables.Column(verbose_name=_("Status"))
created = tables.DateColumn(format="Y-m-d", verbose_name=_("Created On"))
user = tables.Column(verbose_name=_("Created By"), default="Unknown")
actions = tables.TemplateColumn(
template_name="lot_actions.html",
verbose_name=_("Actions"),
orderable=False,
attrs={"td": {"class": "text-end"}}
)
class Meta:
model = Lot
fields = ("name", "description", "closed", "created", "user", "actions")
attrs = {
"class": "table table-hover align-middle",
"thead": {"class": "table-light"}
}
class LotsTagsView(DashboardView, tables.SingleTableView):
template_name = "lots.html" template_name = "lots.html"
title = _("lots") title = _("lots")
breadcrumb = _("lots") + " /" breadcrumb = _("lots") + " /"
success_url = reverse_lazy('dashboard:unassigned') success_url = reverse_lazy('dashboard:unassigned')
model = Lot
table_class = LotTable
def get_queryset(self):
self.pk = self.kwargs.get('pk')
self.tag = get_object_or_404(LotTag, owner=self.request.user.institution, id=self.pk)
self.show_closed = self.request.GET.get('show_closed', 'false') == 'true'
self.search_query = self.request.GET.get('q', '').strip()
queryset = Lot.objects.filter(owner=self.request.user.institution, type=self.tag)
if not self.show_closed:
queryset = queryset.filter(closed=True)
if self.search_query:
queryset = queryset.filter(
Q(name__icontains=self.search_query) |
Q(description__icontains=self.search_query) |
Q(code__icontains=self.search_query)
)
sort = self.request.GET.get('sort')
if sort:
queryset = queryset.order_by(sort)
return queryset
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
self.pk = kwargs.get('pk')
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
tag = get_object_or_404(LotTag, owner=self.request.user.institution, id=self.pk)
self.title += " {}".format(tag.name)
self.breadcrumb += " {}".format(tag.name)
show_closed = self.request.GET.get('show_closed', 'false') == 'true'
lots = Lot.objects.filter(owner=self.request.user.institution).filter(
type=tag, closed=show_closed
)
context.update({ context.update({
'lots': lots, 'title': self.title + " " + self.tag.name,
'title': self.title, 'breadcrumb': self.breadcrumb + " " + self.tag.name,
'breadcrumb': self.breadcrumb, 'show_closed': self.show_closed,
'show_closed': show_closed 'search_query': self.search_query, # Pass the search query to the template
}) })
return context return context
class LotPropertiesView(DashboardView, TemplateView): class LotPropertiesView(DashboardView, TemplateView):
template_name = "properties.html" template_name = "properties.html"
title = _("New Lot Property") title = _("New Lot Property")