|
|
@@ -975,3 +975,64 @@ def open_outlook_mail(request):
|
|
|
|
|
|
except Exception as e:
|
|
|
return HttpResponse(f"<h3>Error:</h3><p>{str(e)}</p>", status=500)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# views.py
|
|
|
+from rest_framework.views import APIView
|
|
|
+from rest_framework.response import Response
|
|
|
+from rest_framework import status
|
|
|
+from django.db.models import Avg, Count
|
|
|
+from .models import Product, AttributeScore # adjust the import path as needed
|
|
|
+
|
|
|
+class ProductTypeQualityMetricsView(APIView):
|
|
|
+ """
|
|
|
+ API endpoint to fetch product type quality metrics.
|
|
|
+ Supports optional ?product_type=<type> query param.
|
|
|
+ """
|
|
|
+
|
|
|
+ def get(self, request):
|
|
|
+ product_type_filter = request.query_params.get('product_type', None)
|
|
|
+ queryset = Product.objects.all()
|
|
|
+
|
|
|
+ if product_type_filter:
|
|
|
+ queryset = queryset.filter(product_type=product_type_filter)
|
|
|
+
|
|
|
+ scored = (
|
|
|
+ AttributeScore.objects.filter(product__in=queryset)
|
|
|
+ .annotate(
|
|
|
+ title_quality=Avg('details__title_quality'),
|
|
|
+ description_quality=Avg('details__description_quality'),
|
|
|
+ image_quality=Avg('details__image_score'),
|
|
|
+ attributes_quality=Avg('details__attributes'),
|
|
|
+ )
|
|
|
+ .values('product__product_type')
|
|
|
+ .annotate(
|
|
|
+ product_count=Count('product', distinct=True),
|
|
|
+ scored_product_count=Count('id'),
|
|
|
+ avg_overall_score=Avg('score'),
|
|
|
+ avg_title_quality=Avg('details__title_quality'),
|
|
|
+ avg_description_quality=Avg('details__description_quality'),
|
|
|
+ avg_image_quality=Avg('details__image_score'),
|
|
|
+ avg_attributes_quality=Avg('details__attributes'),
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ results = [
|
|
|
+ {
|
|
|
+ "product_type": item['product__product_type'],
|
|
|
+ "product_count": item['product_count'],
|
|
|
+ "scored_product_count": item['scored_product_count'],
|
|
|
+ "avg_overall_score": round(item['avg_overall_score'] or 0, 2),
|
|
|
+ "avg_title_quality_percent": round(item['avg_title_quality'] or 0, 2),
|
|
|
+ "avg_description_quality_percent": round(item['avg_description_quality'] or 0, 2),
|
|
|
+ "avg_image_quality_percent": round(item['avg_image_quality'] or 0, 2),
|
|
|
+ "avg_attributes_quality_percent": round(item['avg_attributes_quality'] or 0, 2),
|
|
|
+ }
|
|
|
+ for item in scored
|
|
|
+ ]
|
|
|
+
|
|
|
+ return Response(results, status=status.HTTP_200_OK)
|