From a00a1a75a30856eaa8f85a33894c144f29da927a Mon Sep 17 00:00:00 2001 From: shri Date: Tue, 27 Aug 2024 16:48:33 +0200 Subject: [PATCH] Compute company profile pic url on the schema --- src/app/domain/companies/controllers/companies.py | 4 +++- src/app/domain/companies/schemas.py | 14 ++++---------- src/app/domain/companies/services.py | 4 +--- src/app/lib/utils.py | 2 +- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/app/domain/companies/controllers/companies.py b/src/app/domain/companies/controllers/companies.py index edc2406f..55d1ac15 100644 --- a/src/app/domain/companies/controllers/companies.py +++ b/src/app/domain/companies/controllers/companies.py @@ -85,7 +85,9 @@ async def get_company( ) -> Company: """Get details about a comapny.""" db_obj = await companies_service.get(company_id) - return companies_service.to_schema(schema_type=Company, data=db_obj) + # company = companies_service.to_schema(schema_type=Company, data=db_obj) + # Workaround due to https://github.com/jcrist/msgspec/issues/673 + return Company.from_dict(db_obj.to_dict()) @patch( operation_id="UpdateCompany", diff --git a/src/app/domain/companies/schemas.py b/src/app/domain/companies/schemas.py index 4b602542..cd2eb3f7 100644 --- a/src/app/domain/companies/schemas.py +++ b/src/app/domain/companies/schemas.py @@ -30,16 +30,10 @@ class Company(CamelizedBaseStruct): last_funding: Funding | None = None org_size: OrgSize | None = None - @classmethod - def from_dict(cls, data): - """Create an instance from a dictionary.""" - obj = cls(**data) - - # Add company logo URL - if obj.url: - obj.profile_pic_url = get_logo_dev_link(obj.url) - - return obj + def __post_init__(self): + """Build a profile pic url from company url.""" + if self.url: + self.profile_pic_url = get_logo_dev_link(self.url) class CompanyCreate(CamelizedBaseStruct): diff --git a/src/app/domain/companies/services.py b/src/app/domain/companies/services.py index d5b9789d..9af2a7f0 100644 --- a/src/app/domain/companies/services.py +++ b/src/app/domain/companies/services.py @@ -21,9 +21,7 @@ from msgspec import Struct from sqlalchemy.orm import InstrumentedAttribute -__all__ = ( - "CompanyService", -) +__all__ = ("CompanyService",) class CompanyService(SQLAlchemyAsyncRepositoryService[Company]): diff --git a/src/app/lib/utils.py b/src/app/lib/utils.py index f6ae6302..0cf95f08 100644 --- a/src/app/lib/utils.py +++ b/src/app/lib/utils.py @@ -15,4 +15,4 @@ def get_logo_dev_link(url: str) -> str | None: logo_dev_token = os.environ["LOGO_DEV_TOKEN"] return f"https://img.logo.dev/{domain}?token={logo_dev_token}" except Exception: - pass + return None