diff --git a/essenza/essenza/urls.py b/essenza/essenza/urls.py index 2390f45..4f381fc 100644 --- a/essenza/essenza/urls.py +++ b/essenza/essenza/urls.py @@ -12,8 +12,8 @@ path("admin/", admin.site.urls), path("product/", include("product.urls")), path("", DashboardView.as_view(), name="dashboard"), - path("catalogo/", CatalogView.as_view(), name="catalog"), - path("catalogo//", CatalogDetailView.as_view(), name="catalog_detail"), + path("catalog/", CatalogView.as_view(), name="catalog"), + path("catalog//", CatalogDetailView.as_view(), name="catalog_detail"), ] if settings.DEBUG: diff --git a/essenza/product/urls.py b/essenza/product/urls.py index dfa067d..7d75e6e 100644 --- a/essenza/product/urls.py +++ b/essenza/product/urls.py @@ -6,11 +6,12 @@ urlpatterns = [ path("stock/", views.StockView.as_view(), name="stock"), - path("", views.ProductListView.as_view(), name="product_list"), - path("create/", views.ProductCreateView.as_view(), name="product_create"), - path("/", views.ProductDetailView.as_view(), name="product_detail"), - path("/edit/", views.ProductUpdateView.as_view(), name="product_update"), - path("/delete/", views.ProductDeleteView.as_view(), name="product_delete"), + 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('/', views.ProductDetailView.as_view(), name='product_detail'), + path('/edit/', views.ProductUpdateView.as_view(), name='product_update'), + path('/delete/', views.ProductDeleteView.as_view(), name='product_delete'), ] if settings.DEBUG: diff --git a/essenza/product/views.py b/essenza/product/views.py index bb824ea..c0ea21a 100644 --- a/essenza/product/views.py +++ b/essenza/product/views.py @@ -187,3 +187,28 @@ class CatalogDetailView(View): def get(self, request, pk): product = get_object_or_404(Product, pk=pk, is_active=True) 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}) diff --git a/essenza/templates/base.html b/essenza/templates/base.html index 5e04e96..2bf2a82 100644 --- a/essenza/templates/base.html +++ b/essenza/templates/base.html @@ -259,33 +259,34 @@ } - {% block extra_head %}{% endblock %} - - -
-