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:
@@ -15,22 +15,19 @@ from typing import List, Optional, Tuple
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.exceptions import (
|
||||
VendorNotFoundException,
|
||||
VendorAlreadyExistsException,
|
||||
UnauthorizedVendorAccessException,
|
||||
InvalidVendorDataException,
|
||||
MarketplaceProductNotFoundException,
|
||||
ProductAlreadyExistsException,
|
||||
MaxVendorsReachedException,
|
||||
ValidationException,
|
||||
)
|
||||
from models.schema.vendor import VendorCreate
|
||||
from models.schema.product import ProductCreate
|
||||
from app.exceptions import (InvalidVendorDataException,
|
||||
MarketplaceProductNotFoundException,
|
||||
MaxVendorsReachedException,
|
||||
ProductAlreadyExistsException,
|
||||
UnauthorizedVendorAccessException,
|
||||
ValidationException, VendorAlreadyExistsException,
|
||||
VendorNotFoundException)
|
||||
from models.database.marketplace_product import MarketplaceProduct
|
||||
from models.database.vendor import Vendor
|
||||
from models.database.product import Product
|
||||
from models.database.user import User
|
||||
from models.database.vendor import Vendor
|
||||
from models.schema.product import ProductCreate
|
||||
from models.schema.vendor import VendorCreate
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -39,7 +36,7 @@ class VendorService:
|
||||
"""Service class for vendor operations following the application's service pattern."""
|
||||
|
||||
def create_vendor(
|
||||
self, db: Session, vendor_data: VendorCreate, current_user: User
|
||||
self, db: Session, vendor_data: VendorCreate, current_user: User
|
||||
) -> Vendor:
|
||||
"""
|
||||
Create a new vendor.
|
||||
@@ -47,7 +44,7 @@ class VendorService:
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_data: Vendor creation data
|
||||
current_user: User creating the vendor
|
||||
current_user: User creating the vendor
|
||||
|
||||
Returns:
|
||||
Created vendor object
|
||||
@@ -91,7 +88,11 @@ class VendorService:
|
||||
)
|
||||
return new_vendor
|
||||
|
||||
except (VendorAlreadyExistsException, MaxVendorsReachedException, InvalidVendorDataException):
|
||||
except (
|
||||
VendorAlreadyExistsException,
|
||||
MaxVendorsReachedException,
|
||||
InvalidVendorDataException,
|
||||
):
|
||||
db.rollback()
|
||||
raise # Re-raise custom exceptions
|
||||
except Exception as e:
|
||||
@@ -100,13 +101,13 @@ class VendorService:
|
||||
raise ValidationException("Failed to create vendor ")
|
||||
|
||||
def get_vendors(
|
||||
self,
|
||||
db: Session,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
verified_only: bool = False,
|
||||
self,
|
||||
db: Session,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
verified_only: bool = False,
|
||||
) -> Tuple[List[Vendor], int]:
|
||||
"""
|
||||
Get vendors with filtering.
|
||||
@@ -129,7 +130,10 @@ class VendorService:
|
||||
if current_user.role != "admin":
|
||||
query = query.filter(
|
||||
(Vendor.is_active == True)
|
||||
& ((Vendor.is_verified == True) | (Vendor.owner_user_id == current_user.id))
|
||||
& (
|
||||
(Vendor.is_verified == True)
|
||||
| (Vendor.owner_user_id == current_user.id)
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Admin can apply filters
|
||||
@@ -147,14 +151,16 @@ class VendorService:
|
||||
logger.error(f"Error getting vendors: {str(e)}")
|
||||
raise ValidationException("Failed to retrieve vendors")
|
||||
|
||||
def get_vendor_by_code(self, db: Session, vendor_code: str, current_user: User) -> Vendor:
|
||||
def get_vendor_by_code(
|
||||
self, db: Session, vendor_code: str, current_user: User
|
||||
) -> Vendor:
|
||||
"""
|
||||
Get vendor by vendor code with access control.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_code: Vendor code to find
|
||||
current_user: Current user requesting the vendor
|
||||
current_user: Current user requesting the vendor
|
||||
|
||||
Returns:
|
||||
Vendor object
|
||||
@@ -170,14 +176,14 @@ class VendorService:
|
||||
.first()
|
||||
)
|
||||
|
||||
if not vendor :
|
||||
if not vendor:
|
||||
raise VendorNotFoundException(vendor_code)
|
||||
|
||||
# Check access permissions
|
||||
if not self._can_access_vendor(vendor, current_user):
|
||||
raise UnauthorizedVendorAccessException(vendor_code, current_user.id)
|
||||
|
||||
return vendor
|
||||
return vendor
|
||||
|
||||
except (VendorNotFoundException, UnauthorizedVendorAccessException):
|
||||
raise # Re-raise custom exceptions
|
||||
@@ -186,7 +192,7 @@ class VendorService:
|
||||
raise ValidationException("Failed to retrieve vendor ")
|
||||
|
||||
def add_product_to_catalog(
|
||||
self, db: Session, vendor : Vendor, product: ProductCreate
|
||||
self, db: Session, vendor: Vendor, product: ProductCreate
|
||||
) -> Product:
|
||||
"""
|
||||
Add existing product to vendor catalog with vendor -specific settings.
|
||||
@@ -201,15 +207,19 @@ class VendorService:
|
||||
|
||||
Raises:
|
||||
MarketplaceProductNotFoundException: If product not found
|
||||
ProductAlreadyExistsException: If product already in vendor
|
||||
ProductAlreadyExistsException: If product already in vendor
|
||||
"""
|
||||
try:
|
||||
# Check if product exists
|
||||
marketplace_product = self._get_product_by_id_or_raise(db, product.marketplace_product_id)
|
||||
marketplace_product = self._get_product_by_id_or_raise(
|
||||
db, product.marketplace_product_id
|
||||
)
|
||||
|
||||
# Check if product already in vendor
|
||||
# Check if product already in vendor
|
||||
if self._product_in_catalog(db, vendor.id, marketplace_product.id):
|
||||
raise ProductAlreadyExistsException(vendor.vendor_code, product.marketplace_product_id)
|
||||
raise ProductAlreadyExistsException(
|
||||
vendor.vendor_code, product.marketplace_product_id
|
||||
)
|
||||
|
||||
# Create vendor -product association
|
||||
new_product = Product(
|
||||
@@ -225,7 +235,9 @@ class VendorService:
|
||||
# Load the product relationship
|
||||
db.refresh(new_product)
|
||||
|
||||
logger.info(f"MarketplaceProduct {product.marketplace_product_id} added to vendor {vendor.vendor_code}")
|
||||
logger.info(
|
||||
f"MarketplaceProduct {product.marketplace_product_id} added to vendor {vendor.vendor_code}"
|
||||
)
|
||||
return new_product
|
||||
|
||||
except (MarketplaceProductNotFoundException, ProductAlreadyExistsException):
|
||||
@@ -237,14 +249,14 @@ class VendorService:
|
||||
raise ValidationException("Failed to add product to vendor ")
|
||||
|
||||
def get_products(
|
||||
self,
|
||||
db: Session,
|
||||
vendor : Vendor,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
featured_only: bool = False,
|
||||
self,
|
||||
db: Session,
|
||||
vendor: Vendor,
|
||||
current_user: User,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
active_only: bool = True,
|
||||
featured_only: bool = False,
|
||||
) -> Tuple[List[Product], int]:
|
||||
"""
|
||||
Get products in vendor catalog with filtering.
|
||||
@@ -267,7 +279,9 @@ class VendorService:
|
||||
try:
|
||||
# Check access permissions
|
||||
if not self._can_access_vendor(vendor, current_user):
|
||||
raise UnauthorizedVendorAccessException(vendor.vendor_code, current_user.id)
|
||||
raise UnauthorizedVendorAccessException(
|
||||
vendor.vendor_code, current_user.id
|
||||
)
|
||||
|
||||
# Query vendor products
|
||||
query = db.query(Product).filter(Product.vendor_id == vendor.id)
|
||||
@@ -292,17 +306,20 @@ class VendorService:
|
||||
def _validate_vendor_data(self, vendor_data: VendorCreate) -> None:
|
||||
"""Validate vendor creation data."""
|
||||
if not vendor_data.vendor_code or not vendor_data.vendor_code.strip():
|
||||
raise InvalidVendorDataException("Vendor code is required", field="vendor_code")
|
||||
raise InvalidVendorDataException(
|
||||
"Vendor code is required", field="vendor_code"
|
||||
)
|
||||
|
||||
if not vendor_data.vendor_name or not vendor_data.vendor_name.strip():
|
||||
raise InvalidVendorDataException("Vendor name is required", field="name")
|
||||
|
||||
# Validate vendor code format (alphanumeric, underscores, hyphens)
|
||||
import re
|
||||
if not re.match(r'^[A-Za-z0-9_-]+$', vendor_data.vendor_code):
|
||||
|
||||
if not re.match(r"^[A-Za-z0-9_-]+$", vendor_data.vendor_code):
|
||||
raise InvalidVendorDataException(
|
||||
"Vendor code can only contain letters, numbers, underscores, and hyphens",
|
||||
field="vendor_code"
|
||||
field="vendor_code",
|
||||
)
|
||||
|
||||
def _check_vendor_limit(self, db: Session, user: User) -> None:
|
||||
@@ -310,7 +327,9 @@ class VendorService:
|
||||
if user.role == "admin":
|
||||
return # Admins have no limit
|
||||
|
||||
user_vendor_count = db.query(Vendor).filter(Vendor.owner_user_id == user.id).count()
|
||||
user_vendor_count = (
|
||||
db.query(Vendor).filter(Vendor.owner_user_id == user.id).count()
|
||||
)
|
||||
max_vendors = 5 # Configure this as needed
|
||||
|
||||
if user_vendor_count >= max_vendors:
|
||||
@@ -319,30 +338,40 @@ class VendorService:
|
||||
def _vendor_code_exists(self, db: Session, vendor_code: str) -> bool:
|
||||
"""Check if vendor code already exists (case-insensitive)."""
|
||||
return (
|
||||
db.query(Vendor)
|
||||
.filter(func.upper(Vendor.vendor_code) == vendor_code.upper())
|
||||
.first() is not None
|
||||
db.query(Vendor)
|
||||
.filter(func.upper(Vendor.vendor_code) == vendor_code.upper())
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
def _get_product_by_id_or_raise(self, db: Session, marketplace_product_id: str) -> MarketplaceProduct:
|
||||
def _get_product_by_id_or_raise(
|
||||
self, db: Session, marketplace_product_id: str
|
||||
) -> MarketplaceProduct:
|
||||
"""Get product by ID or raise exception."""
|
||||
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id).first()
|
||||
product = (
|
||||
db.query(MarketplaceProduct)
|
||||
.filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id)
|
||||
.first()
|
||||
)
|
||||
if not product:
|
||||
raise MarketplaceProductNotFoundException(marketplace_product_id)
|
||||
return product
|
||||
|
||||
def _product_in_catalog(self, db: Session, vendor_id: int, marketplace_product_id: int) -> bool:
|
||||
def _product_in_catalog(
|
||||
self, db: Session, vendor_id: int, marketplace_product_id: int
|
||||
) -> bool:
|
||||
"""Check if product is already in vendor."""
|
||||
return (
|
||||
db.query(Product)
|
||||
.filter(
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.marketplace_product_id == marketplace_product_id
|
||||
)
|
||||
.first() is not None
|
||||
db.query(Product)
|
||||
.filter(
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.marketplace_product_id == marketplace_product_id,
|
||||
)
|
||||
.first()
|
||||
is not None
|
||||
)
|
||||
|
||||
def _can_access_vendor(self, vendor : Vendor, user: User) -> bool:
|
||||
def _can_access_vendor(self, vendor: Vendor, user: User) -> bool:
|
||||
"""Check if user can access vendor."""
|
||||
# Admins and owners can always access
|
||||
if user.role == "admin" or vendor.owner_user_id == user.id:
|
||||
@@ -351,9 +380,10 @@ class VendorService:
|
||||
# Others can only access active and verified vendors
|
||||
return vendor.is_active and vendor.is_verified
|
||||
|
||||
def _is_vendor_owner(self, vendor : Vendor, user: User) -> bool:
|
||||
def _is_vendor_owner(self, vendor: Vendor, user: User) -> bool:
|
||||
"""Check if user is vendor owner."""
|
||||
return vendor.owner_user_id == user.id
|
||||
|
||||
|
||||
# Create service instance following the same pattern as other services
|
||||
vendor_service = VendorService()
|
||||
|
||||
Reference in New Issue
Block a user