marketplace refactoring
This commit is contained in:
@@ -4,10 +4,10 @@
|
||||
# Database models (SQLAlchemy)
|
||||
from .database.base import Base
|
||||
from .database.user import User
|
||||
from .database.product import Product
|
||||
from .database.marketplace_product import MarketplaceProduct
|
||||
from .database.stock import Stock
|
||||
from .database.shop import Shop, ShopProduct
|
||||
from .database.marketplace import MarketplaceImportJob
|
||||
from .database.marketplace_import_job import MarketplaceImportJob
|
||||
|
||||
# API models (Pydantic) - import the modules, not all classes
|
||||
from . import schemas
|
||||
@@ -16,7 +16,7 @@ from . import schemas
|
||||
__all__ = [
|
||||
"Base",
|
||||
"User",
|
||||
"Product",
|
||||
"MarketplaceProduct",
|
||||
"Stock",
|
||||
"Shop",
|
||||
"ShopProduct",
|
||||
|
||||
@@ -3,15 +3,15 @@
|
||||
|
||||
from .base import Base
|
||||
from .user import User
|
||||
from .product import Product
|
||||
from .marketplace_product import MarketplaceProduct
|
||||
from .stock import Stock
|
||||
from .shop import Shop, ShopProduct
|
||||
from .marketplace import MarketplaceImportJob
|
||||
from .marketplace_import_job import MarketplaceImportJob
|
||||
|
||||
__all__ = [
|
||||
"Base",
|
||||
"User",
|
||||
"Product",
|
||||
"MarketplaceProduct",
|
||||
"Stock",
|
||||
"Shop",
|
||||
"ShopProduct",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import Column, DateTime
|
||||
|
||||
@@ -8,7 +8,7 @@ from app.core.database import Base
|
||||
class TimestampMixin:
|
||||
"""Mixin to add created_at and updated_at timestamps to models"""
|
||||
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.now(timezone.utc), nullable=False)
|
||||
updated_at = Column(
|
||||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
|
||||
DateTime, default=datetime.now(timezone.utc), onupdate=datetime.now(timezone.utc), nullable=False
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Index,
|
||||
Integer, String, Text, UniqueConstraint)
|
||||
@@ -6,9 +6,10 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class MarketplaceImportJob(Base):
|
||||
class MarketplaceImportJob(Base, TimestampMixin):
|
||||
__tablename__ = "marketplace_import_jobs"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
@@ -37,7 +38,7 @@ class MarketplaceImportJob(Base):
|
||||
error_message = Column(String)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
started_at = Column(DateTime)
|
||||
completed_at = Column(DateTime)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Index,
|
||||
Integer, String, Text, UniqueConstraint)
|
||||
@@ -6,13 +6,14 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
class MarketplaceProduct(Base, TimestampMixin):
|
||||
__tablename__ = "marketplace_products"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
product_id = Column(String, unique=True, index=True, nullable=False)
|
||||
marketplace_product_id = Column(String, unique=True, index=True, nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
link = Column(String)
|
||||
@@ -56,16 +57,11 @@ class Product(Base):
|
||||
) # 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
|
||||
)
|
||||
|
||||
# Relationship to stock (one-to-many via GTIN)
|
||||
stock_entries = relationship(
|
||||
"Stock",
|
||||
foreign_keys="Stock.gtin",
|
||||
primaryjoin="Product.gtin == Stock.gtin",
|
||||
primaryjoin="MarketplaceProduct.gtin == Stock.gtin",
|
||||
viewonly=True,
|
||||
)
|
||||
shop_products = relationship("ShopProduct", back_populates="product")
|
||||
@@ -82,6 +78,6 @@ class Product(Base):
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
f"<Product(product_id='{self.product_id}', title='{self.title}', marketplace='{self.marketplace}', "
|
||||
f"<MarketplaceProduct(marketplace_product_id='{self.marketplace_product_id}', title='{self.title}', marketplace='{self.marketplace}', "
|
||||
f"shop='{self.shop_name}')>"
|
||||
)
|
||||
@@ -6,9 +6,10 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class Shop(Base):
|
||||
class Shop(Base, TimestampMixin):
|
||||
__tablename__ = "shops"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
@@ -32,10 +33,6 @@ class Shop(Base):
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_verified = Column(Boolean, default=False)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
owner = relationship("User", back_populates="owned_shops")
|
||||
shop_products = relationship("ShopProduct", back_populates="shop")
|
||||
@@ -49,7 +46,7 @@ class ShopProduct(Base):
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
shop_id = Column(Integer, ForeignKey("shops.id"), nullable=False)
|
||||
product_id = Column(Integer, ForeignKey("products.id"), nullable=False)
|
||||
marketplace_product_id = Column(Integer, ForeignKey("marketplace_products.id"), nullable=False)
|
||||
|
||||
# Shop-specific overrides (can override the main product data)
|
||||
shop_product_id = Column(String) # Shop's internal product ID
|
||||
@@ -74,11 +71,11 @@ class ShopProduct(Base):
|
||||
|
||||
# Relationships
|
||||
shop = relationship("Shop", back_populates="shop_products")
|
||||
product = relationship("Product", back_populates="shop_products")
|
||||
product = relationship("MarketplaceProduct", back_populates="shop_products")
|
||||
|
||||
# Constraints
|
||||
__table_args__ = (
|
||||
UniqueConstraint("shop_id", "product_id", name="uq_shop_product"),
|
||||
UniqueConstraint("shop_id", "marketplace_product_id", name="uq_shop_product"),
|
||||
Index("idx_shop_product_active", "shop_id", "is_active"),
|
||||
Index("idx_shop_product_featured", "shop_id", "is_featured"),
|
||||
)
|
||||
|
||||
@@ -6,9 +6,9 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class Stock(Base):
|
||||
class Stock(Base, TimestampMixin):
|
||||
__tablename__ = "stock"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
@@ -20,11 +20,6 @@ class Stock(Base):
|
||||
reserved_quantity = Column(Integer, default=0) # For orders being processed
|
||||
shop_id = Column(Integer, ForeignKey("shops.id")) # Optional: shop-specific stock
|
||||
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
updated_at = Column(
|
||||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
|
||||
)
|
||||
|
||||
# Relationships
|
||||
shop = relationship("Shop")
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class User(Base):
|
||||
class User(Base, TimestampMixin):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
@@ -18,10 +18,6 @@ class User(Base):
|
||||
role = Column(String, nullable=False, default="user") # user, admin, shop_owner
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
last_login = Column(DateTime, nullable=True)
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
updated_at = Column(
|
||||
DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False
|
||||
)
|
||||
|
||||
# Relationships
|
||||
marketplace_import_jobs = relationship(
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
# models/api/__init__.py
|
||||
# models/schemas/__init__.py
|
||||
"""API models package - Pydantic models for request/response validation."""
|
||||
|
||||
# Import API model modules
|
||||
from . import base
|
||||
from . import auth
|
||||
from . import product
|
||||
from . import marketplace_product
|
||||
from . import stock
|
||||
from . import shop
|
||||
from . import marketplace
|
||||
from . import marketplace_import_job
|
||||
from . import stats
|
||||
|
||||
# Common imports for convenience
|
||||
@@ -16,9 +16,9 @@ from .base import * # Base Pydantic models
|
||||
__all__ = [
|
||||
"base",
|
||||
"auth",
|
||||
"product",
|
||||
"marketplace_product",
|
||||
"stock",
|
||||
"shop",
|
||||
"marketplace",
|
||||
"marketplace_import_job",
|
||||
"stats",
|
||||
]
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# marketplace.py - Keep URL validation, remove business constraints
|
||||
# models/schemas/marketplace_import_job.py - Keep URL validation, remove business constraints
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
class MarketplaceImportRequest(BaseModel):
|
||||
class MarketplaceImportJobRequest(BaseModel):
|
||||
url: str = Field(..., description="URL to CSV file from marketplace")
|
||||
marketplace: str = Field(default="Letzshop", description="Marketplace name")
|
||||
shop_code: str = Field(..., description="Shop code to associate products with")
|
||||
@@ -1,11 +1,11 @@
|
||||
# product.py - Simplified validation
|
||||
# models/schemas/marketplace_products.py - Simplified validation
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from models.schemas.stock import StockSummaryResponse
|
||||
|
||||
class ProductBase(BaseModel):
|
||||
product_id: Optional[str] = None
|
||||
class MarketplaceProductBase(BaseModel):
|
||||
marketplace_product_id: Optional[str] = None
|
||||
title: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
link: Optional[str] = None
|
||||
@@ -45,27 +45,27 @@ class ProductBase(BaseModel):
|
||||
marketplace: Optional[str] = None
|
||||
shop_name: Optional[str] = None
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
product_id: str = Field(..., description="Product identifier")
|
||||
title: str = Field(..., description="Product title")
|
||||
class MarketplaceProductCreate(MarketplaceProductBase):
|
||||
marketplace_product_id: str = Field(..., description="MarketplaceProduct identifier")
|
||||
title: str = Field(..., description="MarketplaceProduct title")
|
||||
# Removed: min_length constraints and custom validators
|
||||
# Service will handle empty string validation with proper domain exceptions
|
||||
|
||||
class ProductUpdate(ProductBase):
|
||||
class MarketplaceProductUpdate(MarketplaceProductBase):
|
||||
pass
|
||||
|
||||
class ProductResponse(ProductBase):
|
||||
class MarketplaceProductResponse(MarketplaceProductBase):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class ProductListResponse(BaseModel):
|
||||
products: List[ProductResponse]
|
||||
class MarketplaceProductListResponse(BaseModel):
|
||||
products: List[MarketplaceProductResponse]
|
||||
total: int
|
||||
skip: int
|
||||
limit: int
|
||||
|
||||
class ProductDetailResponse(BaseModel):
|
||||
product: ProductResponse
|
||||
class MarketplaceProductDetailResponse(BaseModel):
|
||||
product: MarketplaceProductResponse
|
||||
stock_info: Optional[StockSummaryResponse] = None
|
||||
@@ -3,7 +3,7 @@ import re
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
from models.schemas.product import ProductResponse
|
||||
from models.schemas.marketplace_product import MarketplaceProductResponse
|
||||
|
||||
class ShopCreate(BaseModel):
|
||||
shop_code: str = Field(..., description="Unique shop identifier")
|
||||
@@ -64,7 +64,7 @@ class ShopListResponse(BaseModel):
|
||||
limit: int
|
||||
|
||||
class ShopProductCreate(BaseModel):
|
||||
product_id: str = Field(..., description="Product ID to add to shop")
|
||||
marketplace_product_id: str = Field(..., description="MarketplaceProduct ID to add to shop")
|
||||
shop_product_id: Optional[str] = None
|
||||
shop_price: Optional[float] = None # Removed: ge=0 constraint
|
||||
shop_sale_price: Optional[float] = None # Removed: ge=0 constraint
|
||||
@@ -80,7 +80,7 @@ class ShopProductResponse(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
id: int
|
||||
shop_id: int
|
||||
product: ProductResponse
|
||||
product: MarketplaceProductResponse
|
||||
shop_product_id: Optional[str]
|
||||
shop_price: Optional[float]
|
||||
shop_sale_price: Optional[float]
|
||||
|
||||
Reference in New Issue
Block a user