|
@@ -662,3 +662,68 @@ class ProductUploadExcelView(APIView):
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import pandas as pd
|
|
|
|
|
+from rest_framework.views import APIView
|
|
|
|
|
+from rest_framework.response import Response
|
|
|
|
|
+from rest_framework import status
|
|
|
|
|
+from rest_framework.parsers import MultiPartParser, FormParser
|
|
|
|
|
+from .models import ProductType, ProductAttribute, AttributePossibleValue
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class ProductAttributesUploadView(APIView):
|
|
|
|
|
+ """
|
|
|
|
|
+ POST API to upload an Excel file and add mandatory/additional attributes
|
|
|
|
|
+ for product types with possible values.
|
|
|
|
|
+ """
|
|
|
|
|
+ parser_classes = (MultiPartParser, FormParser)
|
|
|
|
|
+
|
|
|
|
|
+ def post(self, request):
|
|
|
|
|
+ file_obj = request.FILES.get('file')
|
|
|
|
|
+ if not file_obj:
|
|
|
|
|
+ return Response({"error": "No file provided."}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
+
|
|
|
|
|
+ try:
|
|
|
|
|
+ df = pd.read_excel(file_obj)
|
|
|
|
|
+
|
|
|
|
|
+ required_columns = {'product_type', 'attribute_name', 'is_mandatory', 'possible_values'}
|
|
|
|
|
+ if not required_columns.issubset(df.columns):
|
|
|
|
|
+ return Response({
|
|
|
|
|
+ "error": f"Missing required columns. Found: {list(df.columns)}"
|
|
|
|
|
+ }, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
+
|
|
|
|
|
+ for _, row in df.iterrows():
|
|
|
|
|
+ product_type_name = str(row['product_type']).strip()
|
|
|
|
|
+ attr_name = str(row['attribute_name']).strip()
|
|
|
|
|
+ is_mandatory = str(row['is_mandatory']).strip().lower() in ['yes', 'true', '1']
|
|
|
|
|
+ possible_values = str(row.get('possible_values', '')).strip()
|
|
|
|
|
+
|
|
|
|
|
+ # Get or create product type
|
|
|
|
|
+ product_type, _ = ProductType.objects.get_or_create(name=product_type_name)
|
|
|
|
|
+
|
|
|
|
|
+ # Get or create attribute
|
|
|
|
|
+ attribute, _ = ProductAttribute.objects.get_or_create(
|
|
|
|
|
+ product_type=product_type,
|
|
|
|
|
+ name=attr_name,
|
|
|
|
|
+ defaults={'is_mandatory': is_mandatory}
|
|
|
|
|
+ )
|
|
|
|
|
+ attribute.is_mandatory = is_mandatory
|
|
|
|
|
+ attribute.save()
|
|
|
|
|
+
|
|
|
|
|
+ # Handle possible values
|
|
|
|
|
+ AttributePossibleValue.objects.filter(attribute=attribute).delete()
|
|
|
|
|
+ if possible_values:
|
|
|
|
|
+ for val in [v.strip() for v in possible_values.split(',') if v.strip()]:
|
|
|
|
|
+ AttributePossibleValue.objects.create(attribute=attribute, value=val)
|
|
|
|
|
+
|
|
|
|
|
+ return Response({"message": "Attributes uploaded successfully."}, status=status.HTTP_201_CREATED)
|
|
|
|
|
+
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|