52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
from django.shortcuts import render, redirect, get_object_or_404
|
|
from django.urls import reverse_lazy
|
|
from django.views.generic import ListView, CreateView, UpdateView, DeleteView
|
|
from .models import Product
|
|
|
|
class DashboardView(ListView):
|
|
model = Product
|
|
template_name = 'core/index.html'
|
|
context_object_name = 'products'
|
|
|
|
class ProductListView(ListView):
|
|
model = Product
|
|
template_name = 'core/product_list.html'
|
|
context_object_name = 'products'
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
name = self.request.GET.get('name')
|
|
category = self.request.GET.get('category')
|
|
|
|
if name:
|
|
queryset = queryset.filter(name__icontains=name)
|
|
|
|
if category:
|
|
queryset = queryset.filter(category__iexact=category)
|
|
|
|
return queryset
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['categories'] = Product.objects.values_list('category', flat=True).distinct()
|
|
return context
|
|
|
|
class ProductCreateView(CreateView):
|
|
model = Product
|
|
template_name = 'core/product_form.html'
|
|
fields = ['name', 'category', 'sku', 'price', 'stock', 'minimum_stock_level']
|
|
success_url = reverse_lazy('core:product-list')
|
|
|
|
class ProductUpdateView(UpdateView):
|
|
model = Product
|
|
template_name = 'core/product_form.html'
|
|
fields = ['name', 'category', 'sku', 'price', 'stock', 'minimum_stock_level']
|
|
success_url = reverse_lazy('core:product-list')
|
|
|
|
class ProductDeleteView(DeleteView):
|
|
model = Product
|
|
template_name = 'core/product_confirm_delete.html'
|
|
success_url = reverse_lazy('core:product-list')
|
|
|
|
# The home view is now the product list view.
|
|
home = DashboardView.as_view() |