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)
|
||||
|
||||
Reference in New Issue
Block a user