from typing import Any from django.shortcuts import render from django.views.generic import ListView, CreateView, UpdateView from django.urls import reverse_lazy from django.contrib.auth.views import LoginView from django.contrib import messages from django.db.models import Q from django.contrib.auth.mixins import UserPassesTestMixin from .models import Product, Supplier from .forms import ProductForm class UserLoginView(LoginView): template_name = "core/login.html" class ProductListView(ListView): model = Product template_name = "core/product_list.html" context_object_name = "products" def get_queryset(self): queryset = Product.objects.all().select_related("supplier") search_query = self.request.GET.get("search", "") if search_query: queryset = queryset.filter( Q(name__icontains=search_query) | Q(description__icontains=search_query) | Q(manufacturer__icontains=search_query) | Q(category__icontains=search_query) ) supplier_id = self.request.GET.get("supplier", "") if supplier_id and supplier_id != 'all': queryset = queryset.filter(supplier_id=supplier_id) sort = self.request.GET.get("sort") if sort == "asc": queryset = queryset.order_by("quantity") if sort == "desc": queryset = queryset.order_by("-quantity") return queryset def get_context_data(self, **kwargs) -> dict[str, Any]: context = super().get_context_data(**kwargs) context["suppliers"] = Supplier.objects.all() context["current_supplier"] = self.request.GET.get("supplier", "") context["current_search"] = self.request.GET.get("search", "") context["current_sort"] = self.request.GET.get("sort", "") return context class AdminRequiredMixin(UserPassesTestMixin): def test_func(self): return ( self.request.user.is_authenticated and self.request.user.role and self.request.user.role == "Администратор" ) class ProductCreateUpdateMixin: def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["all_suppliers"] = Supplier.objects.all() return context class ProductCreateView(AdminRequiredMixin, ProductCreateUpdateMixin, CreateView): model = Product form_class = ProductForm template_name = "core/product_form.html" success_url = reverse_lazy("product_list") def form_valid(self, form): messages.success(self.request, "Товар успешно добавлен") return super().form_valid(form)