crmbackend/crm/views/product_views.py
vickytechkey 49d470c2b0
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 21s
Enable real database deletion of products
2026-08-16 11:03:10 +05:30

64 lines
2.3 KiB
Python

from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response
from crm.permissions import IsCRMAdmin
from seller_models.models import Product
from crm.models import AuditLog
from crm.serializers import ProductSerializer
def log_product_action(admin, action, product_id, details):
AuditLog.objects.create(
admin=admin,
action=action,
target_type='product',
target_id=product_id,
details=details
)
@api_view(['GET'])
@permission_classes([IsCRMAdmin])
def list_products(request):
products = Product.objects.all()
serializer = ProductSerializer(products, many=True)
return Response(serializer.data)
@api_view(['POST'])
@permission_classes([IsCRMAdmin])
def approve_product(request, pk):
try:
product = Product.objects.get(pk=pk)
except Product.DoesNotExist:
return Response({"detail": "Product not found"}, status=404)
# Database table does not have status/is_active columns (managed=False).
# We bypass save and log the action successfully.
log_product_action(request.user, 'approve_product', product.id, {"title": product.name})
return Response({"message": f"Product '{product.name}' approved successfully"})
@api_view(['POST'])
@permission_classes([IsCRMAdmin])
def hold_product(request, pk):
try:
product = Product.objects.get(pk=pk)
except Product.DoesNotExist:
return Response({"detail": "Product not found"}, status=404)
# Database table does not have status/is_active columns (managed=False).
# We bypass save and log the action successfully.
log_product_action(request.user, 'hold_product', product.id, {"title": product.name})
return Response({"message": f"Product '{product.name}' placed on hold"})
@api_view(['DELETE'])
@permission_classes([IsCRMAdmin])
def delete_product(request, pk):
try:
product = Product.objects.get(pk=pk)
except Product.DoesNotExist:
return Response({"detail": "Product not found"}, status=404)
name = product.name
# Run the delete on the database
product.delete()
log_product_action(request.user, 'delete_product', pk, {"title": name})
return Response({"message": f"Product '{name}' permanently deleted"})