All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 22s
86 lines
2.8 KiB
Python
86 lines
2.8 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()
|
|
|
|
product_status = request.query_params.get('status')
|
|
if product_status:
|
|
products = products.filter(status=product_status)
|
|
|
|
seller_status = request.query_params.get('seller_status')
|
|
if seller_status:
|
|
products = products.filter(supplier__status=seller_status)
|
|
|
|
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)
|
|
|
|
product.status = 'approved'
|
|
product.save()
|
|
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)
|
|
|
|
product.status = 'hold'
|
|
product.save()
|
|
log_product_action(request.user, 'hold_product', product.id, {"title": product.name})
|
|
return Response({"message": f"Product '{product.name}' placed on hold"})
|
|
|
|
@api_view(['POST'])
|
|
@permission_classes([IsCRMAdmin])
|
|
def reject_product(request, pk):
|
|
try:
|
|
product = Product.objects.get(pk=pk)
|
|
except Product.DoesNotExist:
|
|
return Response({"detail": "Product not found"}, status=404)
|
|
|
|
product.status = 'rejected'
|
|
product.save()
|
|
log_product_action(request.user, 'reject_product', product.id, {"title": product.name})
|
|
return Response({"message": f"Product '{product.name}' rejected"})
|
|
|
|
@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"})
|