Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion essenza/product/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

urlpatterns = [
path("stock/", views.StockView.as_view(), name="stock"),
path('search/', views.SearchView.as_view(), name='product_search'),
path('', views.ProductListView.as_view(), name='product_list'),
path('create/', views.ProductCreateView.as_view(), name='product_create'),
path('<int:pk>/', views.ProductDetailView.as_view(), name='product_detail'),
Expand Down
53 changes: 25 additions & 28 deletions essenza/product/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_func(self):
)

def get(self, request, *args, **kwargs):
q = request.GET.get('q', '').strip()

month_ago = timezone.now() - timezone.timedelta(days=30)
year_ago = timezone.now() - timezone.timedelta(days=365)

Expand All @@ -43,6 +45,11 @@ def get_top_selling_products(since):
top_products = ordered_query[:10]
return top_products

# If a search query is provided, show matching products instead of top sellers
if q:
products = Product.objects.filter(is_active=True, name__icontains=q)
return render(request, self.template_name, {"products": products, "query": q})

products = get_top_selling_products(since=month_ago)
if not products.exists():
products = get_top_selling_products(since=year_ago)
Expand All @@ -63,8 +70,12 @@ def handle_no_permission(self):

def get(self, request):
# Carga y muestra todos los productos ordenados por nombre
products = Product.objects.all().order_by("name")
return render(request, "product/stock.html", {"products": products})
q = request.GET.get('q', '').strip()
if q:
products = Product.objects.filter(name__icontains=q).order_by("name")
else:
products = Product.objects.all().order_by("name")
return render(request, "product/stock.html", {"products": products, "query": q})

def post(self, request):
# Coge datos del formulario para actualizar stock
Expand Down Expand Up @@ -102,8 +113,12 @@ def test_func(self):
return self.request.user.is_authenticated and self.request.user.role == "admin"

def get(self, request):
products = Product.objects.all()
return render(request, self.template_name, {"products": products})
q = request.GET.get('q', '').strip()
if q:
products = Product.objects.filter(name__icontains=q)
else:
products = Product.objects.all()
return render(request, self.template_name, {"products": products, "query": q})


class ProductDetailView(LoginRequiredMixin, UserPassesTestMixin, View):
Expand Down Expand Up @@ -177,8 +192,12 @@ class CatalogView(View):
template_name = "product/catalog.html"

def get(self, request):
products = Product.objects.filter(is_active=True)
return render(request, self.template_name, {"products": products})
q = request.GET.get('q', '').strip()
if q:
products = Product.objects.filter(is_active=True, name__icontains=q)
else:
products = Product.objects.filter(is_active=True)
return render(request, self.template_name, {"products": products, "query": q})


class CatalogDetailView(View):
Expand All @@ -189,26 +208,4 @@ def get(self, request, pk):
return render(request, self.template_name, {"product": product})


class SearchView(View):
"""Search products by name. Uses GET parameter `q`.
- For anonymous or regular users, only searches active products.
- For staff/admin, searches all products.
Renders `product/catalog.html` with `products` and `query` in context.
"""
template_name = "product/catalog.html"

def get(self, request):
q = request.GET.get("q", "").strip()

# If admin/staff, show the admin list template; otherwise show catalog for users
if request.user.is_authenticated and (request.user.is_staff or getattr(request.user, 'role', None) == 'admin'):
template_name = "product/list.html"
qs = Product.objects.all()
else:
template_name = "product/catalog.html"
qs = Product.objects.filter(is_active=True)

products = qs.filter(name__icontains=q) if q else qs

return render(request, template_name, {"products": products, "query": q})
2 changes: 1 addition & 1 deletion essenza/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@

<div class="nav-center">
<div class="search-bar">
<form method="get" action="{% url 'product_search' %}">
<form method="get" action="{{ request.path }}">
<input name="q" type="search" placeholder="Buscar..." aria-label="Buscar productos" value="{{ request.GET.q|default:'' }}">
</form>
</div>
Expand Down