I'll review your code and suggest some improvements based on best practices. Here's an enhanced version with explanations:
from typing import Optional
from sqlalchemy.orm import Session
from sqlalchemy.exc import SQLAlchemyError
from schemas import (
ProductMaker_response,
BusinessInfoType,
AwardType,
ContactPersonType,
ProgramType,
AddressType,
TelephoneType
)
from logging import getLogger
logger = getLogger(__name__)
def retrieve_pm_by_productmakerid(
productmaker_id: int,
ProductMaker_Schema: type[ProductMaker_response]
) -> Optional[ProductMaker_response]:
"""
Retrieve product maker information by ID and convert to Pydantic model.
Args:
productmaker_id: The ID of the product maker
ProductMaker_Schema: Pydantic schema class for response
Returns:
ProductMaker_response instance or None if not found
Raises:
SQLAlchemyError: For database-related errors
"""
try:
with get_db_session() as session:
p_maker = session.query(ProductMaker).filter(
ProductMaker.Product_Maker_id == productmaker_id
).first()
if not p_maker:
logger.info(f"No product maker found with ID {productmaker_id}")
return None
# Map related entities using list comprehensions
awards_data = [
AwardType(
award_name=award.award_name,
year=award.year,
issuing_organisation=award.issuing_organisation,
award_path=award.award_path
) for award in p_maker.awards
]
contacts_data = [
ContactPersonType(
first_name=contact.first_name,
last_name=contact.last_name,
email=contact.email,
phone=contact.phone,
role=contact.role
) for contact in p_maker.contact_persons
]
programs_data = [
ProgramType(
program_id=program.program_id,
program_name=program.program_name,
program_status=program.program_status
) for program in p_maker.programs
]
# Create composite type instances
business_info = BusinessInfoType(
business_name=p_maker.business_name,
business_registration_doc=None,
year_incorporated=p_maker.year_incorporated,
founder_CEO=p_maker.founder_CEO,
website=p_maker.website,
category=p_maker.category
)
office_address = AddressType(
street=p_maker.office_address.street,
state=p_maker.office_address.state,
city=p_maker.office_address.city,
postal_code=p_maker.office_address.postal_code,
country=p_maker.office_address.country
)
warehouse_address = AddressType(
street=p_maker.warehouse_address.street,
state=p_maker.warehouse_address.state,
city=p_maker.warehouse_address.city,
postal_code=p_maker.warehouse_address.postal_code,
country=p_maker.warehouse_address.country
)
telephone = TelephoneType(
country_code=p_maker.telephone.country_code,
area_code=p_maker.telephone.area_code,
telephone_number=p_maker.telephone.telephone_number
)
# Create and return the main Pydantic instance
return ProductMaker_Schema(
Product_Maker_id=p_maker.Product_Maker_id,
business_info=business_info,
office_address=office_address,
warehouse_address=warehouse_address,
telephone=telephone,
awards=awards_data,
contact_persons=contacts_data,
validation_status=p_maker.validation_status,
last_validation_date=p_maker.last_validation_date,
programs=programs_data,
created_at=p_maker.created_at,
created_by=p_maker.created_by,
updated_at=p_maker.updated_at,
TSI_User_Info_id=p_maker.TSI_User_Info_id
)
except SQLAlchemyError as db_error:
logger.error(f"Database error occurred: {db_error}")
raise
except Exception as e:
logger.error(f"Unexpected error occurred: {e}")
raise