You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.8 KiB
43 lines
1.8 KiB
import logging # Импортируем библиотеку для логирования
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
from .models import Product
|
|
from .forms import ProductForm
|
|
|
|
# Настраиваем логгер
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def product_list(request):
|
|
products = Product.objects.all()
|
|
return render(request, 'product_directory/product_list.html', {'products': products})
|
|
|
|
def product_create(request):
|
|
if request.method == 'POST':
|
|
form = ProductForm(request.POST)
|
|
if form.is_valid():
|
|
product = form.save()
|
|
logger.info(f"Пользователь {request.user} создал товар '{product.name}'")
|
|
return redirect('product_list')
|
|
else:
|
|
form = ProductForm()
|
|
return render(request, 'product_directory/product_form.html', {'form': form})
|
|
|
|
def product_update(request, pk):
|
|
product = get_object_or_404(Product, pk=pk)
|
|
if request.method == 'POST':
|
|
form = ProductForm(request.POST, instance=product)
|
|
if form.is_valid():
|
|
product = form.save()
|
|
logger.info(f"Пользователь {request.user} обновил товар '{product.name}'")
|
|
return redirect('product_list')
|
|
else:
|
|
form = ProductForm(instance=product)
|
|
return render(request, 'product_directory/product_form.html', {'form': form})
|
|
|
|
def product_delete(request, pk):
|
|
product = get_object_or_404(Product, pk=pk)
|
|
if request.method == 'POST':
|
|
logger.warning(f"Пользователь {request.user} удалил товар '{product.name}'")
|
|
product.delete()
|
|
return redirect('product_list')
|
|
return render(request, 'product_directory/product_confirm_delete.html', {'product': product})
|