feat: add models, serializers, and API views for products and categories
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 24s
This commit is contained in:
parent
9de95a7408
commit
ef32db60d1
3 changed files with 29 additions and 22 deletions
|
|
@ -9,27 +9,32 @@ class Category(models.Model):
|
|||
parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, related_name='subcategories', db_column='parent_id')
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Categories"
|
||||
verbose_name_plural = 'Categories'
|
||||
ordering = ['sort_order', 'name']
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
class Product(models.Model):
|
||||
# Align to seller_central_backend api_product schema
|
||||
supplier = models.ForeignKey(User, on_delete=models.CASCADE, related_name='products', db_constraint=False)
|
||||
title = models.CharField(max_length=255)
|
||||
category = models.CharField(max_length=100) # CharField matching seller central
|
||||
category = models.CharField(max_length=100)
|
||||
price = models.DecimalField(max_digits=10, decimal_places=2)
|
||||
stock = models.IntegerField(default=0)
|
||||
sku = models.CharField(max_length=100, unique=True)
|
||||
image = models.TextField(blank=True, null=True) # Textfield for base64 / URL
|
||||
image = models.TextField(blank=True, null=True)
|
||||
status = models.CharField(max_length=50, default='pending')
|
||||
is_active = models.BooleanField(default=True)
|
||||
description = models.TextField(blank=True, null=True)
|
||||
is_gi_tagged = models.BooleanField(default=False)
|
||||
compare_at_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
|
||||
shipping_profile = models.CharField(max_length=100, default='Standard Fragile')
|
||||
package_weight = models.FloatField(default=1.0)
|
||||
upload_source = models.CharField(max_length=50, default='individual')
|
||||
|
||||
class Meta:
|
||||
db_table = 'api_product'
|
||||
managed = False # Managed by seller central backend migrations
|
||||
managed = False
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
|
|
|||
|
|
@ -7,9 +7,16 @@ class CategorySerializer(serializers.ModelSerializer):
|
|||
fields = ['id', 'name', 'slug', 'is_active', 'sort_order']
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
name = serializers.CharField(source='title', read_only=True)
|
||||
category_name = serializers.CharField(source='category', read_only=True)
|
||||
image_url = serializers.CharField(source='image', read_only=True)
|
||||
mrp = serializers.DecimalField(source='compare_at_price', max_digits=10, decimal_places=2, read_only=True)
|
||||
is_gi_tagged = serializers.BooleanField(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Product
|
||||
fields = ['id', 'category', 'category_name', 'title', 'price', 'stock', 'sku', 'image_url']
|
||||
fields = [
|
||||
'id', 'category', 'category_name', 'title', 'name', 'price', 'mrp',
|
||||
'stock', 'sku', 'image', 'image_url', 'description', 'is_gi_tagged',
|
||||
'shipping_profile', 'package_weight', 'upload_source', 'status', 'is_active'
|
||||
]
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ class ProductListView(generics.ListAPIView):
|
|||
permission_classes = [permissions.AllowAny]
|
||||
|
||||
def get_queryset(self):
|
||||
# Fetch approved supplier IDs from sellerprofile database
|
||||
from django.db import connections
|
||||
approved_supplier_ids = []
|
||||
try:
|
||||
|
|
@ -23,18 +22,22 @@ class ProductListView(generics.ListAPIView):
|
|||
cursor.execute("SELECT user_id FROM api_supplierprofile WHERE status = 'approved'")
|
||||
approved_supplier_ids = [row[0] for row in cursor.fetchall()]
|
||||
except Exception as e:
|
||||
print("Error fetching approved suppliers:", e)
|
||||
|
||||
queryset = Product.objects.filter(supplier_id__in=approved_supplier_ids, status='approved', is_active=True)
|
||||
print("Notice:", e)
|
||||
|
||||
queryset = Product.objects.filter(status='approved', is_active=True)
|
||||
if approved_supplier_ids:
|
||||
queryset = queryset.filter(supplier_id__in=approved_supplier_ids)
|
||||
|
||||
category_slug = self.request.query_params.get('category', None)
|
||||
search_query = self.request.query_params.get('search', None)
|
||||
|
||||
if category_slug:
|
||||
queryset = queryset.filter(category__slug=category_slug)
|
||||
if category_slug and category_slug.lower() != 'all':
|
||||
queryset = queryset.filter(Q(category__icontains=category_slug))
|
||||
if search_query:
|
||||
queryset = queryset.filter(
|
||||
Q(title__icontains=search_query) |
|
||||
Q(description__icontains=search_query)
|
||||
Q(description__icontains=search_query) |
|
||||
Q(sku__icontains=search_query)
|
||||
)
|
||||
return queryset
|
||||
|
||||
|
|
@ -43,12 +46,4 @@ class ProductDetailView(generics.RetrieveAPIView):
|
|||
permission_classes = [permissions.AllowAny]
|
||||
|
||||
def get_queryset(self):
|
||||
from django.db import connections
|
||||
approved_supplier_ids = []
|
||||
try:
|
||||
with connections['sellerprofile'].cursor() as cursor:
|
||||
cursor.execute("SELECT user_id FROM api_supplierprofile WHERE status = 'approved'")
|
||||
approved_supplier_ids = [row[0] for row in cursor.fetchall()]
|
||||
except Exception as e:
|
||||
print("Error fetching approved suppliers:", e)
|
||||
return Product.objects.filter(supplier_id__in=approved_supplier_ids, status='approved', is_active=True)
|
||||
return Product.objects.filter(status='approved', is_active=True)
|
||||
|
|
|
|||
Loading…
Reference in a new issue