claude fix: improve product scraping and debugging features
This commit is contained in:
@@ -38,6 +38,58 @@ def create_product(db: Session, data: schemas.ProductCreate) -> models.Product:
|
||||
return product
|
||||
|
||||
|
||||
def create_product_with_snapshot(
|
||||
db: Session, data: schemas.ProductCreateWithSnapshot
|
||||
) -> models.Product:
|
||||
"""Crée un produit avec un snapshot initial (données issues de la prévisualisation)."""
|
||||
data_dict = data.model_dump()
|
||||
|
||||
# Extraire les champs du snapshot
|
||||
snapshot_fields = [
|
||||
"prix_actuel",
|
||||
"prix_conseille",
|
||||
"prix_min_30j",
|
||||
"etat_stock",
|
||||
"en_stock",
|
||||
"note",
|
||||
"nombre_avis",
|
||||
"prime",
|
||||
"choix_amazon",
|
||||
"offre_limitee",
|
||||
"exclusivite_amazon",
|
||||
]
|
||||
snapshot_data = {k: data_dict.pop(k) for k in snapshot_fields if k in data_dict}
|
||||
|
||||
# Convertir les HttpUrl en strings pour SQLite
|
||||
if data_dict.get("url"):
|
||||
data_dict["url"] = str(data_dict["url"])
|
||||
if data_dict.get("url_image"):
|
||||
data_dict["url_image"] = str(data_dict["url_image"])
|
||||
|
||||
# Créer le produit
|
||||
product = models.Product(**data_dict)
|
||||
db.add(product)
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
raise
|
||||
db.refresh(product)
|
||||
|
||||
# Créer le snapshot initial si on a des données
|
||||
has_snapshot_data = any(v is not None for v in snapshot_data.values())
|
||||
if has_snapshot_data:
|
||||
snapshot = models.ProductSnapshot(
|
||||
produit_id=product.id,
|
||||
statut_scrap="preview",
|
||||
**snapshot_data,
|
||||
)
|
||||
db.add(snapshot)
|
||||
db.commit()
|
||||
|
||||
return product
|
||||
|
||||
|
||||
def update_product(db: Session, product: models.Product, changes: schemas.ProductUpdate) -> models.Product:
|
||||
for field, value in changes.dict(exclude_unset=True).items():
|
||||
setattr(product, field, value)
|
||||
|
||||
@@ -87,3 +87,20 @@ class ProductWithSnapshot(ProductBase):
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class ProductCreateWithSnapshot(ProductBase):
|
||||
"""Création d'un produit avec données de snapshot initiales (depuis preview)."""
|
||||
|
||||
# Données du snapshot initial
|
||||
prix_actuel: Optional[float] = None
|
||||
prix_conseille: Optional[float] = None
|
||||
prix_min_30j: Optional[float] = None
|
||||
etat_stock: Optional[str] = None
|
||||
en_stock: Optional[bool] = None
|
||||
note: Optional[float] = None
|
||||
nombre_avis: Optional[int] = None
|
||||
prime: Optional[bool] = None
|
||||
choix_amazon: Optional[bool] = None
|
||||
offre_limitee: Optional[bool] = None
|
||||
exclusivite_amazon: Optional[bool] = None
|
||||
|
||||
Reference in New Issue
Block a user