I have built a simple python module to retrieve data from PostgreSQL and create a pydantic instance of the response for sending to my frontend. I am using a SQLAlchemy ORM to Map to my database tables - Productmaker, awards, programs and contact person. I also have some fields like telephone and address defined as composite type. The function uses teh product maker id to retrieve data from database and recompose a pydantic instance. Can you check if i covered all best practices here : from schemas import ProductMaker_response
from schemas import BusinessInfoType, AwardType, ContactPersonType, ProgramType, AddressType, TelephoneType
retrieve a product maker information by the product maker id
Params - product maker id, Pydantic Model/Schema for Query Response
returns - Pydantic instance for Query
def retrieve_pm_by_productmakerid(productmaker_id, ProductMaker_Schema:ProductMaker_response):
pm_id = productmaker_id
try:
with get_db_session() as session:
p_maker = session.query(ProductMaker).filter_by(ProductMaker.Product_Maker_id == pm_id ).first()
if p_maker != []:
awards_data = [AwardType_res(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_res(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_res(program_id = program.program_id, program_name = program.program_name, program_status = program.program_status)
for program in p_maker.programs]
business_info = BusinessInfoType_res( 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_res( 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_res( 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_res(country_code = p_maker.telephone.country_code,
area_code = p_maker.telephone.area_code,,
telephone_number = p_maker.telephone.telephone_number,)
result = 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 Exception as e:
# a general except block to catch any unhandled exceptions
print(f"An unexpected error occurred: {e}")
else:
print("Product maker data retrieval operation successful!")
return result
finally:
print("All cleanup operations completed.")
from sqlalchemy.orm import selectinload
p_maker = (
session.query(ProductMaker)
.options(
selectinload(ProductMaker.awards),
selectinload(ProductMaker.contact_persons),
selectinload(ProductMaker.programs)
)
.filter(ProductMaker.Product_Maker_id == pm_id)
.first()
)
from sqlalchemy.orm import selectinload
from schemas import ProductMaker_response, AwardType, ContactPersonType, ProgramType, BusinessInfoType, AddressType, TelephoneType
import logging
logger = logging.getLogger(__name__)
def retrieve_pm_by_productmakerid(productmaker_id: int) -> ProductMaker_response:
try:
with get_db_session() as session:
p_maker = (
session.query(ProductMaker)
.options(
selectinload(ProductMaker.awards),
selectinload(ProductMaker.contact_persons),
selectinload(ProductMaker.programs)
)
.filter(ProductMaker.Product_Maker_id == productmaker_id)
.first()
)
if not p_maker:
raise ValueError("Product maker not found")
awards_data = [
AwardType(
award_name=a.award_name,
year=a.year,
issuing_organisation=a.issuing_organisation,
award_path=a.award_path
) for a in p_maker.awards
]
contacts_data = [
ContactPersonType(
first_name=c.first_name,
last_name=c.last_name,
email=c.email,
phone=c.phone,
role=c.role
) for c in p_maker.contact_persons
]
programs_data = [
ProgramType(
program_id=p.program_id,
program_name=p.program_name,
program_status=p.program_status
) for p in p_maker.programs
]
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
)
return ProductMaker_response(
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 Exception as e:
logger.exception("Error retrieving product maker")
raise