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
4 changes: 2 additions & 2 deletions essenza/essenza/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:pk>/", CatalogDetailView.as_view(), name="catalog_detail"),
path("catalog/", CatalogView.as_view(), name="catalog"),
path("catalog/<int:pk>/", CatalogDetailView.as_view(), name="catalog_detail"),
]

if settings.DEBUG:
Expand Down
11 changes: 6 additions & 5 deletions essenza/product/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<int:pk>/", views.ProductDetailView.as_view(), name="product_detail"),
path("<int:pk>/edit/", views.ProductUpdateView.as_view(), name="product_update"),
path("<int:pk>/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('<int:pk>/', views.ProductDetailView.as_view(), name='product_detail'),
path('<int:pk>/edit/', views.ProductUpdateView.as_view(), name='product_update'),
path('<int:pk>/delete/', views.ProductDeleteView.as_view(), name='product_delete'),
]

if settings.DEBUG:
Expand Down
25 changes: 25 additions & 0 deletions essenza/product/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
41 changes: 21 additions & 20 deletions essenza/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -259,33 +259,34 @@
}
</style>

{% block extra_head %}{% endblock %}
</head>
<body>
<header>
<div class="nav-left">
<a href="/info/" class="info-button" title="Información">i</a>
<a href="/" class="brand">ESSENZA</a>
<nav class="nav-links" aria-label="Navegación principal">
{% if not user.is_staff and not user.is_superuser %}
<a href="/">Escaparate</a>
<a href="/catalogo">Catalogo</a>
{% endif %} {% if user.is_staff or user.is_superuser %}
{% block extra_head %}{% endblock %}
</head>
<body>

<header>

<div class="nav-left">
<a href="/info/" class="info-button" title="Información">i</a>
<a href="/" class="brand">ESSENZA</a>
<nav class="nav-links" aria-label="Navegación principal">
{% if not user.is_staff and not user.is_superuser %}
<a href="/">Escaparate</a>
<a href="/catalog">Catalogo</a>
{% endif %}
{% if user.is_staff or user.is_superuser %}
<a href="/product/stock/">Stock</a>
<a href="/product/">Productos</a>
{% endif %}
</nav>
</div>

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

<div class="nav-right">
<div class="profile-dropdown">
Expand Down
2 changes: 1 addition & 1 deletion essenza/templates/product/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<div class="product-details">
<h1>{{ product.name }}</h1>
<div class="product-brand">{{ product.brand }}</div>
<div class="product-category">{{ product.category }}</div>
<div class="product-category">{{ product.get_category_display }}</div>
<div class="product-price">€ {{ product.price }}</div>
{% if product.stock == 0 %}
<span>¡Producto agotado!</span>
Expand Down