All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 39s
2188 lines
100 KiB
Python
2188 lines
100 KiB
Python
import logging
|
|
import requests
|
|
import random
|
|
import secrets
|
|
import hashlib
|
|
import csv
|
|
import io
|
|
import os
|
|
import zipfile
|
|
from django.conf import settings
|
|
from django.db.models import Q
|
|
from django.http import FileResponse, Http404, HttpResponse
|
|
from datetime import date, datetime, timedelta
|
|
from django.utils import timezone
|
|
from django.contrib.auth import authenticate
|
|
from django.contrib.auth.models import User
|
|
from django.utils.text import slugify
|
|
from rest_framework import viewsets, status, generics
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework.decorators import action as drf_action
|
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
|
from rest_framework_simplejwt.tokens import RefreshToken
|
|
|
|
import json
|
|
from .models import SupplierProfile, Product, Order, ReturnRequest, Wallet, WalletTransaction, OTPRecord, BulkUploadLog, Category
|
|
from .serializers import (
|
|
UserSerializer, RegisterSerializer, ProductSerializer,
|
|
OrderSerializer, ReturnRequestSerializer, WalletSerializer, BulkUploadLogSerializer
|
|
)
|
|
from .shipping_client import shipping_client
|
|
|
|
logger = logging.getLogger('api')
|
|
|
|
class RegisterView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def post(self, request):
|
|
try:
|
|
serializer = RegisterSerializer(data=request.data)
|
|
if not serializer.is_valid():
|
|
logger.warning('Registration failed: %s', serializer.errors)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
user = serializer.save()
|
|
profile, _ = SupplierProfile.objects.get_or_create(user=user)
|
|
|
|
# Generate OTPs
|
|
otp_phone = str(random.randint(100000, 999999))
|
|
otp_email = str(random.randint(100000, 999999))
|
|
|
|
logger.info("Generated phone OTP for %s: %s", user.username, otp_phone)
|
|
logger.info("Generated email OTP for %s: %s", user.username, otp_email)
|
|
|
|
# Send OTP via WhatsApp API
|
|
whatsapp_api_url = getattr(settings, 'WHATSAPP_API_URL', 'http://betawhatsapp.tradhox.com/api/send/')
|
|
if profile.phone:
|
|
try:
|
|
payload = {
|
|
"mobile_number": profile.phone,
|
|
"otp_code": otp_phone,
|
|
"template_name": "mobile_number_verification"
|
|
}
|
|
res = requests.post(whatsapp_api_url, json=payload, timeout=5)
|
|
logger.info("Sent WhatsApp OTP to %s: status=%s", profile.phone, res.status_code)
|
|
except Exception as e:
|
|
logger.error("Failed to send WhatsApp OTP to %s: %s", profile.phone, e)
|
|
|
|
# Send Email OTP via Email Service API
|
|
email_api_url = getattr(settings, 'EMAIL_API_URL', 'https://mailservices.tradhox.com/api/send/')
|
|
if user.email:
|
|
try:
|
|
payload = {
|
|
"recipient": user.email,
|
|
"template_name": "verification",
|
|
"context": {
|
|
"otp": otp_email
|
|
}
|
|
}
|
|
res = requests.post(email_api_url, json=payload, timeout=5)
|
|
logger.info("Sent email OTP to %s: status=%s", user.email, res.status_code)
|
|
except Exception as e:
|
|
logger.error("Failed to send email OTP to %s: %s", user.email, e)
|
|
|
|
# Store OTP hashes
|
|
expiry = timezone.now() + timedelta(minutes=10)
|
|
OTPRecord.objects.create(
|
|
user=user,
|
|
type='phone',
|
|
otp_hash=hashlib.sha256(otp_phone.encode()).hexdigest(),
|
|
expires_at=expiry
|
|
)
|
|
OTPRecord.objects.create(
|
|
user=user,
|
|
type='email',
|
|
otp_hash=hashlib.sha256(otp_email.encode()).hexdigest(),
|
|
expires_at=expiry
|
|
)
|
|
|
|
# Generate tokens
|
|
refresh = RefreshToken.for_user(user)
|
|
logger.info('New supplier registered: username=%s email=%s', user.username, user.email)
|
|
return Response({
|
|
"access_token": str(refresh.access_token),
|
|
"refresh_token": str(refresh),
|
|
"user": UserSerializer(user, context={'request': request}).data,
|
|
"message": "OTP sent to phone and email"
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
except serializers.ValidationError as e:
|
|
return Response(e.detail, status=status.HTTP_400_BAD_REQUEST)
|
|
except Exception as e:
|
|
logger.error("Registration error: %s", e, exc_info=True)
|
|
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class SendEmailOtpView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def post(self, request):
|
|
email = request.data.get('email')
|
|
template_name = request.data.get('template_name', 'verification')
|
|
|
|
if not email:
|
|
return Response({'error': 'email is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# Always generate a fresh, unique 6-digit OTP code for every dispatch
|
|
otp_code = str(random.randint(100000, 999999))
|
|
|
|
user = request.user if (request.user and request.user.is_authenticated) else None
|
|
if not user:
|
|
user = User.objects.filter(email__iexact=email).first()
|
|
if not user:
|
|
user = User.objects.filter(username__iexact=email).first()
|
|
|
|
if user:
|
|
profile = SupplierProfile.objects.filter(user=user).first()
|
|
is_inactive = (not user.is_active) or (profile and profile.status in ['suspended', 'rejected', 'deactivated', 'inactive'])
|
|
if is_inactive:
|
|
logger.warning("Email OTP requested for inactive user: username=%s, status=%s, is_active=%s",
|
|
user.username, getattr(profile, 'status', None), user.is_active)
|
|
return Response({
|
|
'error': 'Your account status is inactive. Please contact Tradhox support for assistance.',
|
|
'status': 'account_inactive'
|
|
}, status=status.HTTP_403_FORBIDDEN)
|
|
|
|
if not user:
|
|
username = email.split('@')[0] if '@' in email else email
|
|
base_user = username
|
|
c = 1
|
|
while User.objects.filter(username=username).exists():
|
|
username = f"{base_user}{c}"
|
|
c += 1
|
|
user = User.objects.create_user(username=username, email=email, is_active=True)
|
|
SupplierProfile.objects.get_or_create(user=user, defaults={'store_slug': slugify(username), 'status': 'approved', 'approved_at': timezone.now()})
|
|
|
|
expiry = timezone.now() + timedelta(minutes=10)
|
|
OTPRecord.objects.create(
|
|
user=user,
|
|
type='email',
|
|
otp_hash=hashlib.sha256(str(otp_code).encode()).hexdigest(),
|
|
expires_at=expiry
|
|
)
|
|
|
|
email_api_url = getattr(settings, 'EMAIL_API_URL', 'http://localhost:8001/api/send/')
|
|
try:
|
|
payload = {
|
|
"recipient": email,
|
|
"template_name": template_name,
|
|
"context": {
|
|
"otp": str(otp_code)
|
|
},
|
|
"subject": "Your Tradhox Seller Hub Login OTP"
|
|
}
|
|
res = requests.post(email_api_url, json=payload, timeout=5)
|
|
logger.info("Sent email OTP to %s: status=%s", email, res.status_code)
|
|
except Exception as e:
|
|
logger.error("Failed to send email OTP to %s: %s", email, e)
|
|
|
|
return Response({
|
|
"status": "success",
|
|
"message": "OTP sent to email address",
|
|
"otp_code": str(otp_code)
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
def resolve_user_by_identifier(identifier):
|
|
if not identifier:
|
|
return None
|
|
raw = str(identifier).strip()
|
|
digits = ''.join(c for c in raw if c.isdigit())
|
|
phone_search = digits[-10:] if len(digits) >= 10 else None
|
|
|
|
# 1. If 10-digit phone number is found, check SupplierProfile first
|
|
if phone_search:
|
|
profile = SupplierProfile.objects.filter(phone__icontains=phone_search).first()
|
|
if profile and profile.user:
|
|
return profile.user
|
|
|
|
user_by_phone_uname = User.objects.filter(username=f'user_{phone_search}').first()
|
|
if user_by_phone_uname:
|
|
return user_by_phone_uname
|
|
|
|
# 2. Check exact username or email
|
|
user_obj = User.objects.filter(username__iexact=raw).first() or User.objects.filter(email__iexact=raw).first()
|
|
if user_obj:
|
|
return user_obj
|
|
|
|
# 3. If phone_search and user not found, create or get user_9600494039
|
|
if phone_search:
|
|
user_name_gen = f'user_{phone_search}'
|
|
user_obj = User.objects.filter(username=user_name_gen).first()
|
|
if not user_obj:
|
|
user_obj = User.objects.create_user(
|
|
username=user_name_gen,
|
|
email="",
|
|
is_active=True
|
|
)
|
|
profile, created = SupplierProfile.objects.get_or_create(
|
|
user=user_obj,
|
|
defaults={
|
|
'phone': phone_search,
|
|
'status': 'approved',
|
|
'approved_at': timezone.now(),
|
|
'store_slug': slugify(user_name_gen)
|
|
}
|
|
)
|
|
if not profile.phone:
|
|
profile.phone = phone_search
|
|
if not profile.store_slug:
|
|
profile.store_slug = slugify(user_name_gen)
|
|
profile.save()
|
|
return user_obj
|
|
|
|
return None
|
|
|
|
def dispatch_msg91_whatsapp_otp(clean_number, otp_code, template_name='mobile_number_verification'):
|
|
try:
|
|
import http.client
|
|
import json
|
|
|
|
target_number = clean_number if clean_number.startswith('91') else f"91{clean_number}"
|
|
msg91_authkey = getattr(settings, 'MSG91_AUTHKEY', '563180AluxJHIeTGn6aaf97aeP1')
|
|
integrated_number = getattr(settings, 'MSG91_INTEGRATED_NUMBER', '918610694498')
|
|
|
|
payload_dict = {
|
|
"integrated_number": integrated_number,
|
|
"content_type": "template",
|
|
"payload": {
|
|
"messaging_product": "whatsapp",
|
|
"type": "template",
|
|
"template": {
|
|
"name": template_name,
|
|
"language": {
|
|
"code": "en",
|
|
"policy": "deterministic"
|
|
},
|
|
"to_and_components": [
|
|
{
|
|
"to": [target_number],
|
|
"components": {
|
|
"body_1": {
|
|
"type": "text",
|
|
"value": str(otp_code)
|
|
},
|
|
"button_1": {
|
|
"subtype": "url",
|
|
"type": "text",
|
|
"value": str(otp_code)
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
payload = json.dumps(payload_dict)
|
|
headers = {
|
|
'accept': "application/json",
|
|
'authkey': msg91_authkey,
|
|
'content-type': "application/json"
|
|
}
|
|
|
|
conn = http.client.HTTPSConnection("control.msg91.com", timeout=8)
|
|
conn.request("POST", "/api/v5/whatsapp/whatsapp-outbound-message/bulk/", payload, headers)
|
|
res = conn.getresponse()
|
|
data = res.read().decode('utf-8', errors='ignore')
|
|
logger.info("Direct MSG91 WhatsApp OTP dispatched to %s: status=%s, resp=%s", target_number, res.status, data)
|
|
if res.status < 400:
|
|
return True, data
|
|
return False, data
|
|
except Exception as exc:
|
|
logger.error("Direct MSG91 WhatsApp OTP failed for %s: %s", clean_number, exc)
|
|
return False, str(exc)
|
|
|
|
class SendWhatsAppOtpView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def post(self, request):
|
|
mobile_number = request.data.get('mobile_number')
|
|
otp_code = request.data.get('otp_code')
|
|
template_name = request.data.get('template_name', 'mobile_number_verification')
|
|
|
|
if not mobile_number or not otp_code:
|
|
return Response({'error': 'mobile_number and otp_code are required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
digits = ''.join(c for c in str(mobile_number) if c.isdigit())
|
|
if len(digits) == 10:
|
|
clean_number = f'91{digits}'
|
|
else:
|
|
clean_number = digits
|
|
|
|
user = request.user if (request.user and request.user.is_authenticated) else None
|
|
if not user and digits:
|
|
user = resolve_user_by_identifier(digits)
|
|
|
|
if user:
|
|
profile = SupplierProfile.objects.filter(user=user).first()
|
|
is_inactive = (not user.is_active) or (profile and profile.status in ['suspended', 'rejected', 'deactivated', 'inactive'])
|
|
if is_inactive:
|
|
logger.warning("WhatsApp OTP requested for inactive user: username=%s, phone=%s, status=%s, is_active=%s",
|
|
user.username, clean_number, getattr(profile, 'status', None), user.is_active)
|
|
return Response({
|
|
'error': 'Your account status is inactive. Please contact Tradhox support for assistance.',
|
|
'status': 'account_inactive'
|
|
}, status=status.HTTP_403_FORBIDDEN)
|
|
|
|
expiry = timezone.now() + timedelta(minutes=10)
|
|
OTPRecord.objects.create(
|
|
user=user,
|
|
type='phone',
|
|
otp_hash=hashlib.sha256(str(otp_code).encode()).hexdigest(),
|
|
expires_at=expiry
|
|
)
|
|
|
|
whatsapp_api_url = getattr(settings, 'WHATSAPP_API_URL', 'http://betawhatsapp.tradhox.com/api/send/')
|
|
sent_successfully = False
|
|
res_json = None
|
|
|
|
try:
|
|
payload = {
|
|
'mobile_number': clean_number,
|
|
'otp_code': str(otp_code),
|
|
'template_name': template_name
|
|
}
|
|
res = requests.post(whatsapp_api_url, json=payload, timeout=5)
|
|
logger.info('Proxied WhatsApp OTP to %s: status=%s', clean_number, res.status_code)
|
|
if res.status_code < 400:
|
|
res_json = res.json() if (res.content and len(res.content) > 0) else {'status': 'success'}
|
|
sent_successfully = True
|
|
except Exception as e:
|
|
logger.warning('Proxy WhatsApp gateway failed: %s. Switching to direct MSG91 gateway.', e)
|
|
|
|
if not sent_successfully:
|
|
logger.info('Attempting direct MSG91 WhatsApp delivery for %s...', clean_number)
|
|
success, _ = dispatch_msg91_whatsapp_otp(clean_number, otp_code, template_name)
|
|
if success:
|
|
return Response({
|
|
'status': 'success',
|
|
'message': 'OTP dispatched successfully via WhatsApp',
|
|
'otp_code': str(otp_code)
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
return Response(res_json or {
|
|
'status': 'success',
|
|
'message': 'Dispatched via proxy fallback',
|
|
'otp_code': str(otp_code)
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
class LoginView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def post(self, request):
|
|
username = request.data.get('username')
|
|
password = request.data.get('password')
|
|
otp_code = request.data.get('otp_code')
|
|
otp_type = request.data.get('otp_type', 'phone')
|
|
|
|
if not username:
|
|
return Response({'error': 'Username or mobile number is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
user_obj = resolve_user_by_identifier(username)
|
|
resolved_username = user_obj.username if user_obj else username
|
|
|
|
auth_user = None
|
|
if otp_code:
|
|
h = hashlib.sha256(str(otp_code).encode()).hexdigest()
|
|
record = None
|
|
if user_obj:
|
|
record = OTPRecord.objects.filter(
|
|
user=user_obj,
|
|
type=otp_type,
|
|
used=False
|
|
).order_by('-created_at').first()
|
|
|
|
# Robust fallback: if user resolution was skewed by multiple accounts, check recent valid unused OTP hash
|
|
if not record:
|
|
record = OTPRecord.objects.filter(
|
|
type=otp_type,
|
|
otp_hash=h,
|
|
used=False,
|
|
expires_at__gte=timezone.now()
|
|
).order_by('-created_at').first()
|
|
if record:
|
|
user_obj = record.user
|
|
resolved_username = user_obj.username
|
|
|
|
if not record:
|
|
return Response({'error': 'Invalid or expired OTP code'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if record.attempts >= 5:
|
|
return Response({'error': 'Max OTP attempts reached. Please request a new OTP.'}, status=status.HTTP_429_TOO_MANY_REQUESTS)
|
|
|
|
if record.expires_at >= timezone.now() and record.otp_hash == h:
|
|
record.used = True
|
|
record.save()
|
|
auth_user = user_obj
|
|
else:
|
|
record.attempts += 1
|
|
record.save()
|
|
return Response({'error': 'Invalid or expired OTP code'}, status=status.HTTP_400_BAD_REQUEST)
|
|
elif password:
|
|
auth_user = authenticate(username=resolved_username, password=password)
|
|
if not auth_user:
|
|
return Response({'error': 'Invalid username/phone or password'}, status=status.HTTP_400_BAD_REQUEST)
|
|
else:
|
|
return Response({'error': 'OTP code or password is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if auth_user:
|
|
profile = getattr(auth_user, 'profile', None)
|
|
is_inactive = (not auth_user.is_active) or (profile and profile.status in ['suspended', 'rejected', 'deactivated', 'inactive'])
|
|
if is_inactive:
|
|
return Response({'error': 'Your account status is inactive. Please contact Tradhox support for assistance.'}, status=status.HTTP_403_FORBIDDEN)
|
|
remember_me = request.data.get('remember_me', True)
|
|
cookie_max_age = 2592000 if remember_me else 86400 # 30 days (30 * 86400) vs 1 day
|
|
refresh = RefreshToken.for_user(auth_user)
|
|
logger.info('Login successful: username=%s (remember_me=%s)', resolved_username, remember_me)
|
|
res = Response({
|
|
'access_token': str(refresh.access_token),
|
|
'refresh_token': str(refresh),
|
|
'user': UserSerializer(auth_user).data,
|
|
'remember_me': remember_me,
|
|
'expires_in_days': 30 if remember_me else 1
|
|
})
|
|
res.set_cookie(
|
|
'refresh_token',
|
|
str(refresh),
|
|
max_age=cookie_max_age,
|
|
httponly=True,
|
|
secure=True,
|
|
samesite='Strict'
|
|
)
|
|
return res
|
|
return Response({'error': 'Authentication failed'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class LogoutView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
logger.info('User logged out successfully')
|
|
res = Response({'success': True, 'message': 'Logged out successfully'})
|
|
res.delete_cookie('refresh_token')
|
|
return res
|
|
|
|
class VerifyOtpView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def post(self, request):
|
|
otp_type = request.data.get('type') # 'phone' or 'email'
|
|
otp_code = request.data.get('otp')
|
|
phone_param = request.data.get('phone')
|
|
email_param = request.data.get('email')
|
|
|
|
if not otp_type or not otp_code:
|
|
return Response({'error': 'type and otp are required fields'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
user_obj = request.user if (request.user and request.user.is_authenticated) else None
|
|
h = hashlib.sha256(str(otp_code).encode()).hexdigest()
|
|
|
|
if not user_obj:
|
|
if phone_param:
|
|
user_obj = resolve_user_by_identifier(phone_param)
|
|
elif email_param:
|
|
user_obj = resolve_user_by_identifier(email_param)
|
|
|
|
record = None
|
|
if user_obj:
|
|
record = OTPRecord.objects.filter(
|
|
user=user_obj,
|
|
type=otp_type,
|
|
used=False,
|
|
expires_at__gte=timezone.now()
|
|
).order_by('-created_at').first()
|
|
|
|
if not record:
|
|
record = OTPRecord.objects.filter(
|
|
type=otp_type,
|
|
otp_hash=h,
|
|
used=False,
|
|
expires_at__gte=timezone.now()
|
|
).order_by('-created_at').first()
|
|
|
|
if not record:
|
|
return Response({'verified': False, 'error': 'Invalid or expired OTP code'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if record.attempts >= 5:
|
|
return Response({'verified': False, 'error': 'Max attempts reached. Please request a new OTP.'}, status=status.HTTP_429_TOO_MANY_REQUESTS)
|
|
|
|
if record.otp_hash != h:
|
|
record.attempts += 1
|
|
record.save()
|
|
return Response({'verified': False, 'error': 'Invalid or expired OTP code'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
record.used = True
|
|
record.save()
|
|
|
|
user_obj = record.user
|
|
profile, _ = SupplierProfile.objects.get_or_create(user=user_obj)
|
|
if otp_type == 'phone':
|
|
profile.phone_verified = True
|
|
profile.status = 'phone_verified'
|
|
elif otp_type == 'email':
|
|
profile.email_verified = True
|
|
profile.status = 'email_verified'
|
|
if email_param:
|
|
user_obj.email = email_param
|
|
user_obj.save()
|
|
if not profile.submitted_at:
|
|
profile.submitted_at = timezone.now()
|
|
if not profile.business_type:
|
|
profile.business_type = 'individual'
|
|
if not profile.store_slug:
|
|
profile.store_slug = slugify(user_obj.username) if user_obj.username else f'seller-{user_obj.id}'
|
|
profile.save()
|
|
|
|
refresh = RefreshToken.for_user(user_obj)
|
|
return Response({
|
|
'verified': True,
|
|
'next_step': 'verify_email' if otp_type == 'phone' else 'complete_profile',
|
|
'access_token': str(refresh.access_token),
|
|
'refresh_token': str(refresh),
|
|
'user': UserSerializer(user_obj, context={'request': request}).data
|
|
})
|
|
|
|
class ProfileView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
return Response(UserSerializer(request.user).data)
|
|
|
|
def put(self, request):
|
|
user = request.user
|
|
profile, _ = SupplierProfile.objects.get_or_create(user=user)
|
|
|
|
# Check permissions & s3 keys
|
|
logo_key = request.data.get('logo_s3_key', profile.logo_s3_key)
|
|
if logo_key and not logo_key.startswith(f"suppliers/{user.id}/") and not logo_key.startswith("suppliers/default/") and not logo_key.startswith(f"supplierdocument/{user.id}/"):
|
|
return Response({'error': 'Invalid logo_s3_key ownership'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# Validate first-mile courier pickup serviceability before saving pickup address
|
|
new_pincode = request.data.get('pincode')
|
|
if new_pincode:
|
|
is_avail, err_msg = shipping_client.is_pincode_pickup_serviceable(new_pincode)
|
|
if not is_avail:
|
|
return Response({'error': err_msg}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
profile.phone = request.data.get('phone', profile.phone)
|
|
profile.phone_verified = request.data.get('phone_verified', profile.phone_verified)
|
|
profile.email_verified = request.data.get('email_verified', profile.email_verified)
|
|
profile.gstin = request.data.get('gstin', profile.gstin)
|
|
profile.is_gstin_verified = request.data.get('is_gstin_verified', profile.is_gstin_verified)
|
|
profile.business_type = request.data.get('business_type', profile.business_type)
|
|
profile.store_slug = request.data.get('store_slug', profile.store_slug)
|
|
|
|
categories_data = request.data.get('categories')
|
|
if categories_data is not None:
|
|
category_objs = []
|
|
for cat_name in categories_data:
|
|
cat_obj, _ = Category.objects.get_or_create(name=cat_name)
|
|
category_objs.append(cat_obj)
|
|
profile.categories.set(category_objs)
|
|
|
|
profile.aadhar_file = request.data.get('aadhar_file', profile.aadhar_file)
|
|
profile.pan_file = request.data.get('pan_file', profile.pan_file)
|
|
profile.aadhar_s3_key = request.data.get('aadhar_s3_key', profile.aadhar_s3_key)
|
|
profile.pan_s3_key = request.data.get('pan_s3_key', profile.pan_s3_key)
|
|
profile.store_name = request.data.get('store_name', profile.store_name)
|
|
profile.store_logo = request.data.get('store_logo', profile.store_logo)
|
|
profile.logo_s3_key = logo_key
|
|
profile.business_bio = request.data.get('business_bio', profile.business_bio)
|
|
profile.street = request.data.get('street', profile.street)
|
|
profile.city = request.data.get('city', profile.city)
|
|
profile.state = request.data.get('state', profile.state)
|
|
profile.pincode = request.data.get('pincode', profile.pincode)
|
|
profile.latitude = request.data.get('latitude', profile.latitude)
|
|
profile.longitude = request.data.get('longitude', profile.longitude)
|
|
|
|
# Bank Details
|
|
profile.account_holder_name = request.data.get('account_holder_name', profile.account_holder_name)
|
|
profile.account_number = request.data.get('account_number', profile.account_number)
|
|
profile.ifsc_code = request.data.get('ifsc_code', profile.ifsc_code)
|
|
profile.bank_name = request.data.get('bank_name', profile.bank_name)
|
|
profile.is_bank_verified = request.data.get('is_bank_verified', profile.is_bank_verified)
|
|
|
|
# Tax Details
|
|
profile.tax_mode = request.data.get('tax_mode', profile.tax_mode)
|
|
profile.enrolment_id = request.data.get('enrolment_id', profile.enrolment_id)
|
|
profile.legal_entity_name = request.data.get('legal_entity_name', profile.legal_entity_name)
|
|
profile.trade_name = request.data.get('trade_name', profile.trade_name)
|
|
profile.exemption_reason = request.data.get('exemption_reason', profile.exemption_reason)
|
|
|
|
# Contact & Address
|
|
profile.business_email = request.data.get('business_email', profile.business_email)
|
|
profile.support_phone = request.data.get('support_phone', profile.support_phone)
|
|
profile.address_line2 = request.data.get('address_line2', profile.address_line2)
|
|
profile.landmark = request.data.get('landmark', profile.landmark)
|
|
|
|
# Storefront
|
|
profile.banner_url = request.data.get('banner_url', profile.banner_url)
|
|
profile.banner_s3_key = request.data.get('banner_s3_key', profile.banner_s3_key)
|
|
profile.craft_lineage_years = request.data.get('craft_lineage_years', profile.craft_lineage_years)
|
|
profile.workshop_location = request.data.get('workshop_location', profile.workshop_location)
|
|
profile.artisan_avatar = request.data.get('artisan_avatar', profile.artisan_avatar)
|
|
profile.featured_tagline = request.data.get('featured_tagline', profile.featured_tagline)
|
|
if 'social_links' in request.data:
|
|
profile.social_links = request.data.get('social_links')
|
|
|
|
# Maintain approved status by default for active sellers
|
|
|
|
profile.is_profile_complete = request.data.get('is_profile_complete', profile.is_profile_complete)
|
|
if profile.is_profile_complete and not profile.submitted_at:
|
|
profile.submitted_at = timezone.now()
|
|
|
|
profile.save()
|
|
return Response(UserSerializer(user).data)
|
|
|
|
class DashboardStatsView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
user = request.user
|
|
orders = Order.objects.filter(supplier=user)
|
|
products = Product.objects.filter(supplier=user, is_active=True)
|
|
wallet, _ = Wallet.objects.get_or_create(supplier=user)
|
|
|
|
total_orders = orders.count()
|
|
total_sales = float(sum(o.total for o in orders if o.status in ['Ready to Ship', 'Shipped', 'Delivered']))
|
|
pending_orders = orders.filter(status='Pending Acceptance').count()
|
|
ready_to_ship = orders.filter(status='Ready to Ship').count()
|
|
delivered_orders = orders.filter(status='Delivered').count()
|
|
cancelled_orders = orders.filter(status__in=['Cancelled', 'Rejected']).count()
|
|
|
|
total_products = products.count()
|
|
low_stock_count = products.filter(stock__lte=5, stock__gt=0).count()
|
|
out_of_stock_count = products.filter(stock=0).count()
|
|
|
|
now = timezone.now().date()
|
|
monthly_sales = []
|
|
for i in range(5, -1, -1):
|
|
m_date = now - timedelta(days=i * 30)
|
|
month_label = m_date.strftime('%b')
|
|
m_sum = float(sum(
|
|
o.total for o in orders
|
|
if o.date and o.date.month == m_date.month and o.date.year == m_date.year and o.status in ['Ready to Ship', 'Shipped', 'Delivered']
|
|
))
|
|
monthly_sales.append({'month': month_label, 'sales': m_sum})
|
|
|
|
return Response({
|
|
'total_sales': total_sales,
|
|
'total_orders': total_orders,
|
|
'pending_orders': pending_orders,
|
|
'ready_to_ship': ready_to_ship,
|
|
'delivered_orders': delivered_orders,
|
|
'cancelled_orders': cancelled_orders,
|
|
'total_products': total_products,
|
|
'low_stock_products': low_stock_count,
|
|
'out_of_stock_products': out_of_stock_count,
|
|
'wallet_balance': float(wallet.outstanding),
|
|
'wallet_withdrawn': float(wallet.withdrawn),
|
|
'monthly_sales': monthly_sales
|
|
})
|
|
|
|
class SubmitGstinView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
gstin = request.data.get('gstin')
|
|
if not gstin or len(gstin) != 15:
|
|
return Response({'error': 'Invalid GSTIN length. Must be 15 chars.'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
profile = request.user.profile
|
|
profile.gstin = gstin
|
|
profile.is_gstin_verified = True # Simulate validation or flag for admin manual review
|
|
profile.save()
|
|
return Response({
|
|
'verified': True,
|
|
'business_name': 'ARTISAN WEAVES INDIA PRIVATE LIMITED',
|
|
'address': '12 Weaver Lane, Kanchipuram, Tamil Nadu 631502',
|
|
'gstin_status': 'Active'
|
|
})
|
|
|
|
class MockUploadView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def put(self, request):
|
|
s3_key = request.query_params.get('key')
|
|
if not s3_key:
|
|
return Response({"error": "key query param is required"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# Define local mount directory (falls back to local project relative path if not on EC2)
|
|
base_dir = os.environ.get('UPLOAD_MOUNT_DIR', '/home/ubuntu/mnt/s3files/supplierdocuments')
|
|
if not os.path.exists('/home/ubuntu/mnt/s3files/supplierdocuments') and not os.environ.get('UPLOAD_MOUNT_DIR'):
|
|
# Local fallback for local workspace testing
|
|
base_dir = os.path.join(settings.BASE_DIR, 'mnt/s3files/supplierdocuments')
|
|
|
|
file_path = os.path.join(base_dir, s3_key)
|
|
|
|
try:
|
|
# Ensure target directories exist
|
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
|
# Save raw request body to disk
|
|
with open(file_path, 'wb') as f:
|
|
f.write(request.body)
|
|
return Response({"status": "success", "message": "Document successfully stored locally"})
|
|
except Exception as e:
|
|
return Response({"error": f"Failed to save file locally: {str(e)}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
def get_document_url(s3_key, request=None):
|
|
if not s3_key:
|
|
return None
|
|
|
|
# 1. CloudFront distribution
|
|
cloudfront_domain = getattr(settings, 'AWS_CLOUDFRONT_DOMAIN', None)
|
|
if cloudfront_domain:
|
|
domain = cloudfront_domain.replace('https://', '').replace('http://', '').strip('/')
|
|
return f"https://{domain}/{s3_key}"
|
|
|
|
# 2. AWS S3 fallback
|
|
aws_access = os.environ.get('AWS_ACCESS_KEY_ID') or os.environ.get('AWS_ACCESS_KEY')
|
|
aws_secret = os.environ.get('AWS_SECRET_ACCESS_KEY') or os.environ.get('AWS_SECRET_KEY')
|
|
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME') or os.environ.get('AWS_BUCKET_NAME') or 'betasupplierdocumentstorage'
|
|
region_name = os.environ.get('AWS_REGION', 'ap-south-2')
|
|
|
|
if getattr(settings, 'USE_S3', False) or (aws_access and aws_secret):
|
|
return f"https://{bucket_name}.s3.{region_name}.amazonaws.com/{s3_key}"
|
|
|
|
# 3. Local Mock fallback
|
|
path = f"/api/profile/mock-download/?key={s3_key}"
|
|
if request:
|
|
return request.build_absolute_uri(path)
|
|
return path
|
|
|
|
class MockDownloadView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def get(self, request):
|
|
s3_key = request.query_params.get('key')
|
|
if not s3_key:
|
|
return Response({"error": "key query param is required"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
base_dir = os.environ.get('UPLOAD_MOUNT_DIR', '/home/ubuntu/mnt/s3files/supplierdocuments')
|
|
if not os.path.exists('/home/ubuntu/mnt/s3files/supplierdocuments') and not os.environ.get('UPLOAD_MOUNT_DIR'):
|
|
base_dir = os.path.join(settings.BASE_DIR, 'mnt/s3files/supplierdocuments')
|
|
|
|
file_path = os.path.join(base_dir, s3_key)
|
|
if os.path.exists(file_path):
|
|
return FileResponse(open(file_path, 'rb'))
|
|
raise Http404("File not found")
|
|
|
|
class PresignedUrlView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
file_type = request.data.get('file_type')
|
|
content_type = request.data.get('content_type', 'image/jpeg')
|
|
if not file_type:
|
|
return Response({'error': 'file_type is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# Determine file extension based on content_type
|
|
ext = 'jpg'
|
|
if 'pdf' in content_type:
|
|
ext = 'pdf'
|
|
elif 'png' in content_type:
|
|
ext = 'png'
|
|
elif 'jpeg' in content_type:
|
|
ext = 'jpeg'
|
|
elif 'jpg' in content_type:
|
|
ext = 'jpg'
|
|
elif 'mp4' in content_type:
|
|
ext = 'mp4'
|
|
elif 'quicktime' in content_type or 'mov' in content_type:
|
|
ext = 'mov'
|
|
elif 'webm' in content_type:
|
|
ext = 'webm'
|
|
elif '/' in content_type:
|
|
ext = content_type.split('/')[-1]
|
|
|
|
# Structure: supplierdocument/supplierid/products/product_id/<documentname>/versionname.ext
|
|
product_id = request.data.get('product_id') or request.data.get('sku')
|
|
version_name = f"version_{secrets.token_hex(4)}"
|
|
if product_id:
|
|
s3_key = f"supplierdocument/{request.user.id}/products/{product_id}/{file_type}/{version_name}.{ext}"
|
|
else:
|
|
s3_key = f"supplierdocument/{request.user.id}/{file_type}/{version_name}.{ext}"
|
|
|
|
aws_access = os.environ.get('AWS_ACCESS_KEY_ID') or os.environ.get('AWS_ACCESS_KEY')
|
|
aws_secret = os.environ.get('AWS_SECRET_ACCESS_KEY') or os.environ.get('AWS_SECRET_KEY')
|
|
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME') or os.environ.get('AWS_BUCKET_NAME') or 'betasupplierdocumentstorage'
|
|
region_name = os.environ.get('AWS_REGION', 'ap-south-2')
|
|
|
|
use_s3 = getattr(settings, 'USE_S3', True)
|
|
|
|
if use_s3:
|
|
try:
|
|
import boto3
|
|
from botocore.client import Config
|
|
|
|
s3_kwargs = {
|
|
'region_name': region_name,
|
|
'endpoint_url': f"https://s3.{region_name}.amazonaws.com",
|
|
'config': Config(signature_version='s3v4')
|
|
}
|
|
if aws_access and aws_secret:
|
|
s3_kwargs['aws_access_key_id'] = aws_access
|
|
s3_kwargs['aws_secret_access_key'] = aws_secret
|
|
|
|
s3_client = boto3.client('s3', **s3_kwargs)
|
|
presigned_url = s3_client.generate_presigned_url(
|
|
'put_object',
|
|
Params={'Bucket': bucket_name, 'Key': s3_key, 'ContentType': content_type},
|
|
ExpiresIn=3600
|
|
)
|
|
except Exception as e:
|
|
print("S3 presigned URL generation failed, falling back to local mock upload endpoint:", e)
|
|
presigned_url = request.build_absolute_uri(f'/api/profile/mock-upload/?key={s3_key}')
|
|
else:
|
|
presigned_url = request.build_absolute_uri(f'/api/profile/mock-upload/?key={s3_key}')
|
|
|
|
return Response({
|
|
'presigned_url': presigned_url,
|
|
's3_key': s3_key
|
|
})
|
|
|
|
class ProductViewSet(viewsets.ModelViewSet):
|
|
serializer_class = ProductSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
user = self.request.user
|
|
profile = getattr(user, 'profile', None)
|
|
supplier_ids = [user.id]
|
|
|
|
phone = getattr(profile, 'phone', None) or user.username
|
|
digits = ''.join(c for c in str(phone) if c.isdigit())
|
|
phone_search = digits[-10:] if len(digits) >= 10 else None
|
|
if phone_search:
|
|
matching_user_ids = list(User.objects.filter(
|
|
Q(username__icontains=phone_search) |
|
|
Q(profile__phone__icontains=phone_search)
|
|
).values_list('id', flat=True))
|
|
supplier_ids.extend(matching_user_ids)
|
|
|
|
qs = Product.objects.filter(supplier_id__in=set(supplier_ids), is_active=True)
|
|
category = self.request.query_params.get('category')
|
|
search = self.request.query_params.get('search')
|
|
low_stock = self.request.query_params.get('low_stock')
|
|
|
|
if category:
|
|
qs = qs.filter(category=category)
|
|
if search:
|
|
qs = qs.filter(title__icontains=search) | qs.filter(sku__icontains=search)
|
|
if low_stock == 'true':
|
|
qs = qs.filter(stock__lte=5)
|
|
return qs
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
profile = getattr(request.user, 'profile', None)
|
|
seller_pincode = getattr(profile, 'pincode', None)
|
|
if seller_pincode:
|
|
try:
|
|
is_avail, msg = shipping_client.is_pincode_pickup_serviceable(seller_pincode)
|
|
if not is_avail:
|
|
return Response(
|
|
{'error': f"Cannot list item: First-mile courier pickup is unavailable for your pickup location pincode ({seller_pincode}). Please update your pickup address in Settings to a serviceable hub location."},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
except Exception as e:
|
|
logger.warning("Serviceability check error during product create: %s", e)
|
|
|
|
try:
|
|
return super().create(request, *args, **kwargs)
|
|
except Exception as e:
|
|
logger.error("Error creating product: %s", e, exc_info=True)
|
|
return Response(
|
|
{'error': f"Could not create product: {str(e)}"},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
def perform_create(self, serializer):
|
|
serializer.save(supplier=self.request.user, status='pending', is_active=True)
|
|
|
|
def perform_update(self, serializer):
|
|
instance = self.get_object()
|
|
if instance.status == 'rejected':
|
|
serializer.save(status='pending')
|
|
else:
|
|
serializer.save()
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
instance = self.get_object()
|
|
# Soft delete logic
|
|
instance.is_active = False
|
|
instance.save()
|
|
return Response(status=status.HTTP_204_NO_CONTENT)
|
|
|
|
class BulkUploadView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
user = request.user
|
|
|
|
# Validate seller's first-mile courier pickup serviceability before processing bulk upload
|
|
profile = getattr(user, 'profile', None)
|
|
seller_pincode = getattr(profile, 'pincode', None)
|
|
if not seller_pincode:
|
|
return Response(
|
|
{'error': "Cannot upload items: Please set your store's pickup address and 6-digit pincode in Settings before listing items."},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
is_avail, msg = shipping_client.is_pincode_pickup_serviceable(seller_pincode)
|
|
if not is_avail:
|
|
return Response(
|
|
{'error': f"Cannot upload items: First-mile courier pickup is unavailable for your pickup location pincode ({seller_pincode}). Please update your pickup address in Settings to a serviceable hub location."},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
products_data = []
|
|
images_map = {}
|
|
errors_list = []
|
|
error_rows = []
|
|
error_csv_str = None
|
|
|
|
# 1. Discover uploaded file from request.FILES
|
|
uploaded_file = None
|
|
for key in ('zip_file', 'csv_file', 'file', 'catalog', 'zip', 'csv', 'upload', 'archive', 'document'):
|
|
if key in request.FILES:
|
|
uploaded_file = request.FILES[key]
|
|
break
|
|
if not uploaded_file and len(request.FILES) > 0:
|
|
uploaded_file = list(request.FILES.values())[0]
|
|
|
|
zip_file = uploaded_file if (uploaded_file and uploaded_file.name.lower().endswith('.zip')) else None
|
|
csv_file = uploaded_file if (uploaded_file and not uploaded_file.name.lower().endswith('.zip')) else None
|
|
|
|
if zip_file:
|
|
try:
|
|
with zipfile.ZipFile(zip_file) as z:
|
|
csv_found = False
|
|
for name in z.namelist():
|
|
if name.endswith('/') or name.startswith('__MACOSX') or os.path.basename(name).startswith('.'):
|
|
continue
|
|
|
|
base_name = os.path.basename(name)
|
|
file_data = z.read(name)
|
|
|
|
# If ZIP contains a CSV file, parse it as products_data
|
|
if base_name.lower().endswith('.csv') and not products_data:
|
|
csv_found = True
|
|
try:
|
|
io_str = io.StringIO(file_data.decode('utf-8', errors='ignore'))
|
|
rdr = csv.DictReader(io_str)
|
|
for row in rdr:
|
|
products_data.append(row)
|
|
except Exception as csv_err:
|
|
logger.warning("Could not parse CSV inside ZIP: %s", csv_err)
|
|
continue
|
|
|
|
# If it's an image, upload to S3 or local media and index by SKU base_name
|
|
if base_name.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
|
sku_match = os.path.splitext(base_name)[0]
|
|
# Handle naming like TRDX-001_cover.jpg -> TRDX-001
|
|
clean_sku = sku_match.split('_')[0]
|
|
|
|
use_s3 = getattr(settings, 'USE_S3', True)
|
|
if use_s3:
|
|
try:
|
|
import boto3
|
|
aws_access = os.environ.get('AWS_ACCESS_KEY_ID') or os.environ.get('AWS_ACCESS_KEY')
|
|
aws_secret = os.environ.get('AWS_SECRET_ACCESS_KEY') or os.environ.get('AWS_SECRET_KEY')
|
|
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME', 'betasupplierdocumentstorage')
|
|
region_name = os.environ.get('AWS_REGION', 'ap-south-2')
|
|
|
|
s3_kwargs = {'region_name': region_name, 'endpoint_url': f"https://s3.{region_name}.amazonaws.com"}
|
|
if aws_access and aws_secret:
|
|
s3_kwargs['aws_access_key_id'] = aws_access
|
|
s3_kwargs['aws_secret_access_key'] = aws_secret
|
|
|
|
s3_client = boto3.client('s3', **s3_kwargs)
|
|
s3_key = f"supplierdocument/{user.id}/products/{clean_sku}/cover/{base_name}"
|
|
s3_client.put_object(Bucket=bucket_name, Key=s3_key, Body=file_data, ContentType='image/jpeg')
|
|
s3_url = f"https://{bucket_name}.s3.{region_name}.amazonaws.com/{s3_key}"
|
|
images_map[sku_match] = s3_url
|
|
images_map[clean_sku] = s3_url
|
|
except Exception as s3_err:
|
|
logger.warning("Bulk ZIP S3 upload notice: %s", str(s3_err))
|
|
dest_dir = os.path.join(settings.MEDIA_ROOT, 'products', str(user.id))
|
|
os.makedirs(dest_dir, exist_ok=True)
|
|
dest_path = os.path.join(dest_dir, base_name)
|
|
with open(dest_path, 'wb') as f:
|
|
f.write(file_data)
|
|
relative_path = f"{settings.MEDIA_URL}products/{user.id}/{base_name}"
|
|
local_url = request.build_absolute_uri(relative_path)
|
|
images_map[sku_match] = local_url
|
|
images_map[clean_sku] = local_url
|
|
else:
|
|
dest_dir = os.path.join(settings.MEDIA_ROOT, 'products', str(user.id))
|
|
os.makedirs(dest_dir, exist_ok=True)
|
|
dest_path = os.path.join(dest_dir, base_name)
|
|
with open(dest_path, 'wb') as f:
|
|
f.write(file_data)
|
|
relative_path = f"{settings.MEDIA_URL}products/{user.id}/{base_name}"
|
|
local_url = request.build_absolute_uri(relative_path)
|
|
images_map[sku_match] = local_url
|
|
images_map[clean_sku] = local_url
|
|
|
|
# Check if ZIP contained a CSV catalog
|
|
if not csv_found and not csv_file:
|
|
err_msg = "Validation Error: The uploaded ZIP file does not contain a .csv catalog spreadsheet. Please include your catalog CSV spreadsheet inside the ZIP archive."
|
|
out_io = io.StringIO()
|
|
writer = csv.writer(out_io)
|
|
writer.writerow(['File_Name', 'Error_Reason'])
|
|
writer.writerow([zip_file.name, err_msg])
|
|
error_csv_str = out_io.getvalue()
|
|
|
|
batch_id = f"BATCH-{timezone.now().strftime('%Y%m%d')}-{secrets.token_hex(2).upper()}"
|
|
log_record = BulkUploadLog.objects.create(
|
|
supplier=user,
|
|
log_id=batch_id,
|
|
s3_key=zip_file.name,
|
|
total_rows=0,
|
|
rows_created=0,
|
|
rows_updated=0,
|
|
rows_skipped=1,
|
|
status='failed',
|
|
error_summary=json.dumps({'errors': [err_msg], 'error_csv': error_csv_str}),
|
|
completed_at=timezone.now()
|
|
)
|
|
return Response({
|
|
'success': False,
|
|
'error': err_msg,
|
|
'error_csv': error_csv_str,
|
|
'summary': {'total': 0, 'created': 0, 'updated': 0, 'skipped': 1, 'errors': [err_msg]},
|
|
'log': BulkUploadLogSerializer(log_record).data
|
|
}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
except Exception as e:
|
|
logger.error("Error processing bulk ZIP: %s", str(e))
|
|
return Response({'error': f'Invalid ZIP archive: {str(e)}'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# 2. Process CSV File if uploaded
|
|
if csv_file:
|
|
try:
|
|
decoded_file = csv_file.read().decode('utf-8', errors='ignore')
|
|
io_string = io.StringIO(decoded_file)
|
|
reader = csv.DictReader(io_string)
|
|
products_data = list(reader)
|
|
except Exception as e:
|
|
logger.error("Error processing CSV: %s", str(e))
|
|
return Response({'error': f'Invalid CSV template: {str(e)}'}, status=status.HTTP_400_BAD_REQUEST)
|
|
elif not products_data:
|
|
# Fallback to JSON payload if no CSV/ZIP file is uploaded
|
|
products_data = request.data.get('products', [])
|
|
|
|
# Helper function to sanitize currency numbers
|
|
def clean_num_val(val, default_val=0.0):
|
|
if val is None:
|
|
return default_val
|
|
s = str(val).replace(',', '').replace('₹', '').replace('$', '').replace('Rs.', '').replace('Rs', '').replace('INR', '').strip()
|
|
if not s:
|
|
return default_val
|
|
try:
|
|
return float(s)
|
|
except (TypeError, ValueError):
|
|
return -1.0
|
|
|
|
# 3. Create or update products with row-by-row validation
|
|
created_products = []
|
|
rows_created = 0
|
|
rows_updated = 0
|
|
rows_skipped = 0
|
|
|
|
for idx, row in enumerate(products_data, start=1):
|
|
title = (
|
|
row.get('Title') or row.get('title') or row.get('Product Title') or
|
|
row.get('Product Name') or row.get('Item Name') or row.get('Name') or
|
|
row.get('name') or row.get('Product') or row.get('product')
|
|
)
|
|
sku = (
|
|
row.get('SKU') or row.get('sku') or row.get('Item Code') or
|
|
row.get('Product Code') or row.get('Code') or row.get('code') or
|
|
row.get('SKU Code') or row.get('sku_code')
|
|
)
|
|
category = (
|
|
row.get('Category') or row.get('category') or row.get('Product Category') or
|
|
row.get('category_name') or "Handicrafts & Art"
|
|
)
|
|
desc = (
|
|
row.get('Description') or row.get('description') or row.get('Story') or
|
|
row.get('desc') or row.get('Product Description') or ''
|
|
)
|
|
|
|
raw_price = (
|
|
row.get('Price') or row.get('Price (INR)') or row.get('price') or
|
|
row.get('Selling Price') or row.get('Unit Price') or row.get('unit_price') or
|
|
row.get('Rate') or row.get('rate')
|
|
)
|
|
price_val = clean_num_val(raw_price, default_val=0.0)
|
|
|
|
raw_stock = (
|
|
row.get('Stock') or row.get('stock') or row.get('Quantity') or
|
|
row.get('qty') or row.get('Qty') or row.get('Inventory') or
|
|
row.get('inventory') or row.get('Available Stock')
|
|
)
|
|
stock_num = clean_num_val(raw_stock, default_val=0.0)
|
|
stock_val = int(stock_num) if stock_num >= 0 else -1
|
|
|
|
# Validate mandatory requirements
|
|
row_errors = []
|
|
if not title or not str(title).strip():
|
|
row_errors.append("Missing Product Title")
|
|
if not sku or not str(sku).strip():
|
|
row_errors.append("Missing SKU code")
|
|
if price_val <= 0:
|
|
row_errors.append("Invalid Price (must be a positive number > 0)")
|
|
if stock_val < 0:
|
|
row_errors.append("Stock quantity cannot be negative")
|
|
|
|
# Validate first-mile courier pickup serviceability for each product
|
|
if not seller_pincode:
|
|
row_errors.append("Missing seller pickup location pincode in profile")
|
|
elif not is_avail:
|
|
row_errors.append(f"First-mile courier pickup is unavailable for your pickup pincode ({seller_pincode})")
|
|
|
|
# Check if this row failed validation
|
|
if row_errors:
|
|
rows_skipped += 1
|
|
reason_str = "; ".join(row_errors)
|
|
errors_list.append(f"Row {idx} (SKU {sku or 'N/A'}): {reason_str}")
|
|
err_dict = dict(row)
|
|
err_dict['Row_Number'] = idx
|
|
err_dict['Error_Reason'] = reason_str
|
|
error_rows.append(err_dict)
|
|
continue
|
|
|
|
raw_mrp = (
|
|
row.get('MRP') or row.get('mrp') or row.get('Compare At Price') or
|
|
row.get('compare_at_price') or row.get('Compare_At_Price')
|
|
)
|
|
mrp_parsed = clean_num_val(raw_mrp, default_val=0.0)
|
|
compare_at_val = mrp_parsed if mrp_parsed > 0 else (price_val * 1.25)
|
|
|
|
raw_weight = row.get('Package_Weight_Kg') or row.get('package_weight') or row.get('Weight') or row.get('weight')
|
|
weight_parsed = clean_num_val(raw_weight, default_val=1.0)
|
|
weight_val = weight_parsed if weight_parsed > 0 else 1.0
|
|
|
|
gi_str = str(row.get('GI_Tagged') or row.get('is_gi_tagged') or row.get('GI Tagged') or '').strip().lower()
|
|
is_gi_tagged = gi_str in ('true', 'yes', '1', 'y')
|
|
|
|
shipping_profile = row.get('Shipping_Profile') or row.get('shipping_profile') or 'Standard Fragile'
|
|
|
|
# Determine image URL
|
|
image_url = (
|
|
images_map.get(sku) or images_map.get(sku.upper()) or
|
|
row.get('Image URL') or row.get('Image_URL') or row.get('image_url') or
|
|
row.get('image') or row.get('Image') or row.get('Image Link') or row.get('image_link')
|
|
)
|
|
if not image_url:
|
|
image_url = 'https://images.unsplash.com/photo-1544022613-e87ca75a784a?auto=format&fit=crop&q=80&w=600'
|
|
|
|
try:
|
|
# Handle unique SKU constraint across suppliers
|
|
existing_product = Product.objects.filter(sku=sku).first()
|
|
if existing_product and existing_product.supplier != user:
|
|
existing_product.supplier = user
|
|
existing_product.title = title
|
|
existing_product.category = category
|
|
existing_product.price = price_val
|
|
existing_product.compare_at_price = compare_at_val
|
|
existing_product.stock = stock_val
|
|
existing_product.image = image_url
|
|
existing_product.description = desc
|
|
existing_product.is_gi_tagged = is_gi_tagged
|
|
existing_product.package_weight = weight_val
|
|
existing_product.shipping_profile = shipping_profile
|
|
existing_product.upload_source = 'bulk'
|
|
existing_product.status = 'pending'
|
|
existing_product.is_active = True
|
|
existing_product.save()
|
|
product = existing_product
|
|
is_created = True
|
|
else:
|
|
product, is_created = Product.objects.update_or_create(
|
|
sku=sku,
|
|
supplier=user,
|
|
defaults={
|
|
'title': title,
|
|
'category': category,
|
|
'price': price_val,
|
|
'compare_at_price': compare_at_val,
|
|
'stock': stock_val,
|
|
'image': image_url,
|
|
'description': desc,
|
|
'is_gi_tagged': is_gi_tagged,
|
|
'package_weight': weight_val,
|
|
'shipping_profile': shipping_profile,
|
|
'upload_source': 'bulk',
|
|
'status': 'pending',
|
|
'is_active': True
|
|
}
|
|
)
|
|
if is_created:
|
|
rows_created += 1
|
|
else:
|
|
rows_updated += 1
|
|
created_products.append(ProductSerializer(product).data)
|
|
except Exception as row_err:
|
|
rows_skipped += 1
|
|
err_msg = str(row_err)
|
|
errors_list.append(f"Row {idx} (SKU {sku}): {err_msg}")
|
|
err_dict = dict(row)
|
|
err_dict['Row_Number'] = idx
|
|
err_dict['Error_Reason'] = err_msg
|
|
error_rows.append(err_dict)
|
|
|
|
# 4. Generate Error CSV if any errors occurred
|
|
if error_rows:
|
|
out_io = io.StringIO()
|
|
fnames = ['Row_Number', 'Title', 'SKU', 'Category', 'Price', 'MRP', 'Stock', 'Description', 'GI_Tagged', 'Package_Weight_Kg', 'Shipping_Profile', 'Error_Reason']
|
|
writer = csv.DictWriter(out_io, fieldnames=fnames, extrasaction='ignore')
|
|
writer.writeheader()
|
|
for er in error_rows:
|
|
writer.writerow(er)
|
|
error_csv_str = out_io.getvalue()
|
|
|
|
# 5. Save BulkUploadLog record
|
|
batch_id = f"BATCH-{timezone.now().strftime('%Y%m%d')}-{secrets.token_hex(2).upper()}"
|
|
source_filename = zip_file.name if zip_file else (csv_file.name if csv_file else 'Catalog_Batch.csv')
|
|
status_str = 'completed' if (rows_created + rows_updated > 0 and rows_skipped == 0) else ('partially_completed' if (rows_created + rows_updated > 0) else 'failed')
|
|
|
|
error_summary_json = None
|
|
if errors_list or error_csv_str:
|
|
error_summary_json = json.dumps({
|
|
'errors': errors_list,
|
|
'error_csv': error_csv_str
|
|
})
|
|
|
|
log_record = BulkUploadLog.objects.create(
|
|
supplier=user,
|
|
log_id=batch_id,
|
|
s3_key=source_filename,
|
|
total_rows=len(products_data),
|
|
rows_created=rows_created,
|
|
rows_updated=rows_updated,
|
|
rows_skipped=rows_skipped,
|
|
status=status_str,
|
|
error_summary=error_summary_json,
|
|
completed_at=timezone.now()
|
|
)
|
|
|
|
return Response({
|
|
'success': True if (rows_created + rows_updated > 0) else False,
|
|
'log': BulkUploadLogSerializer(log_record).data,
|
|
'products': created_products,
|
|
'error_csv': error_csv_str,
|
|
'summary': {
|
|
'total': len(products_data),
|
|
'created': rows_created,
|
|
'updated': rows_updated,
|
|
'skipped': rows_skipped,
|
|
'errors': errors_list
|
|
}
|
|
}, status=status.HTTP_200_OK if (rows_created + rows_updated > 0 or len(products_data) == 0) else status.HTTP_400_BAD_REQUEST)
|
|
|
|
class BulkUploadLogsView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
logs = BulkUploadLog.objects.filter(supplier=request.user).order_by('-uploaded_at')[:25]
|
|
serializer = BulkUploadLogSerializer(logs, many=True)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
class BulkUploadErrorReportView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
log_id = request.query_params.get('log_id')
|
|
if not log_id:
|
|
return Response({'error': 'log_id parameter is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
log_record = BulkUploadLog.objects.filter(supplier=request.user, log_id=log_id).first()
|
|
if not log_record or not log_record.error_summary:
|
|
return Response({'error': 'No error log found for this batch'}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
try:
|
|
summary_obj = json.loads(log_record.error_summary)
|
|
if isinstance(summary_obj, dict) and summary_obj.get('error_csv'):
|
|
error_csv = summary_obj['error_csv']
|
|
elif isinstance(summary_obj, list):
|
|
out_io = io.StringIO()
|
|
writer = csv.writer(out_io)
|
|
writer.writerow(['Error_Description'])
|
|
for e in summary_obj:
|
|
writer.writerow([e])
|
|
error_csv = out_io.getvalue()
|
|
else:
|
|
error_csv = f"Batch_ID,Error\r\n{log_id},{str(summary_obj)}\r\n"
|
|
except Exception:
|
|
error_csv = f"Batch_ID,Error\r\n{log_id},{str(log_record.error_summary)}\r\n"
|
|
|
|
response = HttpResponse(error_csv, content_type='text/csv')
|
|
response['Content-Disposition'] = f'attachment; filename="Error_Report_{log_id}.csv"'
|
|
return response
|
|
|
|
from rest_framework.pagination import PageNumberPagination
|
|
|
|
class StandardResultsSetPagination(PageNumberPagination):
|
|
page_size = 20
|
|
page_size_query_param = 'page_size'
|
|
max_page_size = 100
|
|
|
|
class OrderViewSet(viewsets.ModelViewSet):
|
|
serializer_class = OrderSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
pagination_class = StandardResultsSetPagination
|
|
|
|
def get_queryset(self):
|
|
user = self.request.user
|
|
profile = getattr(user, 'profile', None)
|
|
supplier_ids = [user.id]
|
|
|
|
phone = getattr(profile, 'phone', None) or user.username
|
|
digits = ''.join(c for c in str(phone) if c.isdigit())
|
|
phone_search = digits[-10:] if len(digits) >= 10 else None
|
|
if phone_search:
|
|
matching_user_ids = list(User.objects.filter(
|
|
Q(username__icontains=phone_search) |
|
|
Q(profile__phone__icontains=phone_search)
|
|
).values_list('id', flat=True))
|
|
supplier_ids.extend(matching_user_ids)
|
|
|
|
# Ensure default catalog supplier ID (26) is included if seller owns catalog products
|
|
if 26 not in supplier_ids and Product.objects.filter(supplier_id__in=supplier_ids).exists():
|
|
supplier_ids.append(26)
|
|
|
|
return Order.objects.filter(supplier_id__in=set(supplier_ids)).order_by('-id')
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def accept(self, request, pk=None):
|
|
try:
|
|
order = self.get_object()
|
|
order.status = 'Accepted'
|
|
order.save()
|
|
|
|
# Sync acceptance status to customer order table in PostgreSQL
|
|
try:
|
|
import re
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, shipping_status = %s WHERE id = %s",
|
|
['confirmed', 'Accepted', int(clean_id)]
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to sync customer order accept: {e}")
|
|
|
|
return Response(OrderSerializer(order).data)
|
|
except Exception as e:
|
|
logger.error("Accept order error: %s", e, exc_info=True)
|
|
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def ready_to_ship(self, request, pk=None):
|
|
try:
|
|
order = self.get_object()
|
|
profile = getattr(request.user, 'profile', None)
|
|
pkg_weight = request.data.get('weight') or request.data.get('package_weight') or request.data.get('actual_weight')
|
|
|
|
# Initiate delivery pickup order with multi-carrier shipping microservice
|
|
res = shipping_client.create_delivery_pickup(order, supplier_profile=profile, weight=pkg_weight)
|
|
|
|
awb = None
|
|
if 'data' in res and isinstance(res['data'], dict):
|
|
awb = res['data'].get('awb_number')
|
|
|
|
if not awb or res.get('status') == 'error':
|
|
err_msg = res.get('message') or 'Shipping service unavailable for this location'
|
|
return Response({'error': err_msg}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
assigned_carrier = (res.get('data') or {}).get('carrier') if isinstance(res.get('data'), dict) else None
|
|
if not assigned_carrier:
|
|
assigned_carrier = 'Delhivery Logistics' if str(awb).startswith(('DELHI', '179', '139', '142', '12', '14')) else 'Shadowfax Logistics'
|
|
|
|
order.carrier = assigned_carrier
|
|
order.tracking = awb
|
|
order.eta = '2-3 Days'
|
|
order.status = 'Ready to Ship'
|
|
|
|
# Auto-link or inherit active warehouse pickup request for carrier
|
|
try:
|
|
existing_pickup_order = Order.objects.filter(
|
|
supplier=order.supplier,
|
|
carrier=assigned_carrier,
|
|
pickup_id__isnull=False
|
|
).exclude(pickup_id='').order_by('-id').first()
|
|
|
|
if existing_pickup_order and existing_pickup_order.pickup_id:
|
|
order.pickup_id = existing_pickup_order.pickup_id
|
|
if existing_pickup_order.eta and 'Pickup:' in existing_pickup_order.eta:
|
|
order.eta = existing_pickup_order.eta
|
|
if existing_pickup_order.pickup_payload:
|
|
order.pickup_payload = existing_pickup_order.pickup_payload
|
|
else:
|
|
pickup_res = shipping_client.schedule_pickup(order, supplier_profile=profile)
|
|
if isinstance(pickup_res, dict) and pickup_res.get('status') == 'success':
|
|
pid = pickup_res.get('pickup_id')
|
|
if pid:
|
|
order.pickup_id = str(pid)
|
|
if pickup_res.get('pickup_date') and pickup_res.get('pickup_time'):
|
|
order.eta = f"Pickup: {pickup_res.get('pickup_date')} ({pickup_res.get('pickup_time')})"
|
|
order.pickup_payload = pickup_res
|
|
except Exception as pe:
|
|
logger.warning(f"Auto-pickup link during ready_to_ship skipped: {pe}")
|
|
|
|
order.save()
|
|
|
|
# Sync ready for pickup status, AWB, and carrier to customer order table in PostgreSQL
|
|
try:
|
|
import re
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, awb_number = %s, shipping_carrier = %s, shipping_status = %s WHERE id = %s",
|
|
['confirmed', awb, assigned_carrier, 'Ready to Ship', int(clean_id)]
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to sync customer order ready_to_ship: {e}")
|
|
|
|
data = OrderSerializer(order).data
|
|
data['shipping_details'] = res.get('data', res)
|
|
return Response(data)
|
|
except Exception as e:
|
|
logger.error("Ready to ship error: %s", e, exc_info=True)
|
|
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def ship(self, request, pk=None):
|
|
try:
|
|
order = self.get_object()
|
|
profile = getattr(request.user, 'profile', None)
|
|
pkg_weight = request.data.get('weight') or request.data.get('package_weight') or request.data.get('actual_weight')
|
|
|
|
# Initiate delivery pickup order with multi-carrier shipping microservice
|
|
res = shipping_client.create_delivery_pickup(order, supplier_profile=profile, weight=pkg_weight)
|
|
|
|
awb = None
|
|
if 'data' in res and isinstance(res['data'], dict):
|
|
awb = res['data'].get('awb_number')
|
|
|
|
if not awb or res.get('status') == 'error':
|
|
err_msg = res.get('message') or 'Shipping service unavailable for this location'
|
|
return Response({'error': err_msg}, status=status.HTTP_400_BAD_REQUEST)
|
|
assigned_carrier = (res.get('data') or {}).get('carrier') if isinstance(res.get('data'), dict) else None
|
|
if not assigned_carrier:
|
|
assigned_carrier = 'Delhivery Logistics' if str(awb).startswith(('DELHI', '179', '139', '142', '12', '14', '698')) else ('Ekart Logistics' if str(awb).startswith(('EKART', 'ELIT')) else 'Shadowfax Logistics')
|
|
|
|
order.carrier = assigned_carrier
|
|
order.tracking = awb
|
|
order.eta = '2-3 Days'
|
|
order.status = 'Shipped'
|
|
order.save()
|
|
|
|
# Sync shipped status to customer order table in PostgreSQL
|
|
try:
|
|
import re
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, awb_number = %s, shipping_carrier = %s, shipping_status = %s WHERE id = %s",
|
|
['shipped', awb, 'Shadowfax Logistics', 'Shipped', int(clean_id)]
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to sync customer order ship: {e}")
|
|
|
|
data = OrderSerializer(order).data
|
|
data['shipping_details'] = res.get('data', res)
|
|
return Response(data)
|
|
except Exception as e:
|
|
logger.error("Ship error: %s", e, exc_info=True)
|
|
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def reject(self, request, pk=None):
|
|
order = self.get_object()
|
|
if order.tracking and order.tracking not in ('Pending', 'N/A'):
|
|
try:
|
|
shipping_client.cancel_delivery_order(order.order_id, awb_number=order.tracking, reason="Seller rejected order")
|
|
except Exception as e:
|
|
logger.warning(f"Failed to trigger shipment cancellation on reject: {e}")
|
|
|
|
order.status = 'Rejected'
|
|
order.carrier = 'N/A'
|
|
order.tracking = 'N/A'
|
|
order.eta = 'N/A'
|
|
order.save()
|
|
|
|
try:
|
|
import re
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, shipping_status = %s WHERE id = %s",
|
|
['cancelled', 'Rejected', int(clean_id)]
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to sync customer order reject: {e}")
|
|
|
|
return Response(OrderSerializer(order).data)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def create_pickup(self, request, pk=None):
|
|
return self.ship(request, pk=pk)
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def track(self, request, pk=None):
|
|
order = self.get_object()
|
|
awb = request.query_params.get('awb') or order.tracking
|
|
if not awb or awb in ('Pending', 'N/A'):
|
|
awb = f"SFX{order.id}01"
|
|
res = shipping_client.track_delivery_order(awb)
|
|
|
|
# Enrich with saved rider executive assignment details if available
|
|
try:
|
|
rider_res = shipping_client.get_rider_executive_details(awb)
|
|
if isinstance(rider_res, dict) and rider_res.get('status') == 'success' and rider_res.get('data'):
|
|
r_data = rider_res.get('data')
|
|
if r_data.get('rider_name'):
|
|
if isinstance(res, dict) and 'data' in res and isinstance(res['data'], dict):
|
|
res['data']['rider_details'] = {
|
|
'name': r_data.get('rider_name'),
|
|
'phone': r_data.get('rider_phone'),
|
|
'status': r_data.get('status')
|
|
}
|
|
except Exception:
|
|
pass
|
|
|
|
# Auto-sync live status across seller and customer databases
|
|
try:
|
|
live_data = res.get('data') if isinstance(res, dict) and 'data' in res else res
|
|
if isinstance(live_data, dict):
|
|
raw_st = str(live_data.get('current_status') or '').lower()
|
|
new_status = None
|
|
cust_status = None
|
|
|
|
if raw_st in ('delivered', 'completed'):
|
|
new_status = 'Delivered'
|
|
cust_status = 'delivered'
|
|
elif raw_st in ('shipped', 'dispatched', 'in_transit', 'picked_up', 'out_for_delivery', 'hub_received'):
|
|
new_status = 'Shipped'
|
|
cust_status = 'shipped' if raw_st != 'out_for_delivery' else 'out_for_delivery'
|
|
elif raw_st in ('cancelled', 'canceled', 'rejected'):
|
|
new_status = 'Cancelled'
|
|
cust_status = 'cancelled'
|
|
|
|
if new_status and order.status != new_status:
|
|
order.status = new_status
|
|
order.save()
|
|
|
|
# Sync to customer database in PostgreSQL
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, shipping_status = %s WHERE id = %s",
|
|
[cust_status, new_status, int(clean_id)]
|
|
)
|
|
except Exception as sync_err:
|
|
logger.warning("Failed to auto-sync tracking status for order %s: %s", order.id, sync_err)
|
|
|
|
if isinstance(res, dict) and 'data' in res and isinstance(res['data'], dict):
|
|
if not res['data'].get('awb_number'):
|
|
res['data']['awb_number'] = awb
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def cancel_pickup(self, request, pk=None):
|
|
order = self.get_object()
|
|
|
|
# Disallow cancellation if package is already scanned and in transit by courier
|
|
curr_status = str(order.status or '').strip().lower()
|
|
if curr_status in ['in transit', 'in_transit', 'shipped', 'delivered', 'out for delivery']:
|
|
return Response({
|
|
'error': f'Order {order.order_id} has already been scanned and is in transit with courier ({order.carrier}). Cancellation is not permitted.'
|
|
}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
reason = request.data.get('reason', 'Seller cancelled pickup request')
|
|
res = shipping_client.cancel_delivery_order(order.order_id, awb_number=order.tracking, reason=reason)
|
|
|
|
# Always update seller order status to Cancelled
|
|
order.status = 'Cancelled'
|
|
order.carrier = 'Pending'
|
|
order.tracking = 'Pending'
|
|
order.eta = 'N/A'
|
|
order.save()
|
|
|
|
# Sync cancellation status to customer order table in PostgreSQL
|
|
try:
|
|
import re
|
|
from django.db import connections
|
|
clean_str = str(order.order_id)
|
|
parts = [p for p in clean_str.split('-') if p.isdigit()]
|
|
clean_id = parts[0] if parts else ''.join(c for c in clean_str if c.isdigit())
|
|
if clean_id:
|
|
with connections['customerprofile'].cursor() as cursor:
|
|
cursor.execute(
|
|
"UPDATE orders_order SET status = %s, shipping_status = %s WHERE id = %s",
|
|
['cancelled', 'Cancelled', int(clean_id)]
|
|
)
|
|
except Exception as e:
|
|
logger.warning(f"Failed to sync customer order cancellation: {e}")
|
|
|
|
return Response({
|
|
'status': 'success',
|
|
'message': f'Order {order.order_id} cancelled successfully',
|
|
'order': OrderSerializer(order).data,
|
|
'shipping_response': res
|
|
})
|
|
|
|
@drf_action(detail=True, methods=['get', 'post'])
|
|
def schedule_pickup(self, request, pk=None):
|
|
order = self.get_object()
|
|
carrier = order.carrier
|
|
|
|
if request.method == 'GET':
|
|
caps = shipping_client.get_pickup_capabilities(carrier)
|
|
return Response({'status': 'success', 'carrier': carrier, 'capabilities': caps})
|
|
|
|
pickup_date = request.data.get('pickup_date') or request.data.get('date')
|
|
pickup_time = request.data.get('pickup_time') or request.data.get('time') or request.data.get('slot')
|
|
package_count = request.data.get('package_count') or request.data.get('expected_package_count') or 1
|
|
notes = request.data.get('notes') or request.data.get('reason')
|
|
profile = getattr(request.user, 'profile', None)
|
|
|
|
res = shipping_client.schedule_pickup(
|
|
order,
|
|
pickup_date=pickup_date,
|
|
pickup_time=pickup_time,
|
|
package_count=package_count,
|
|
notes=notes,
|
|
supplier_profile=profile
|
|
)
|
|
|
|
if isinstance(res, dict) and (res.get('status') == 'error' or res.get('success') is False):
|
|
# Capture failure attempt in order audit history
|
|
current_payload = order.pickup_payload if isinstance(order.pickup_payload, dict) else {}
|
|
history = list(current_payload.get('history', []))
|
|
history.append({
|
|
'attempted_at': timezone.now().isoformat(),
|
|
'status': 'error',
|
|
'pickup_date': pickup_date,
|
|
'pickup_time': pickup_time,
|
|
'error': res.get('message') or res.get('error'),
|
|
'raw_response': res
|
|
})
|
|
order.pickup_payload = {
|
|
'latest_status': 'error',
|
|
'last_attempted_at': timezone.now().isoformat(),
|
|
'last_error': res.get('message') or res.get('error'),
|
|
'history': history
|
|
}
|
|
order.save(update_fields=['pickup_payload'])
|
|
return Response({
|
|
'status': 'error',
|
|
'message': res.get('message', 'Failed to schedule courier pickup'),
|
|
'data': res
|
|
}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
# Capture success details and carrier pickup_id on order
|
|
raw_res = res.get('raw_response') or {}
|
|
pickup_id = (
|
|
res.get('pickup_id')
|
|
or (res.get('data') and isinstance(res['data'], dict) and res['data'].get('pickup_id'))
|
|
or (isinstance(raw_res, dict) and (raw_res.get('pickup_id') or raw_res.get('id') or raw_res.get('pr_id') or raw_res.get('pur_id')))
|
|
)
|
|
if pickup_id:
|
|
order.pickup_id = str(pickup_id)
|
|
|
|
current_payload = order.pickup_payload if isinstance(order.pickup_payload, dict) else {}
|
|
history = list(current_payload.get('history', []))
|
|
history.append({
|
|
'scheduled_at': timezone.now().isoformat(),
|
|
'status': 'success',
|
|
'pickup_id': order.pickup_id,
|
|
'pickup_date': pickup_date,
|
|
'pickup_time': pickup_time,
|
|
'carrier': carrier,
|
|
'message': res.get('message'),
|
|
'raw_response': res
|
|
})
|
|
order.pickup_payload = {
|
|
'latest_status': 'success',
|
|
'last_scheduled_at': timezone.now().isoformat(),
|
|
'pickup_id': order.pickup_id,
|
|
'pickup_date': pickup_date,
|
|
'pickup_time': pickup_time,
|
|
'carrier': carrier,
|
|
'history': history
|
|
}
|
|
|
|
update_fields = ['pickup_payload']
|
|
if pickup_id:
|
|
update_fields.append('pickup_id')
|
|
if pickup_date and pickup_time:
|
|
order.eta = f"Pickup: {pickup_date} ({pickup_time})"
|
|
update_fields.append('eta')
|
|
|
|
order.save(update_fields=update_fields)
|
|
|
|
return Response({
|
|
'status': 'success',
|
|
'message': res.get('message', 'Pickup request submitted successfully'),
|
|
'data': res,
|
|
'order': OrderSerializer(order).data
|
|
})
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def pickup_schedule(self, request, pk=None):
|
|
order = self.get_object()
|
|
awb = request.query_params.get('awb') or request.query_params.get('tracking_id') or order.tracking
|
|
res = shipping_client.get_pickup_schedule(awb)
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def print_label(self, request, pk=None):
|
|
order = self.get_object()
|
|
profile = getattr(request.user, 'profile', None)
|
|
res = shipping_client.generate_shipping_label(order, supplier_profile=profile)
|
|
if res.get('status') == 'error':
|
|
return Response(res, status=status.HTTP_400_BAD_REQUEST)
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def label_pdf(self, request, pk=None):
|
|
order = self.get_object()
|
|
awb = order.tracking
|
|
if not awb or str(awb).strip() in ('Pending', 'N/A', ''):
|
|
return Response({"status": "error", "message": "No tracking assigned"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
token = getattr(settings, 'DELHIVERY_API_TOKEN', 'e70dfd63665335ee6e0a1094f6d34e17a126f5a6')
|
|
d_url = f"https://track.delhivery.com/api/p/packing_slip?wbns={awb}&pdf=true"
|
|
try:
|
|
from .shipping_client import format_pdf_to_4x6
|
|
res = requests.get(d_url, headers={"Authorization": f"Token {token}"}, timeout=8)
|
|
if res.status_code == 200:
|
|
pkgs = res.json().get('packages', [])
|
|
if pkgs and pkgs[0].get('pdf_encoding'):
|
|
import base64
|
|
pdf_bytes = base64.b64decode(pkgs[0]['pdf_encoding'])
|
|
pdf_4x6_bytes = format_pdf_to_4x6(pdf_bytes)
|
|
response = HttpResponse(pdf_4x6_bytes, content_type='application/pdf')
|
|
response['Content-Disposition'] = f'inline; filename="delhivery_label_4x6_{awb}.pdf"'
|
|
return response
|
|
elif pkgs and pkgs[0].get('pdf_download_link'):
|
|
s3_res = requests.get(pkgs[0]['pdf_download_link'], timeout=8)
|
|
if s3_res.status_code == 200:
|
|
pdf_4x6_bytes = format_pdf_to_4x6(s3_res.content)
|
|
response = HttpResponse(pdf_4x6_bytes, content_type='application/pdf')
|
|
response['Content-Disposition'] = f'inline; filename="delhivery_label_4x6_{awb}.pdf"'
|
|
return response
|
|
except Exception as e:
|
|
logger.error("Failed to fetch label PDF: %s", e)
|
|
return Response({"status": "error", "message": "Failed to fetch PDF from carrier"}, status=status.HTTP_502_BAD_GATEWAY)
|
|
|
|
class ReturnRequestViewSet(viewsets.ModelViewSet):
|
|
serializer_class = ReturnRequestSerializer
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get_queryset(self):
|
|
return ReturnRequest.objects.filter(supplier=self.request.user)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def action(self, request, pk=None):
|
|
ret = self.get_object()
|
|
action_type = request.data.get('action')
|
|
if action_type == 'Approved':
|
|
profile = getattr(request.user, 'profile', None)
|
|
res = shipping_client.create_customer_return_pickup(ret, supplier_profile=profile)
|
|
is_ok = res.get('status', '').lower() in ('success', 'healthy') or res.get('status_code') in (200, 201)
|
|
awb = res.get('data', {}).get('awb_number') if (is_ok and isinstance(res.get('data'), dict)) else None
|
|
ret.returning_tracking = awb or f'SFX-REV-{random.randint(100000, 999000)}'
|
|
ret.status = 'In Transit'
|
|
else:
|
|
ret.status = 'Rejected'
|
|
ret.save()
|
|
data = ReturnRequestSerializer(ret).data
|
|
return Response(data)
|
|
|
|
@drf_action(detail=True, methods=['post'])
|
|
def create_return_pickup(self, request, pk=None):
|
|
ret = self.get_object()
|
|
profile = getattr(request.user, 'profile', None)
|
|
res = shipping_client.create_customer_return_pickup(
|
|
ret,
|
|
supplier_profile=profile,
|
|
pickup_details=request.data.get('pickup_details'),
|
|
return_details=request.data.get('return_details'),
|
|
qc_details=request.data.get('qc_details')
|
|
)
|
|
is_ok = res.get('status', '').lower() in ('success', 'healthy') or res.get('status_code') in (200, 201)
|
|
if is_ok:
|
|
awb = res.get('data', {}).get('awb_number') if isinstance(res.get('data'), dict) else None
|
|
ret.returning_tracking = awb or ret.returning_tracking
|
|
ret.status = 'In Transit'
|
|
ret.save()
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def track(self, request, pk=None):
|
|
ret = self.get_object()
|
|
client_req_id = ret.return_id
|
|
res = shipping_client.track_customer_return_pickup(client_req_id)
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['put', 'post'])
|
|
def update_qc(self, request, pk=None):
|
|
ret = self.get_object()
|
|
qc_check = request.data.get('qc_check', True)
|
|
serial_number = request.data.get('serial_number')
|
|
brand_name = request.data.get('brand_name')
|
|
item_name = request.data.get('item_name') or ret.item
|
|
res = shipping_client.update_return_qc(
|
|
ret.return_id,
|
|
qc_check=qc_check,
|
|
serial_number=serial_number,
|
|
brand_name=brand_name,
|
|
item_name=item_name
|
|
)
|
|
return Response(res)
|
|
|
|
@drf_action(detail=True, methods=['get'])
|
|
def print_label(self, request, pk=None):
|
|
ret = self.get_object()
|
|
profile = getattr(request.user, 'profile', None)
|
|
res = shipping_client.generate_return_label(ret, supplier_profile=profile)
|
|
return Response(res)
|
|
|
|
class WalletView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request):
|
|
user = request.user
|
|
wallet, _ = Wallet.objects.get_or_create(supplier=user)
|
|
return Response(WalletSerializer(wallet).data)
|
|
|
|
class WithdrawView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
user = request.user
|
|
wallet, _ = Wallet.objects.get_or_create(supplier=user)
|
|
amount_str = request.data.get('amount')
|
|
try:
|
|
amount = float(amount_str)
|
|
except (TypeError, ValueError):
|
|
logger.error('Withdraw failed for user=%s: invalid amount value=%s', user.username, amount_str)
|
|
return Response({'error': 'Invalid amount'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if amount <= 0 or amount > float(wallet.outstanding):
|
|
logger.warning('Withdraw rejected for user=%s: amount=%.2f outstanding=%.2f', user.username, amount, float(wallet.outstanding))
|
|
return Response({'error': 'Insufficient funds or invalid amount'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
wallet.outstanding = float(wallet.outstanding) - amount
|
|
wallet.withdrawn = float(wallet.withdrawn) + amount
|
|
wallet.save()
|
|
|
|
tx = WalletTransaction.objects.create(
|
|
wallet=wallet,
|
|
tx_id=f"TX-{random.randint(1000, 9999)}",
|
|
date=date.today(),
|
|
amount=amount,
|
|
status='Transferred'
|
|
)
|
|
|
|
logger.info('Withdraw successful: user=%s amount=%.2f tx_id=%s', user.username, amount, tx.tx_id)
|
|
return Response(WalletSerializer(wallet).data)
|
|
|
|
import requests
|
|
from django.core.mail import send_mail
|
|
from rest_framework.parsers import MultiPartParser, FormParser
|
|
from .models import SellerKYC
|
|
|
|
class UploadEAadhaarView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
parser_classes = [MultiPartParser, FormParser]
|
|
|
|
def post(self, request):
|
|
file_obj = request.FILES.get('file')
|
|
if not file_obj:
|
|
return Response({'error': 'file is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
user = request.user
|
|
profile, _ = SupplierProfile.objects.get_or_create(user=user)
|
|
|
|
# 1. Store the file locally (mock S3) or to S3 directly
|
|
ext = 'pdf'
|
|
if file_obj.name.endswith('.xml'):
|
|
ext = 'xml'
|
|
version_name = f"version_{secrets.token_hex(4)}"
|
|
s3_key = f"supplierdocument/{user.id}/eaadhaar/{version_name}.{ext}"
|
|
|
|
aws_access = os.environ.get('AWS_ACCESS_KEY_ID') or os.environ.get('AWS_ACCESS_KEY')
|
|
aws_secret = os.environ.get('AWS_SECRET_ACCESS_KEY') or os.environ.get('AWS_SECRET_KEY')
|
|
bucket_name = os.environ.get('AWS_STORAGE_BUCKET_NAME') or os.environ.get('AWS_BUCKET_NAME') or 'betasupplierdocumentstorage'
|
|
region_name = os.environ.get('AWS_REGION', 'ap-south-2')
|
|
use_s3 = getattr(settings, 'USE_S3', False) or (aws_access and aws_secret)
|
|
|
|
if use_s3:
|
|
try:
|
|
import boto3
|
|
s3_client = boto3.client(
|
|
's3',
|
|
region_name=region_name,
|
|
aws_access_key_id=aws_access,
|
|
aws_secret_access_key=aws_secret
|
|
)
|
|
s3_client.upload_fileobj(file_obj, bucket_name, s3_key)
|
|
except Exception as e:
|
|
logger.error(f"S3 upload failed: {e}")
|
|
return Response({'error': 'Failed to upload document to storage.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
else:
|
|
base_dir = os.environ.get('UPLOAD_MOUNT_DIR', '/home/ubuntu/mnt/s3files/supplierdocuments')
|
|
if not os.path.exists('/home/ubuntu/mnt/s3files/supplierdocuments') and not os.environ.get('UPLOAD_MOUNT_DIR'):
|
|
base_dir = os.path.join(settings.BASE_DIR, 'mnt/s3files/supplierdocuments')
|
|
file_path = os.path.join(base_dir, s3_key)
|
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
|
with open(file_path, 'wb') as f:
|
|
for chunk in file_obj.chunks():
|
|
f.write(chunk)
|
|
|
|
# Update profile with s3 key
|
|
profile.aadhar_s3_key = s3_key
|
|
profile.save()
|
|
|
|
# 2. Call Surepass API
|
|
surepass_token = os.environ.get('SUREPASS_JWT_TOKEN')
|
|
if not surepass_token:
|
|
logger.warning("Surepass token not configured. Skipping actual verification.")
|
|
mock_success_data = {
|
|
"aadhaar_number": "XXXX XXXX 1234",
|
|
"name": request.data.get('full_name', "Mock User"),
|
|
"dob": request.data.get('yob', "1990"),
|
|
"gender": "M",
|
|
"address": "123, Mock Address",
|
|
"face_status": False,
|
|
"face": "",
|
|
}
|
|
kyc, _ = SellerKYC.objects.update_or_create(
|
|
supplier=user,
|
|
defaults={
|
|
'aadhaar_number': mock_success_data['aadhaar_number'],
|
|
'name': mock_success_data['name'],
|
|
'dob': mock_success_data['dob'],
|
|
'gender': mock_success_data['gender'],
|
|
'address': mock_success_data['address'],
|
|
'face_base64': mock_success_data['face'],
|
|
'verification_response': {"mock": True, "data": mock_success_data}
|
|
}
|
|
)
|
|
# Send Email
|
|
send_mail(
|
|
'Aadhaar Verification Successful',
|
|
'Your Aadhaar document has been successfully verified.',
|
|
'noreply@sellercentral.com',
|
|
[user.email],
|
|
fail_silently=True,
|
|
)
|
|
return Response({'success': True, 'kyc_data': mock_success_data})
|
|
|
|
file_obj.seek(0)
|
|
url = "https://kyc-api.surepass.io/api/v1/aadhaar/upload/eaadhaar"
|
|
headers = {'Authorization': f'Bearer {surepass_token}'}
|
|
|
|
# Build payload
|
|
payload = {}
|
|
if request.data.get('yob'):
|
|
payload['yob'] = request.data.get('yob')
|
|
if request.data.get('full_name'):
|
|
payload['full_name'] = request.data.get('full_name')
|
|
if request.data.get('base64'):
|
|
payload['base64'] = request.data.get('base64')
|
|
|
|
files = [
|
|
('file', ('eaadhaar.pdf', file_obj, 'application/pdf'))
|
|
]
|
|
|
|
try:
|
|
response = requests.request("POST", url, headers=headers, data=payload, files=files)
|
|
resp_json = response.json()
|
|
|
|
if response.status_code == 200 and resp_json.get('success'):
|
|
data = resp_json.get('data', {})
|
|
kyc, _ = SellerKYC.objects.update_or_create(
|
|
supplier=user,
|
|
defaults={
|
|
'aadhaar_number': data.get('aadhaar_number'),
|
|
'name': data.get('name'),
|
|
'dob': data.get('dob'),
|
|
'gender': data.get('gender'),
|
|
'address': data.get('address'),
|
|
'face_base64': data.get('face'),
|
|
'verification_response': resp_json
|
|
}
|
|
)
|
|
send_mail(
|
|
'Aadhaar Verification Successful',
|
|
'Your Aadhaar document has been successfully verified.',
|
|
'noreply@sellercentral.com',
|
|
[user.email],
|
|
fail_silently=True,
|
|
)
|
|
return Response({'success': True, 'kyc_data': data})
|
|
else:
|
|
return Response({
|
|
'success': False,
|
|
'error': resp_json.get('message', 'Verification failed')
|
|
}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error calling Surepass API: {e}")
|
|
return Response({'error': 'Internal error verifying Aadhaar'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
class VerifyPANView(APIView):
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
pan_number = request.data.get('id_number')
|
|
if not pan_number:
|
|
return Response({'error': 'id_number is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
user = request.user
|
|
surepass_token = os.environ.get('SUREPASS_JWT_TOKEN')
|
|
|
|
if not surepass_token:
|
|
logger.warning("Surepass token not configured. Skipping actual PAN verification.")
|
|
# Mock success for local dev without token
|
|
mock_success_data = {
|
|
"pan_number": pan_number.upper(),
|
|
"full_name": "MOCK USER NAME",
|
|
"dob": "1990-01-01",
|
|
"gender": "M",
|
|
"status": "valid",
|
|
"aadhaar_linked": True,
|
|
}
|
|
kyc, _ = SellerKYC.objects.update_or_create(
|
|
supplier=user,
|
|
defaults={
|
|
'pan_number': mock_success_data['pan_number'],
|
|
'pan_full_name': mock_success_data['full_name'],
|
|
'pan_dob': mock_success_data['dob'],
|
|
'pan_status': mock_success_data['status'],
|
|
'pan_aadhaar_linked': mock_success_data['aadhaar_linked'],
|
|
'pan_verification_response': {"mock": True, "data": mock_success_data}
|
|
}
|
|
)
|
|
return Response({'success': True, 'kyc_data': mock_success_data})
|
|
|
|
url = "https://kyc-api.surepass.io/api/v1/pan/pan-with-details-v3"
|
|
headers = {
|
|
'Authorization': f'Bearer {surepass_token}',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
payload = {
|
|
"id_number": pan_number
|
|
}
|
|
|
|
try:
|
|
response = requests.request("POST", url, headers=headers, json=payload)
|
|
resp_json = response.json()
|
|
|
|
if response.status_code == 200 and resp_json.get('success'):
|
|
data = resp_json.get('data', {})
|
|
kyc, _ = SellerKYC.objects.update_or_create(
|
|
supplier=user,
|
|
defaults={
|
|
'pan_number': data.get('pan_number'),
|
|
'pan_full_name': data.get('full_name'),
|
|
'pan_dob': data.get('dob'),
|
|
'pan_status': data.get('status'),
|
|
'pan_aadhaar_linked': data.get('aadhaar_linked', False),
|
|
'pan_verification_response': resp_json
|
|
}
|
|
)
|
|
return Response({'success': True, 'kyc_data': data})
|
|
else:
|
|
return Response({
|
|
'success': False,
|
|
'error': resp_json.get('message', 'PAN Verification failed')
|
|
}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error calling Surepass PAN API: {e}")
|
|
return Response({'error': 'Internal error verifying PAN'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
|
class ProductSearchView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
def get(self, request):
|
|
params = request.query_params.dict()
|
|
search_service_url = os.environ.get('SEARCH_SERVICE_URL', 'http://127.0.0.1:8087/api/search/')
|
|
|
|
try:
|
|
resp = requests.get(search_service_url, params=params, timeout=3.0)
|
|
if resp.status_code == 200:
|
|
return Response(resp.json(), status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.warning(f"Searchengine microservice unavailable, falling back to DB: {e}")
|
|
|
|
# Fallback to Database Querying
|
|
try:
|
|
q = params.get('q', '').strip()
|
|
category = params.get('category', '').strip()
|
|
min_price = params.get('min_price')
|
|
max_price = params.get('max_price')
|
|
is_gi_tagged = params.get('is_gi_tagged')
|
|
supplier_id = params.get('supplier_id')
|
|
|
|
qs = Product.objects.filter(is_active=True)
|
|
if q and q != '*':
|
|
qs = qs.filter(models.Q(title__icontains=q) | models.Q(description__icontains=q) | models.Q(category__icontains=q) | models.Q(sku__icontains=q))
|
|
if category:
|
|
qs = qs.filter(category__iexact=category)
|
|
if supplier_id:
|
|
qs = qs.filter(supplier_id=supplier_id)
|
|
if is_gi_tagged is not None and str(is_gi_tagged).lower() in ('true', '1'):
|
|
qs = qs.filter(is_gi_tagged=True)
|
|
if min_price:
|
|
qs = qs.filter(price__gte=float(min_price))
|
|
if max_price:
|
|
qs = qs.filter(price__lte=float(max_price))
|
|
|
|
page = int(params.get('page', 1))
|
|
per_page = int(params.get('per_page', 20))
|
|
total = qs.count()
|
|
|
|
start = (page - 1) * per_page
|
|
end = start + per_page
|
|
products = qs[start:end]
|
|
|
|
serializer = ProductSerializer(products, many=True)
|
|
return Response({
|
|
'count': total,
|
|
'page': page,
|
|
'per_page': per_page,
|
|
'total_pages': (total + per_page - 1) // per_page if per_page > 0 else 1,
|
|
'results': serializer.data,
|
|
'fallback': True
|
|
}, status=status.HTTP_200_OK)
|
|
except Exception as err:
|
|
logger.error(f"Fallback Product Search Error: {err}", exc_info=True)
|
|
return Response({'error': str(err), 'results': [], 'count': 0}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|