shop product refactoring
This commit is contained in:
@@ -5,7 +5,8 @@ from .base import Base
|
||||
from .user import User
|
||||
from .marketplace_product import MarketplaceProduct
|
||||
from .stock import Stock
|
||||
from .shop import Shop, ShopProduct
|
||||
from .shop import Shop
|
||||
from .product import Product
|
||||
from .marketplace_import_job import MarketplaceImportJob
|
||||
|
||||
__all__ = [
|
||||
@@ -14,6 +15,6 @@ __all__ = [
|
||||
"MarketplaceProduct",
|
||||
"Stock",
|
||||
"Shop",
|
||||
"ShopProduct",
|
||||
"Product",
|
||||
"MarketplaceImportJob",
|
||||
]
|
||||
|
||||
@@ -64,7 +64,7 @@ class MarketplaceProduct(Base, TimestampMixin):
|
||||
primaryjoin="MarketplaceProduct.gtin == Stock.gtin",
|
||||
viewonly=True,
|
||||
)
|
||||
shop_products = relationship("ShopProduct", back_populates="product")
|
||||
product = relationship("Product", back_populates="marketplace_product")
|
||||
|
||||
# Additional indexes for marketplace queries
|
||||
__table_args__ = (
|
||||
|
||||
48
models/database/product.py
Normal file
48
models/database/product.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Index,
|
||||
Integer, String, Text, UniqueConstraint)
|
||||
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"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
shop_id = Column(Integer, ForeignKey("shops.id"), nullable=False)
|
||||
marketplace_product_id = Column(Integer, ForeignKey("marketplace_products.id"), nullable=False)
|
||||
|
||||
# Shop-specific overrides (can override the main product data)
|
||||
product_id = Column(String) # Shop's internal product ID
|
||||
price = Column(Float) # Override main product price
|
||||
sale_price = Column(Float)
|
||||
currency = Column(String)
|
||||
availability = Column(String) # Override availability
|
||||
condition = Column(String)
|
||||
|
||||
# Shop-specific metadata
|
||||
is_featured = Column(Boolean, default=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
display_order = Column(Integer, default=0)
|
||||
|
||||
# Inventory management
|
||||
min_quantity = Column(Integer, default=1)
|
||||
max_quantity = Column(Integer)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
shop = relationship("Shop", back_populates="product")
|
||||
marketplace_product = relationship("MarketplaceProduct", back_populates="product")
|
||||
|
||||
# Constraints
|
||||
__table_args__ = (
|
||||
UniqueConstraint("shop_id", "marketplace_product_id", name="uq_product"),
|
||||
Index("idx_product_active", "shop_id", "is_active"),
|
||||
Index("idx_product_featured", "shop_id", "is_featured"),
|
||||
)
|
||||
@@ -35,47 +35,7 @@ class Shop(Base, TimestampMixin):
|
||||
|
||||
# Relationships
|
||||
owner = relationship("User", back_populates="owned_shops")
|
||||
shop_products = relationship("ShopProduct", back_populates="shop")
|
||||
product = relationship("Product", back_populates="shop")
|
||||
marketplace_import_jobs = relationship(
|
||||
"MarketplaceImportJob", back_populates="shop"
|
||||
)
|
||||
|
||||
|
||||
class ShopProduct(Base):
|
||||
__tablename__ = "shop_products"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
shop_id = Column(Integer, ForeignKey("shops.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
|
||||
shop_price = Column(Float) # Override main product price
|
||||
shop_sale_price = Column(Float)
|
||||
shop_currency = Column(String)
|
||||
shop_availability = Column(String) # Override availability
|
||||
shop_condition = Column(String)
|
||||
|
||||
# Shop-specific metadata
|
||||
is_featured = Column(Boolean, default=False)
|
||||
is_active = Column(Boolean, default=True)
|
||||
display_order = Column(Integer, default=0)
|
||||
|
||||
# Inventory management
|
||||
min_quantity = Column(Integer, default=1)
|
||||
max_quantity = Column(Integer)
|
||||
|
||||
# Timestamps
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
shop = relationship("Shop", back_populates="shop_products")
|
||||
product = relationship("MarketplaceProduct", back_populates="shop_products")
|
||||
|
||||
# Constraints
|
||||
__table_args__ = (
|
||||
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"),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user