views.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from django.shortcuts import render
  2. # Create your views here.
  3. # views.py
  4. from rest_framework.views import APIView
  5. from rest_framework.response import Response
  6. from rest_framework import status
  7. from .tasks import extract_product_attributes, batch_extract_attributes
  8. from .models import Product, ProductAttribute
  9. from .serializers import ProductAttributeSerializer
  10. class ExtractAttributesView(APIView):
  11. """
  12. Trigger attribute extraction for a product
  13. """
  14. def post(self, request, product_id):
  15. try:
  16. product = Product.objects.get(id=product_id)
  17. # Trigger async task
  18. task = extract_product_attributes.delay(product_id)
  19. return Response({
  20. 'message': 'Extraction started',
  21. 'task_id': task.id,
  22. 'product_id': product_id
  23. }, status=status.HTTP_202_ACCEPTED)
  24. except Product.DoesNotExist:
  25. return Response({'error': 'Product not found'}, status=status.HTTP_404_NOT_FOUND)
  26. class BatchExtractAttributesView(APIView):
  27. """
  28. Trigger batch extraction
  29. """
  30. def post(self, request):
  31. product_ids = request.data.get('product_ids', [])
  32. if not product_ids:
  33. return Response({'error': 'No product IDs provided'}, status=status.HTTP_400_BAD_REQUEST)
  34. task_results = batch_extract_attributes.delay(product_ids)
  35. return Response({
  36. 'message': f'Batch extraction started for {len(product_ids)} products',
  37. 'task_id': task_results.id
  38. }, status=status.HTTP_202_ACCEPTED)
  39. class ProductAttributesView(APIView):
  40. """
  41. Get extracted attributes for a product
  42. """
  43. def get(self, request, product_id):
  44. try:
  45. product = Product.objects.get(id=product_id)
  46. attributes = ProductAttribute.objects.filter(product=product)
  47. serializer = ProductAttributeSerializer(attributes, many=True)
  48. return Response({
  49. 'product_id': product_id,
  50. 'attributes_extracted': product.attributes_extracted,
  51. 'attributes': serializer.data
  52. })
  53. except Product.DoesNotExist:
  54. return Response({'error': 'Product not found'}, status=status.HTTP_404_NOT_FOUND)
  55. class AttributeReviewView(APIView):
  56. """
  57. Review and update low-confidence attributes
  58. """
  59. def get(self, request):
  60. # Get attributes needing review
  61. attributes = ProductAttribute.objects.filter(
  62. needs_review=True,
  63. reviewed=False
  64. ).select_related('product')[:50]
  65. serializer = ProductAttributeSerializer(attributes, many=True)
  66. return Response(serializer.data)
  67. def patch(self, request, attribute_id):
  68. try:
  69. attribute = ProductAttribute.objects.get(id=attribute_id)
  70. # Update attribute
  71. attribute.attribute_value = request.data.get('attribute_value', attribute.attribute_value)
  72. attribute.reviewed = True
  73. attribute.confidence_score = 1.0 # Human verified
  74. attribute.save()
  75. return Response({'message': 'Attribute updated'})
  76. except ProductAttribute.DoesNotExist:
  77. return Response({'error': 'Attribute not found'}, status=status.HTTP_404_NOT_FOUND)