code quality run

This commit is contained in:
2025-09-13 21:58:54 +02:00
parent 0dfd885847
commit 3eb18ef91e
63 changed files with 1802 additions and 1289 deletions

View File

@@ -1,28 +1,35 @@
# models/api_models.py - Updated with Marketplace Support and Pydantic v2
from pydantic import BaseModel, Field, field_validator, EmailStr, ConfigDict
from typing import Optional, List
from datetime import datetime
import re
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, ConfigDict, EmailStr, Field, field_validator
# 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)")
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')
@field_validator("username")
@classmethod
def validate_username(cls, v):
if not re.match(r'^[a-zA-Z0-9_]+$', v):
raise ValueError('Username must contain only letters, numbers, or underscores')
if not re.match(r"^[a-zA-Z0-9_]+$", v):
raise ValueError(
"Username must contain only letters, numbers, or underscores"
)
return v.lower().strip()
@field_validator('password')
@field_validator("password")
@classmethod
def validate_password(cls, v):
if len(v) < 6:
raise ValueError('Password must be at least 6 characters long')
raise ValueError("Password must be at least 6 characters long")
return v
@@ -30,7 +37,7 @@ class UserLogin(BaseModel):
username: str = Field(..., description="Username")
password: str = Field(..., description="Password")
@field_validator('username')
@field_validator("username")
@classmethod
def validate_username(cls, v):
return v.strip()
@@ -58,27 +65,38 @@ class LoginResponse(BaseModel):
# NEW: Shop models
class ShopCreate(BaseModel):
shop_code: str = Field(..., min_length=3, max_length=50, description="Unique shop code (e.g., TECHSTORE)")
shop_name: str = Field(..., min_length=1, max_length=200, description="Display name of the shop")
description: Optional[str] = Field(None, max_length=2000, description="Shop description")
shop_code: str = Field(
...,
min_length=3,
max_length=50,
description="Unique shop code (e.g., TECHSTORE)",
)
shop_name: str = Field(
..., min_length=1, max_length=200, description="Display name of the shop"
)
description: Optional[str] = Field(
None, max_length=2000, description="Shop description"
)
contact_email: Optional[str] = None
contact_phone: Optional[str] = None
website: Optional[str] = None
business_address: Optional[str] = None
tax_number: Optional[str] = None
@field_validator('shop_code')
@field_validator("shop_code")
def validate_shop_code(cls, v):
# Convert to uppercase and check format
v = v.upper().strip()
if not v.replace('_', '').replace('-', '').isalnum():
raise ValueError('Shop code must be alphanumeric (underscores and hyphens allowed)')
if not v.replace("_", "").replace("-", "").isalnum():
raise ValueError(
"Shop code must be alphanumeric (underscores and hyphens allowed)"
)
return v
@field_validator('contact_email')
@field_validator("contact_email")
def validate_contact_email(cls, v):
if v and ('@' not in v or '.' not in v):
raise ValueError('Invalid email format')
if v and ("@" not in v or "." not in v):
raise ValueError("Invalid email format")
return v.lower() if v else v
@@ -91,10 +109,10 @@ class ShopUpdate(BaseModel):
business_address: Optional[str] = None
tax_number: Optional[str] = None
@field_validator('contact_email')
@field_validator("contact_email")
def validate_contact_email(cls, v):
if v and ('@' not in v or '.' not in v):
raise ValueError('Invalid email format')
if v and ("@" not in v or "." not in v):
raise ValueError("Invalid email format")
return v.lower() if v else v
@@ -172,11 +190,11 @@ 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')
@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')
raise ValueError("Field cannot be empty")
return v.strip()
@@ -195,15 +213,25 @@ class ProductResponse(ProductBase):
# NEW: Shop Product models
class ShopProductCreate(BaseModel):
product_id: str = Field(..., description="Product ID to add to shop")
shop_product_id: Optional[str] = Field(None, description="Shop's internal product ID")
shop_price: Optional[float] = Field(None, ge=0, description="Shop-specific price override")
shop_sale_price: Optional[float] = Field(None, ge=0, description="Shop-specific sale price")
shop_product_id: Optional[str] = Field(
None, description="Shop's internal product ID"
)
shop_price: Optional[float] = Field(
None, ge=0, description="Shop-specific price override"
)
shop_sale_price: Optional[float] = Field(
None, ge=0, description="Shop-specific sale price"
)
shop_currency: Optional[str] = Field(None, description="Shop-specific currency")
shop_availability: Optional[str] = Field(None, description="Shop-specific availability")
shop_availability: Optional[str] = Field(
None, description="Shop-specific availability"
)
shop_condition: Optional[str] = Field(None, description="Shop-specific condition")
is_featured: bool = Field(False, description="Featured product flag")
min_quantity: int = Field(1, ge=1, description="Minimum order quantity")
max_quantity: Optional[int] = Field(None, ge=1, description="Maximum order quantity")
max_quantity: Optional[int] = Field(
None, ge=1, description="Maximum order quantity"
)
class ShopProductResponse(BaseModel):
@@ -270,28 +298,40 @@ class StockSummaryResponse(BaseModel):
# 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)")
marketplace: str = Field(
default="Letzshop",
description="Name of the marketplace (e.g., Letzshop, Amazon, eBay)",
)
shop_code: str = Field(..., description="Shop code to associate products with")
batch_size: Optional[int] = Field(1000, gt=0, le=10000, description="Batch size for processing")
batch_size: Optional[int] = Field(
1000, gt=0, le=10000, description="Batch size for processing"
)
@field_validator('url')
@field_validator("url")
@classmethod
def validate_url(cls, v):
if not v.startswith(('http://', 'https://')):
raise ValueError('URL must start with http:// or https://')
if not v.startswith(("http://", "https://")):
raise ValueError("URL must start with http:// or https://")
return v
@field_validator('marketplace')
@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']
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()
@field_validator('shop_code')
@field_validator("shop_code")
@classmethod
def validate_shop_code(cls, v):
return v.upper().strip()