code quality run
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from models.database_models import Product, Stock
|
||||
from models.api_models import ProductCreate, ProductUpdate, StockLocationResponse, StockSummaryResponse
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
from typing import Optional, List, Generator
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Generator, List, Optional
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.api_models import (ProductCreate, ProductUpdate,
|
||||
StockLocationResponse, StockSummaryResponse)
|
||||
from models.database_models import Product, Stock
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -27,7 +30,9 @@ class ProductService:
|
||||
|
||||
# Process price if provided
|
||||
if product_data.price:
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(product_data.price)
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(
|
||||
product_data.price
|
||||
)
|
||||
if parsed_price:
|
||||
product_data.price = parsed_price
|
||||
product_data.currency = currency
|
||||
@@ -58,16 +63,16 @@ class ProductService:
|
||||
return db.query(Product).filter(Product.product_id == product_id).first()
|
||||
|
||||
def get_products_with_filters(
|
||||
self,
|
||||
db: Session,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
brand: Optional[str] = None,
|
||||
category: Optional[str] = None,
|
||||
availability: Optional[str] = None,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None,
|
||||
search: Optional[str] = None
|
||||
self,
|
||||
db: Session,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
brand: Optional[str] = None,
|
||||
category: Optional[str] = None,
|
||||
availability: Optional[str] = None,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None,
|
||||
search: Optional[str] = None,
|
||||
) -> tuple[List[Product], int]:
|
||||
"""Get products with filtering and pagination"""
|
||||
query = db.query(Product)
|
||||
@@ -87,10 +92,10 @@ class ProductService:
|
||||
# Search in title, description, marketplace, and shop_name
|
||||
search_term = f"%{search}%"
|
||||
query = query.filter(
|
||||
(Product.title.ilike(search_term)) |
|
||||
(Product.description.ilike(search_term)) |
|
||||
(Product.marketplace.ilike(search_term)) |
|
||||
(Product.shop_name.ilike(search_term))
|
||||
(Product.title.ilike(search_term))
|
||||
| (Product.description.ilike(search_term))
|
||||
| (Product.marketplace.ilike(search_term))
|
||||
| (Product.shop_name.ilike(search_term))
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
@@ -98,7 +103,9 @@ class ProductService:
|
||||
|
||||
return products, total
|
||||
|
||||
def update_product(self, db: Session, product_id: str, product_update: ProductUpdate) -> Product:
|
||||
def update_product(
|
||||
self, db: Session, product_id: str, product_update: ProductUpdate
|
||||
) -> Product:
|
||||
"""Update product with validation"""
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
@@ -116,7 +123,9 @@ class ProductService:
|
||||
|
||||
# Process price if being updated
|
||||
if "price" in update_data and update_data["price"]:
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(update_data["price"])
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(
|
||||
update_data["price"]
|
||||
)
|
||||
if parsed_price:
|
||||
update_data["price"] = parsed_price
|
||||
update_data["currency"] = currency
|
||||
@@ -160,21 +169,21 @@ class ProductService:
|
||||
]
|
||||
|
||||
return StockSummaryResponse(
|
||||
gtin=gtin,
|
||||
total_quantity=total_quantity,
|
||||
locations=locations
|
||||
gtin=gtin, total_quantity=total_quantity, locations=locations
|
||||
)
|
||||
|
||||
def generate_csv_export(
|
||||
self,
|
||||
db: Session,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None
|
||||
self,
|
||||
db: Session,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None,
|
||||
) -> Generator[str, None, None]:
|
||||
"""Generate CSV export with streaming for memory efficiency"""
|
||||
# CSV header
|
||||
yield ("product_id,title,description,link,image_link,availability,price,currency,brand,"
|
||||
"gtin,marketplace,shop_name\n")
|
||||
yield (
|
||||
"product_id,title,description,link,image_link,availability,price,currency,brand,"
|
||||
"gtin,marketplace,shop_name\n"
|
||||
)
|
||||
|
||||
batch_size = 1000
|
||||
offset = 0
|
||||
@@ -194,17 +203,22 @@ class ProductService:
|
||||
|
||||
for product in products:
|
||||
# Create CSV row with marketplace fields
|
||||
row = (f'"{product.product_id}","{product.title or ""}","{product.description or ""}",'
|
||||
f'"{product.link or ""}","{product.image_link or ""}","{product.availability or ""}",'
|
||||
f'"{product.price or ""}","{product.currency or ""}","{product.brand or ""}",'
|
||||
f'"{product.gtin or ""}","{product.marketplace or ""}","{product.shop_name or ""}"\n')
|
||||
row = (
|
||||
f'"{product.product_id}","{product.title or ""}","{product.description or ""}",'
|
||||
f'"{product.link or ""}","{product.image_link or ""}","{product.availability or ""}",'
|
||||
f'"{product.price or ""}","{product.currency or ""}","{product.brand or ""}",'
|
||||
f'"{product.gtin or ""}","{product.marketplace or ""}","{product.shop_name or ""}"\n'
|
||||
)
|
||||
yield row
|
||||
|
||||
offset += batch_size
|
||||
|
||||
def product_exists(self, db: Session, product_id: str) -> bool:
|
||||
"""Check if product exists by ID"""
|
||||
return db.query(Product).filter(Product.product_id == product_id).first() is not None
|
||||
return (
|
||||
db.query(Product).filter(Product.product_id == product_id).first()
|
||||
is not None
|
||||
)
|
||||
|
||||
|
||||
# Create service instance
|
||||
|
||||
Reference in New Issue
Block a user