| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- from django.shortcuts import render
- # Create your views here.
- # views.py
- from rest_framework.views import APIView
- from rest_framework.response import Response
- from rest_framework import status
- from .tasks import extract_product_attributes, batch_extract_attributes
- from .models import Product, ProductAttribute
- from .serializers import ProductAttributeSerializer
- class ExtractAttributesView(APIView):
- """
- Trigger attribute extraction for a product
- """
- def post(self, request, product_id):
- try:
- product = Product.objects.get(id=product_id)
-
- # Trigger async task
- task = extract_product_attributes.delay(product_id)
-
- return Response({
- 'message': 'Extraction started',
- 'task_id': task.id,
- 'product_id': product_id
- }, status=status.HTTP_202_ACCEPTED)
-
- except Product.DoesNotExist:
- return Response({'error': 'Product not found'}, status=status.HTTP_404_NOT_FOUND)
- class BatchExtractAttributesView(APIView):
- """
- Trigger batch extraction
- """
- def post(self, request):
- product_ids = request.data.get('product_ids', [])
-
- if not product_ids:
- return Response({'error': 'No product IDs provided'}, status=status.HTTP_400_BAD_REQUEST)
-
- task_results = batch_extract_attributes.delay(product_ids)
-
- return Response({
- 'message': f'Batch extraction started for {len(product_ids)} products',
- 'task_id': task_results.id
- }, status=status.HTTP_202_ACCEPTED)
- class ProductAttributesView(APIView):
- """
- Get extracted attributes for a product
- """
- def get(self, request, product_id):
- try:
- product = Product.objects.get(id=product_id)
- attributes = ProductAttribute.objects.filter(product=product)
-
- serializer = ProductAttributeSerializer(attributes, many=True)
-
- return Response({
- 'product_id': product_id,
- 'attributes_extracted': product.attributes_extracted,
- 'attributes': serializer.data
- })
-
- except Product.DoesNotExist:
- return Response({'error': 'Product not found'}, status=status.HTTP_404_NOT_FOUND)
- class AttributeReviewView(APIView):
- """
- Review and update low-confidence attributes
- """
- def get(self, request):
- # Get attributes needing review
- attributes = ProductAttribute.objects.filter(
- needs_review=True,
- reviewed=False
- ).select_related('product')[:50]
-
- serializer = ProductAttributeSerializer(attributes, many=True)
- return Response(serializer.data)
-
- def patch(self, request, attribute_id):
- try:
- attribute = ProductAttribute.objects.get(id=attribute_id)
-
- # Update attribute
- attribute.attribute_value = request.data.get('attribute_value', attribute.attribute_value)
- attribute.reviewed = True
- attribute.confidence_score = 1.0 # Human verified
- attribute.save()
-
- return Response({'message': 'Attribute updated'})
-
- except ProductAttribute.DoesNotExist:
- return Response({'error': 'Attribute not found'}, status=status.HTTP_404_NOT_FOUND)
|