style: apply black and isort formatting across entire codebase
- Standardize quote style (single to double quotes) - Reorder and group imports alphabetically - Fix line breaks and indentation for consistency - Apply PEP 8 formatting standards Also updated Makefile to exclude both venv and .venv from code quality checks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -9,23 +9,20 @@ This module provides:
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional
|
||||
from datetime import datetime, timezone
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.exceptions import (CartItemNotFoundException, CartValidationException,
|
||||
InsufficientInventoryForCartException,
|
||||
InvalidCartQuantityException,
|
||||
ProductNotAvailableForCartException,
|
||||
ProductNotFoundException)
|
||||
from models.database.cart import CartItem
|
||||
from models.database.product import Product
|
||||
from models.database.vendor import Vendor
|
||||
from models.database.cart import CartItem
|
||||
from app.exceptions import (
|
||||
ProductNotFoundException,
|
||||
CartItemNotFoundException,
|
||||
CartValidationException,
|
||||
InsufficientInventoryForCartException,
|
||||
InvalidCartQuantityException,
|
||||
ProductNotAvailableForCartException,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -33,12 +30,7 @@ logger = logging.getLogger(__name__)
|
||||
class CartService:
|
||||
"""Service for managing shopping carts."""
|
||||
|
||||
def get_cart(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str
|
||||
) -> Dict:
|
||||
def get_cart(self, db: Session, vendor_id: int, session_id: str) -> Dict:
|
||||
"""
|
||||
Get cart contents for a session.
|
||||
|
||||
@@ -55,20 +47,21 @@ class CartService:
|
||||
extra={
|
||||
"vendor_id": vendor_id,
|
||||
"session_id": session_id,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Fetch cart items from database
|
||||
cart_items = db.query(CartItem).filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id
|
||||
cart_items = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(CartItem.vendor_id == vendor_id, CartItem.session_id == session_id)
|
||||
)
|
||||
).all()
|
||||
.all()
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] Found {len(cart_items)} items in database",
|
||||
extra={"item_count": len(cart_items)}
|
||||
extra={"item_count": len(cart_items)},
|
||||
)
|
||||
|
||||
# Build response
|
||||
@@ -79,14 +72,20 @@ class CartService:
|
||||
product = cart_item.product
|
||||
line_total = cart_item.line_total
|
||||
|
||||
items.append({
|
||||
"product_id": product.id,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"quantity": cart_item.quantity,
|
||||
"price": cart_item.price_at_add,
|
||||
"line_total": line_total,
|
||||
"image_url": product.marketplace_product.image_link if product.marketplace_product else None,
|
||||
})
|
||||
items.append(
|
||||
{
|
||||
"product_id": product.id,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"quantity": cart_item.quantity,
|
||||
"price": cart_item.price_at_add,
|
||||
"line_total": line_total,
|
||||
"image_url": (
|
||||
product.marketplace_product.image_link
|
||||
if product.marketplace_product
|
||||
else None
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
subtotal += line_total
|
||||
|
||||
@@ -95,23 +94,23 @@ class CartService:
|
||||
"session_id": session_id,
|
||||
"items": items,
|
||||
"subtotal": subtotal,
|
||||
"total": subtotal # Could add tax/shipping later
|
||||
"total": subtotal, # Could add tax/shipping later
|
||||
}
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] get_cart returning: {len(cart_data['items'])} items, total: {cart_data['total']}",
|
||||
extra={"cart": cart_data}
|
||||
extra={"cart": cart_data},
|
||||
)
|
||||
|
||||
return cart_data
|
||||
|
||||
def add_to_cart(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int = 1
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int = 1,
|
||||
) -> Dict:
|
||||
"""
|
||||
Add product to cart.
|
||||
@@ -136,23 +135,27 @@ class CartService:
|
||||
"vendor_id": vendor_id,
|
||||
"session_id": session_id,
|
||||
"product_id": product_id,
|
||||
"quantity": quantity
|
||||
}
|
||||
"quantity": quantity,
|
||||
},
|
||||
)
|
||||
|
||||
# Verify product exists and belongs to vendor
|
||||
product = db.query(Product).filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True
|
||||
product = (
|
||||
db.query(Product)
|
||||
.filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if not product:
|
||||
logger.error(
|
||||
f"[CART_SERVICE] Product not found",
|
||||
extra={"product_id": product_id, "vendor_id": vendor_id}
|
||||
extra={"product_id": product_id, "vendor_id": vendor_id},
|
||||
)
|
||||
raise ProductNotFoundException(product_id=product_id, vendor_id=vendor_id)
|
||||
|
||||
@@ -161,21 +164,25 @@ class CartService:
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"available_inventory": product.available_inventory
|
||||
}
|
||||
"available_inventory": product.available_inventory,
|
||||
},
|
||||
)
|
||||
|
||||
# Get current price (use sale_price if available, otherwise regular price)
|
||||
current_price = product.sale_price if product.sale_price else product.price
|
||||
|
||||
# Check if item already exists in cart
|
||||
existing_item = db.query(CartItem).filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id
|
||||
existing_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_item:
|
||||
# Update quantity
|
||||
@@ -190,14 +197,14 @@ class CartService:
|
||||
"current_in_cart": existing_item.quantity,
|
||||
"adding": quantity,
|
||||
"requested_total": new_quantity,
|
||||
"available": product.available_inventory
|
||||
}
|
||||
"available": product.available_inventory,
|
||||
},
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=new_quantity,
|
||||
available=product.available_inventory
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
existing_item.quantity = new_quantity
|
||||
@@ -206,16 +213,13 @@ class CartService:
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] Updated existing cart item",
|
||||
extra={
|
||||
"cart_item_id": existing_item.id,
|
||||
"new_quantity": new_quantity
|
||||
}
|
||||
extra={"cart_item_id": existing_item.id, "new_quantity": new_quantity},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Product quantity updated in cart",
|
||||
"product_id": product_id,
|
||||
"quantity": new_quantity
|
||||
"quantity": new_quantity,
|
||||
}
|
||||
else:
|
||||
# Check inventory for new item
|
||||
@@ -225,14 +229,14 @@ class CartService:
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"requested": quantity,
|
||||
"available": product.available_inventory
|
||||
}
|
||||
"available": product.available_inventory,
|
||||
},
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
# Create new cart item
|
||||
@@ -241,7 +245,7 @@ class CartService:
|
||||
session_id=session_id,
|
||||
product_id=product_id,
|
||||
quantity=quantity,
|
||||
price_at_add=current_price
|
||||
price_at_add=current_price,
|
||||
)
|
||||
db.add(cart_item)
|
||||
db.commit()
|
||||
@@ -252,23 +256,23 @@ class CartService:
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"quantity": quantity,
|
||||
"price": current_price
|
||||
}
|
||||
"price": current_price,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Product added to cart",
|
||||
"product_id": product_id,
|
||||
"quantity": quantity
|
||||
"quantity": quantity,
|
||||
}
|
||||
|
||||
def update_cart_item(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int,
|
||||
) -> Dict:
|
||||
"""
|
||||
Update quantity of item in cart.
|
||||
@@ -292,25 +296,35 @@ class CartService:
|
||||
raise InvalidCartQuantityException(quantity=quantity, min_quantity=1)
|
||||
|
||||
# Find cart item
|
||||
cart_item = db.query(CartItem).filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id
|
||||
cart_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if not cart_item:
|
||||
raise CartItemNotFoundException(product_id=product_id, session_id=session_id)
|
||||
raise CartItemNotFoundException(
|
||||
product_id=product_id, session_id=session_id
|
||||
)
|
||||
|
||||
# Verify product still exists and is active
|
||||
product = db.query(Product).filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True
|
||||
product = (
|
||||
db.query(Product)
|
||||
.filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if not product:
|
||||
raise ProductNotFoundException(str(product_id))
|
||||
@@ -321,7 +335,7 @@ class CartService:
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
# Update quantity
|
||||
@@ -334,22 +348,18 @@ class CartService:
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"product_id": product_id,
|
||||
"new_quantity": quantity
|
||||
}
|
||||
"new_quantity": quantity,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Cart updated",
|
||||
"product_id": product_id,
|
||||
"quantity": quantity
|
||||
"quantity": quantity,
|
||||
}
|
||||
|
||||
def remove_from_cart(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int
|
||||
self, db: Session, vendor_id: int, session_id: str, product_id: int
|
||||
) -> Dict:
|
||||
"""
|
||||
Remove item from cart.
|
||||
@@ -367,16 +377,22 @@ class CartService:
|
||||
ProductNotFoundException: If product not in cart
|
||||
"""
|
||||
# Find and delete cart item
|
||||
cart_item = db.query(CartItem).filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id
|
||||
cart_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if not cart_item:
|
||||
raise CartItemNotFoundException(product_id=product_id, session_id=session_id)
|
||||
raise CartItemNotFoundException(
|
||||
product_id=product_id, session_id=session_id
|
||||
)
|
||||
|
||||
db.delete(cart_item)
|
||||
db.commit()
|
||||
@@ -386,21 +402,13 @@ class CartService:
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"product_id": product_id,
|
||||
"session_id": session_id
|
||||
}
|
||||
"session_id": session_id,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Item removed from cart",
|
||||
"product_id": product_id
|
||||
}
|
||||
return {"message": "Item removed from cart", "product_id": product_id}
|
||||
|
||||
def clear_cart(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str
|
||||
) -> Dict:
|
||||
def clear_cart(self, db: Session, vendor_id: int, session_id: str) -> Dict:
|
||||
"""
|
||||
Clear all items from cart.
|
||||
|
||||
@@ -413,12 +421,13 @@ class CartService:
|
||||
Success message with count of items removed
|
||||
"""
|
||||
# Delete all cart items for this session
|
||||
deleted_count = db.query(CartItem).filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id
|
||||
deleted_count = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(CartItem.vendor_id == vendor_id, CartItem.session_id == session_id)
|
||||
)
|
||||
).delete()
|
||||
.delete()
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
@@ -427,14 +436,11 @@ class CartService:
|
||||
extra={
|
||||
"session_id": session_id,
|
||||
"vendor_id": vendor_id,
|
||||
"items_removed": deleted_count
|
||||
}
|
||||
"items_removed": deleted_count,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Cart cleared",
|
||||
"items_removed": deleted_count
|
||||
}
|
||||
return {"message": "Cart cleared", "items_removed": deleted_count}
|
||||
|
||||
|
||||
# Create service instance
|
||||
|
||||
Reference in New Issue
Block a user