Fix: Integration de la pagination dans le systeme de filtre de produit
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -54,17 +54,22 @@
|
||||
</div>
|
||||
|
||||
<div class="row mt-2">
|
||||
<div class="col">
|
||||
<input type="text" placeholder="Rechercher par libellé" class="form-control" id="searchInput">
|
||||
</div>
|
||||
<div class="col">
|
||||
<select name="" id="filterCategorie" class="form-select">
|
||||
<option value="">Filtre par catégorie</option>
|
||||
{% for categorie in liste_categorie %}
|
||||
<option value="{{ categorie.id }}">{{ categorie.categorie }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<form class="col d-flex flex-column flex-lg-row justify-content-between" id="searchForm" method="get" action="{% url 'gestion_produit:index' %}">
|
||||
<div class="col">
|
||||
<input type="text" id="searchInput" placeholder="Rechercher par libellé" class="form-control" name="search" value="{{ request.GET.search }}">
|
||||
</div>
|
||||
<div class="col">
|
||||
<select id="filterCategorie" name="filterCategorie" class="form-select">
|
||||
<option value="">Filtre par catégorie</option>
|
||||
{% for categorie in liste_categorie %}
|
||||
<option value="{{ categorie.id }}" {% if request.GET.filterCategorie == categorie.id|stringformat:"s" %}selected{% endif %}>{{ categorie.categorie }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col mt-2 mt-lg-0">
|
||||
<button type="submit" class="btn btn-primary"><i class="bi bi-search"></i> Rechercher</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="row p-2 justify-content-around" id = "cardContainer">
|
||||
|
||||
@@ -81,14 +81,4 @@ urlpatterns = [
|
||||
views.detail_demande,
|
||||
name="detail_demande"
|
||||
),
|
||||
path(
|
||||
"filtrer-produit-par-categorie/<int:id_categorie>/",
|
||||
views.filtrer_produit_par_categorie,
|
||||
name="filtrer_produit_par_categorie"
|
||||
),
|
||||
path(
|
||||
"filtrer-par-nom-produit/",
|
||||
views.filtre_par_nom_produit,
|
||||
name="filtrer_par_nom_produit"
|
||||
),
|
||||
]
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from django.shortcuts import render, redirect
|
||||
from django.core.paginator import Paginator
|
||||
from django.db.models import Sum
|
||||
from django.db.models import Sum, Q
|
||||
from authentification.models import Profile
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -90,9 +90,25 @@ def index(request):
|
||||
La réponse HTTP contenant la page rendue.
|
||||
|
||||
"""
|
||||
liste_produit = Produit.objects.all().order_by("-id")
|
||||
product_name = request.GET.get("search", "")
|
||||
categorie_name = request.GET.get("filterCategorie", "")
|
||||
liste_categorie = Categorie.objects.all().order_by("categorie")
|
||||
|
||||
if product_name == "" and categorie_name == "":
|
||||
liste_produit = Produit.objects.all().order_by("-id")
|
||||
elif product_name != "" and categorie_name == "":
|
||||
liste_produit = Produit.objects.filter(
|
||||
Q(libelle__icontains=product_name)
|
||||
).order_by("-id")
|
||||
elif product_name == "" and categorie_name != "":
|
||||
liste_produit = Produit.objects.filter(
|
||||
Q(categorie=categorie_name)
|
||||
).order_by("-id")
|
||||
else:
|
||||
liste_produit = Produit.objects.filter(
|
||||
Q(categorie=categorie_name) & Q(libelle__icontains=product_name)
|
||||
).order_by("-id")
|
||||
|
||||
liste_produit_finale = [
|
||||
calcul_stock_produit(produit) for produit in liste_produit
|
||||
]
|
||||
@@ -422,73 +438,6 @@ def enregistrer_categorie(request):
|
||||
return JsonResponse({"success": False})
|
||||
return None
|
||||
|
||||
def filtrer_produit_par_categorie(request, id_categorie):
|
||||
"""Filtrez les produits par catégorie et renvoyez-les au format JSON.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
request : HttpRequest
|
||||
La requête HTTP entrante.
|
||||
id_categorie : int
|
||||
L'ID de la catégorie pour laquelle filtrer les produits.
|
||||
|
||||
Returns
|
||||
-------
|
||||
JsonResponse
|
||||
Réponse JSON contenant la liste des produits filtrés par catégorie.
|
||||
|
||||
"""
|
||||
produits = Produit.objects.filter(categorie_id=id_categorie)
|
||||
produits_filtres = [
|
||||
{
|
||||
"id": produit.id,
|
||||
"libelle": produit.libelle,
|
||||
"conditionnement": produit.conditionnement,
|
||||
"quantite_stock": produit.quantite_stock,
|
||||
"seuil_pre_rupture": produit.seuil_pre_rupture,
|
||||
"image": produit.image.url if produit.image else "",
|
||||
"appartient_logistique": request.user.groups.filter(name="Logistique").exists(),
|
||||
|
||||
}
|
||||
for produit in produits
|
||||
]
|
||||
return JsonResponse(produits_filtres, safe=False)
|
||||
|
||||
def filtre_par_nom_produit(request):
|
||||
"""Filtrez les produits par nom et renvoyez-les au format JSON.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
request : HttpRequest
|
||||
La requête HTTP entrante.
|
||||
nom_produit : str
|
||||
Le nom du produit pour lequel filtrer les produits.
|
||||
|
||||
Returns
|
||||
-------
|
||||
JsonResponse
|
||||
Réponse JSON contenant la liste des produits filtrés par nom.
|
||||
|
||||
"""
|
||||
nom_produit = request.GET.get("nom", "")
|
||||
if nom_produit == "":
|
||||
produits = Produit.objects.all()
|
||||
else:
|
||||
produits = Produit.objects.filter(libelle__icontains=nom_produit)
|
||||
produits_filtres = [
|
||||
{
|
||||
"id": produit.id,
|
||||
"libelle": produit.libelle,
|
||||
"conditionnement": produit.conditionnement,
|
||||
"quantite_stock": produit.quantite_stock,
|
||||
"seuil_pre_rupture": produit.seuil_pre_rupture,
|
||||
"image": produit.image.url if produit.image else "",
|
||||
"appartient_logistique": request.user.groups.filter(name="Logistique").exists(),
|
||||
}
|
||||
for produit in produits
|
||||
]
|
||||
return JsonResponse(produits_filtres, safe=False)
|
||||
|
||||
@login_required
|
||||
def recuperer_fournisseur_par_categorie(_request, id_produit):
|
||||
"""Récupèrez les fournisseurs associés à la catégorie d'un produit donné.
|
||||
|
||||
Reference in New Issue
Block a user