Initial commit
This commit is contained in:
209
models/api_models.py
Normal file
209
models/api_models.py
Normal file
@@ -0,0 +1,209 @@
|
||||
# models/api_models.py
|
||||
from pydantic import BaseModel, Field, field_validator, EmailStr
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
# User Authentication Models
|
||||
class UserRegister(BaseModel):
|
||||
email: EmailStr = Field(..., description="Valid email address")
|
||||
username: str = Field(..., min_length=3, max_length=50, description="Username (3-50 characters)")
|
||||
password: str = Field(..., min_length=6, description="Password (minimum 6 characters)")
|
||||
|
||||
@field_validator('username')
|
||||
@classmethod
|
||||
def validate_username(cls, v):
|
||||
if not v.isalnum():
|
||||
raise ValueError('Username must contain only alphanumeric characters')
|
||||
return v.lower().strip()
|
||||
|
||||
@field_validator('password')
|
||||
@classmethod
|
||||
def validate_password(cls, v):
|
||||
if len(v) < 6:
|
||||
raise ValueError('Password must be at least 6 characters long')
|
||||
return v
|
||||
|
||||
|
||||
class UserLogin(BaseModel):
|
||||
username: str = Field(..., description="Username")
|
||||
password: str = Field(..., description="Password")
|
||||
|
||||
@field_validator('username')
|
||||
@classmethod
|
||||
def validate_username(cls, v):
|
||||
return v.strip()
|
||||
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
id: int
|
||||
email: str
|
||||
username: str
|
||||
role: str
|
||||
is_active: bool
|
||||
last_login: Optional[datetime] = None
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class LoginResponse(BaseModel):
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
expires_in: int
|
||||
user: UserResponse
|
||||
|
||||
|
||||
# Base Product Models
|
||||
class ProductBase(BaseModel):
|
||||
product_id: Optional[str] = None
|
||||
title: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
link: Optional[str] = None
|
||||
image_link: Optional[str] = None
|
||||
availability: Optional[str] = None
|
||||
price: Optional[str] = None
|
||||
brand: Optional[str] = None
|
||||
gtin: Optional[str] = None
|
||||
mpn: Optional[str] = None
|
||||
condition: Optional[str] = None
|
||||
adult: Optional[str] = None
|
||||
multipack: Optional[int] = None
|
||||
is_bundle: Optional[str] = None
|
||||
age_group: Optional[str] = None
|
||||
color: Optional[str] = None
|
||||
gender: Optional[str] = None
|
||||
material: Optional[str] = None
|
||||
pattern: Optional[str] = None
|
||||
size: Optional[str] = None
|
||||
size_type: Optional[str] = None
|
||||
size_system: Optional[str] = None
|
||||
item_group_id: Optional[str] = None
|
||||
google_product_category: Optional[str] = None
|
||||
product_type: Optional[str] = None
|
||||
custom_label_0: Optional[str] = None
|
||||
custom_label_1: Optional[str] = None
|
||||
custom_label_2: Optional[str] = None
|
||||
custom_label_3: Optional[str] = None
|
||||
custom_label_4: Optional[str] = None
|
||||
additional_image_link: Optional[str] = None
|
||||
sale_price: Optional[str] = None
|
||||
unit_pricing_measure: Optional[str] = None
|
||||
unit_pricing_base_measure: Optional[str] = None
|
||||
identifier_exists: Optional[str] = None
|
||||
shipping: Optional[str] = None
|
||||
currency: Optional[str] = None
|
||||
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
product_id: str = Field(..., min_length=1, description="Product ID is required")
|
||||
title: str = Field(..., min_length=1, description="Title is required")
|
||||
|
||||
@field_validator('product_id', 'title')
|
||||
@classmethod
|
||||
def validate_required_fields(cls, v):
|
||||
if not v or not v.strip():
|
||||
raise ValueError('Field cannot be empty')
|
||||
return v.strip()
|
||||
|
||||
|
||||
class ProductUpdate(ProductBase):
|
||||
pass
|
||||
|
||||
|
||||
class ProductResponse(ProductBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
# Stock Models
|
||||
class StockBase(BaseModel):
|
||||
gtin: str = Field(..., min_length=1, description="GTIN is required")
|
||||
location: str = Field(..., min_length=1, description="Location is required")
|
||||
|
||||
|
||||
class StockCreate(StockBase):
|
||||
quantity: int = Field(ge=0, description="Quantity must be non-negative")
|
||||
|
||||
|
||||
class StockAdd(StockBase):
|
||||
quantity: int = Field(gt=0, description="Quantity to add must be positive")
|
||||
|
||||
|
||||
class StockUpdate(BaseModel):
|
||||
quantity: int = Field(ge=0, description="Quantity must be non-negative")
|
||||
|
||||
|
||||
class StockResponse(BaseModel):
|
||||
id: int
|
||||
gtin: str
|
||||
location: str
|
||||
quantity: int
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class StockLocationResponse(BaseModel):
|
||||
location: str
|
||||
quantity: int
|
||||
|
||||
|
||||
class StockSummaryResponse(BaseModel):
|
||||
gtin: str
|
||||
total_quantity: int
|
||||
locations: List[StockLocationResponse]
|
||||
product_title: Optional[str] = None
|
||||
|
||||
|
||||
# Import Models
|
||||
class CSVImportRequest(BaseModel):
|
||||
url: str = Field(..., description="URL to CSV file")
|
||||
batch_size: Optional[int] = Field(1000, gt=0, le=10000, description="Batch size for processing")
|
||||
|
||||
@field_validator('url')
|
||||
@classmethod
|
||||
def validate_url(cls, v):
|
||||
if not v.startswith(('http://', 'https://')):
|
||||
raise ValueError('URL must start with http:// or https://')
|
||||
return v
|
||||
|
||||
|
||||
class ImportJobResponse(BaseModel):
|
||||
job_id: int
|
||||
status: str
|
||||
message: Optional[str] = None
|
||||
imported: Optional[int] = 0
|
||||
updated: Optional[int] = 0
|
||||
total_processed: Optional[int] = 0
|
||||
error_count: Optional[int] = 0
|
||||
error_message: Optional[str] = None
|
||||
created_at: Optional[datetime] = None
|
||||
started_at: Optional[datetime] = None
|
||||
completed_at: Optional[datetime] = None
|
||||
|
||||
|
||||
# Response Models
|
||||
class ProductListResponse(BaseModel):
|
||||
products: List[ProductResponse]
|
||||
total: int
|
||||
skip: int
|
||||
limit: int
|
||||
|
||||
|
||||
class ProductDetailResponse(BaseModel):
|
||||
product: ProductResponse
|
||||
stock_info: Optional[StockSummaryResponse] = None
|
||||
|
||||
|
||||
class StatsResponse(BaseModel):
|
||||
total_products: int
|
||||
unique_brands: int
|
||||
unique_categories: int
|
||||
total_stock_entries: int = 0
|
||||
total_inventory_quantity: int = 0
|
||||
120
models/database_models.py
Normal file
120
models/database_models.py
Normal file
@@ -0,0 +1,120 @@
|
||||
# models/database_models.py
|
||||
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Index, UniqueConstraint, Boolean
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import relationship
|
||||
from datetime import datetime
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
username = Column(String, unique=True, index=True, nullable=False)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
role = Column(String, nullable=False, default="user") # 'admin' or 'user'
|
||||
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)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User(username='{self.username}', email='{self.email}', role='{self.role}')>"
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
product_id = Column(String, unique=True, index=True, nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
description = Column(String)
|
||||
link = Column(String)
|
||||
image_link = Column(String)
|
||||
availability = Column(String, index=True) # Index for filtering
|
||||
price = Column(String)
|
||||
brand = Column(String, index=True) # Index for filtering
|
||||
gtin = Column(String, index=True) # Index for stock lookups
|
||||
mpn = Column(String)
|
||||
condition = Column(String)
|
||||
adult = Column(String)
|
||||
multipack = Column(Integer)
|
||||
is_bundle = Column(String)
|
||||
age_group = Column(String)
|
||||
color = Column(String)
|
||||
gender = Column(String)
|
||||
material = Column(String)
|
||||
pattern = Column(String)
|
||||
size = Column(String)
|
||||
size_type = Column(String)
|
||||
size_system = Column(String)
|
||||
item_group_id = Column(String)
|
||||
google_product_category = Column(String, index=True) # Index for filtering
|
||||
product_type = Column(String)
|
||||
custom_label_0 = Column(String)
|
||||
custom_label_1 = Column(String)
|
||||
custom_label_2 = Column(String)
|
||||
custom_label_3 = Column(String)
|
||||
custom_label_4 = Column(String)
|
||||
additional_image_link = Column(String)
|
||||
sale_price = Column(String)
|
||||
unit_pricing_measure = Column(String)
|
||||
unit_pricing_base_measure = Column(String)
|
||||
identifier_exists = Column(String)
|
||||
shipping = Column(String)
|
||||
currency = Column(String)
|
||||
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",
|
||||
viewonly=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Product(product_id='{self.product_id}', title='{self.title}')>"
|
||||
|
||||
|
||||
class Stock(Base):
|
||||
__tablename__ = "stock"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
gtin = Column(String, index=True, nullable=False) # Foreign key relationship would be ideal
|
||||
location = Column(String, nullable=False, index=True)
|
||||
quantity = Column(Integer, nullable=False, default=0)
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
||||
|
||||
# Composite unique constraint to prevent duplicate GTIN-location combinations
|
||||
__table_args__ = (
|
||||
UniqueConstraint('gtin', 'location', name='uq_stock_gtin_location'),
|
||||
Index('idx_stock_gtin_location', 'gtin', 'location'), # Composite index for efficient queries
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Stock(gtin='{self.gtin}', location='{self.location}', quantity={self.quantity})>"
|
||||
|
||||
|
||||
class ImportJob(Base):
|
||||
__tablename__ = "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)
|
||||
user_id = Column(Integer, ForeignKey('users.id')) # Foreign key to users table
|
||||
imported_count = Column(Integer, default=0)
|
||||
updated_count = Column(Integer, default=0)
|
||||
error_count = Column(Integer, default=0)
|
||||
total_processed = Column(Integer, default=0)
|
||||
error_message = Column(String)
|
||||
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
started_at = Column(DateTime)
|
||||
completed_at = Column(DateTime)
|
||||
|
||||
# Relationship to user
|
||||
user = relationship("User", foreign_keys=[user_id])
|
||||
|
||||
def __repr__(self):
|
||||
return f"<ImportJob(id={self.id}, status='{self.status}', imported={self.imported_count})>"
|
||||
Reference in New Issue
Block a user