Added marketplace support

This commit is contained in:
2025-09-05 22:14:52 +02:00
parent 9dd177bddc
commit 4fb67e594d
6 changed files with 1307 additions and 117 deletions

View File

@@ -1,4 +1,4 @@
# models/api_models.py
# models/api_models.py - Updated with Marketplace Support
from pydantic import BaseModel, Field, field_validator, EmailStr
from typing import Optional, List
from datetime import datetime
@@ -55,7 +55,7 @@ class LoginResponse(BaseModel):
user: UserResponse
# Base Product Models
# Base Product Models with Marketplace Support
class ProductBase(BaseModel):
product_id: Optional[str] = None
title: Optional[str] = None
@@ -94,6 +94,9 @@ class ProductBase(BaseModel):
identifier_exists: Optional[str] = None
shipping: Optional[str] = None
currency: Optional[str] = None
# New marketplace fields
marketplace: Optional[str] = None
shop_name: Optional[str] = None
class ProductCreate(ProductBase):
@@ -161,9 +164,11 @@ class StockSummaryResponse(BaseModel):
product_title: Optional[str] = None
# Import Models
class CSVImportRequest(BaseModel):
url: str = Field(..., description="URL to CSV file")
# Marketplace Import Models
class MarketplaceImportRequest(BaseModel):
url: str = Field(..., description="URL to CSV file from marketplace")
marketplace: str = Field(default="Letzshop", description="Name of the marketplace (e.g., Letzshop, Amazon, eBay)")
shop_name: str = Field(..., min_length=1, description="Name of the shop these products belong to")
batch_size: Optional[int] = Field(1000, gt=0, le=10000, description="Batch size for processing")
@field_validator('url')
@@ -173,10 +178,29 @@ class CSVImportRequest(BaseModel):
raise ValueError('URL must start with http:// or https://')
return v
@field_validator('marketplace')
@classmethod
def validate_marketplace(cls, v):
# You can add validation for supported marketplaces here
supported_marketplaces = ['Letzshop', 'Amazon', 'eBay', 'Etsy', 'Shopify', 'Other']
if v not in supported_marketplaces:
# For now, allow any marketplace but log it
pass
return v.strip()
class ImportJobResponse(BaseModel):
@field_validator('shop_name')
@classmethod
def validate_shop_name(cls, v):
if not v or not v.strip():
raise ValueError('Shop name cannot be empty')
return v.strip()
class MarketplaceImportJobResponse(BaseModel):
job_id: int
status: str
marketplace: str
shop_name: str
message: Optional[str] = None
imported: Optional[int] = 0
updated: Optional[int] = 0
@@ -205,5 +229,14 @@ class StatsResponse(BaseModel):
total_products: int
unique_brands: int
unique_categories: int
unique_marketplaces: int = 0
unique_shops: int = 0
total_stock_entries: int = 0
total_inventory_quantity: int = 0
class MarketplaceStatsResponse(BaseModel):
marketplace: str
total_products: int
unique_shops: int
unique_brands: int

View File

@@ -1,4 +1,4 @@
# models/database_models.py
# models/database_models.py - Updated with Marketplace Support
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Index, UniqueConstraint, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
@@ -65,6 +65,11 @@ class Product(Base):
identifier_exists = Column(String)
shipping = Column(String)
currency = Column(String)
# New marketplace fields
marketplace = Column(String, index=True, nullable=True, default="Letzshop") # Index for marketplace filtering
shop_name = Column(String, index=True, nullable=True) # Index for shop filtering
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
@@ -72,8 +77,14 @@ class Product(Base):
stock_entries = relationship("Stock", foreign_keys="Stock.gtin", primaryjoin="Product.gtin == Stock.gtin",
viewonly=True)
# Additional indexes for marketplace queries
__table_args__ = (
Index('idx_marketplace_shop', 'marketplace', 'shop_name'), # Composite index for marketplace+shop queries
Index('idx_marketplace_brand', 'marketplace', 'brand'), # Composite index for marketplace+brand queries
)
def __repr__(self):
return f"<Product(product_id='{self.product_id}', title='{self.title}')>"
return f"<Product(product_id='{self.product_id}', title='{self.title}', marketplace='{self.marketplace}', shop='{self.shop_name}')>"
class Stock(Base):
@@ -96,13 +107,15 @@ class Stock(Base):
return f"<Stock(gtin='{self.gtin}', location='{self.location}', quantity={self.quantity})>"
class ImportJob(Base):
__tablename__ = "import_jobs"
class MarketplaceImportJob(Base):
__tablename__ = "marketplace_import_jobs"
id = Column(Integer, primary_key=True, index=True)
status = Column(String, nullable=False,
default="pending") # pending, processing, completed, failed, completed_with_errors
source_url = Column(String, nullable=False)
marketplace = Column(String, nullable=False, index=True, default="Letzshop") # Index for marketplace filtering
shop_name = Column(String, nullable=False, index=True) # Index for shop filtering
user_id = Column(Integer, ForeignKey('users.id')) # Foreign key to users table
imported_count = Column(Integer, default=0)
updated_count = Column(Integer, default=0)
@@ -116,5 +129,11 @@ class ImportJob(Base):
# Relationship to user
user = relationship("User", foreign_keys=[user_id])
# Additional indexes for marketplace import job queries
__table_args__ = (
Index('idx_marketplace_import_user_marketplace', 'user_id', 'marketplace'), # User's marketplace imports
Index('idx_marketplace_import_shop_status', 'shop_name', 'status'), # Shop import status
)
def __repr__(self):
return f"<ImportJob(id={self.id}, status='{self.status}', imported={self.imported_count})>"
return f"<MarketplaceImportJob(id={self.id}, marketplace='{self.marketplace}', shop='{self.shop_name}', status='{self.status}', imported={self.imported_count})>"