marketplace refactoring
This commit is contained in:
@@ -17,7 +17,7 @@ import requests
|
||||
from sqlalchemy import literal
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.database.product import Product
|
||||
from models.database.marketplace_product import MarketplaceProduct
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -40,15 +40,15 @@ class CSVProcessor:
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
# Standard variations
|
||||
"id": "product_id",
|
||||
"ID": "product_id",
|
||||
"Product ID": "product_id",
|
||||
"id": "marketplace_product_id",
|
||||
"ID": "marketplace_product_id",
|
||||
"MarketplaceProduct ID": "marketplace_product_id",
|
||||
"name": "title",
|
||||
"Name": "title",
|
||||
"product_name": "title",
|
||||
"Product Name": "title",
|
||||
"MarketplaceProduct Name": "title",
|
||||
# Google Shopping feed standard
|
||||
"g:id": "product_id",
|
||||
"g:id": "marketplace_product_id",
|
||||
"g:title": "title",
|
||||
"g:description": "description",
|
||||
"g:link": "link",
|
||||
@@ -266,8 +266,8 @@ class CSVProcessor:
|
||||
product_data["shop_name"] = shop_name
|
||||
|
||||
# Validate required fields
|
||||
if not product_data.get("product_id"):
|
||||
logger.warning(f"Row {index}: Missing product_id, skipping")
|
||||
if not product_data.get("marketplace_product_id"):
|
||||
logger.warning(f"Row {index}: Missing marketplace_product_id, skipping")
|
||||
errors += 1
|
||||
continue
|
||||
|
||||
@@ -278,8 +278,8 @@ class CSVProcessor:
|
||||
|
||||
# Check if product exists
|
||||
existing_product = (
|
||||
db.query(Product)
|
||||
.filter(Product.product_id == literal(product_data["product_id"]))
|
||||
db.query(MarketplaceProduct)
|
||||
.filter(MarketplaceProduct.marketplace_product_id == literal(product_data["marketplace_product_id"]))
|
||||
.first()
|
||||
)
|
||||
|
||||
@@ -293,7 +293,7 @@ class CSVProcessor:
|
||||
existing_product.updated_at = datetime.now(timezone.utc)
|
||||
updated += 1
|
||||
logger.debug(
|
||||
f"Updated product {product_data['product_id']} for "
|
||||
f"Updated product {product_data['marketplace_product_id']} for "
|
||||
f"{marketplace} and shop {shop_name}"
|
||||
)
|
||||
else:
|
||||
@@ -302,13 +302,13 @@ class CSVProcessor:
|
||||
k: v
|
||||
for k, v in product_data.items()
|
||||
if k not in ["id", "created_at", "updated_at"]
|
||||
and hasattr(Product, k)
|
||||
and hasattr(MarketplaceProduct, k)
|
||||
}
|
||||
new_product = Product(**filtered_data)
|
||||
new_product = MarketplaceProduct(**filtered_data)
|
||||
db.add(new_product)
|
||||
imported += 1
|
||||
logger.debug(
|
||||
f"Imported new product {product_data['product_id']} "
|
||||
f"Imported new product {product_data['marketplace_product_id']} "
|
||||
f"for {marketplace} and shop {shop_name}"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user