Skip to content

Commit

Permalink
Serialize decimal with 2 decimal points (#441)
Browse files Browse the repository at this point in the history
* chore: Add CustomDecimal_13Digits

* chore: Add CustomDecimal_12Digits

* chore: 13 digits

* chore: Add CustomDecimal_7Digits

* chore: Add CustomDecimal_15Digits

* chore: update import

* chore: Add CustomDecimal_12Digits

* chore: Add CustomDecimal_13Digits

* chore: Add CustomDecimal_7Digits

* chore: Update imports

* chore: Update generate_treasury_report with str

---------

Co-authored-by: Victor Shia <[email protected]>
  • Loading branch information
vshia and vshia authored Sep 25, 2024
1 parent 218fd74 commit 1d7daac
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 82 deletions.
2 changes: 1 addition & 1 deletion python/src/functions/generate_treasury_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def insert_project_row(
if prop_meta[f"treasury_report_col_{project_use_code}"]:
row_with_output_cols[
prop_meta[f"treasury_report_col_{project_use_code}"]
] = row_dict[prop] if row_dict[prop] else ""
] = str(row_dict[prop]) if row_dict[prop] else ""

for col in row_with_output_cols.keys():
if sheet:
Expand Down
54 changes: 54 additions & 0 deletions python/src/schemas/custom_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from typing_extensions import Annotated
from pydantic import BaseModel, Field, PlainSerializer
from decimal import Decimal


CustomDecimal_16Digits = Annotated[
Decimal, Field(
max_digits=16,
decimal_places=2
), PlainSerializer(
lambda x: f"{float(x):.2f}",
return_type=str,
),
]

CustomDecimal_15Digits = Annotated[
Decimal, Field(
max_digits=15,
decimal_places=2
), PlainSerializer(
lambda x: f"{float(x):.2f}",
return_type=str,
),
]

CustomDecimal_13Digits = Annotated[
Decimal, Field(
max_digits=13,
decimal_places=2
), PlainSerializer(
lambda x: f"{float(x):.2f}",
return_type=str,
),
]

CustomDecimal_12Digits = Annotated[
Decimal, Field(
max_digits=12,
decimal_places=2
), PlainSerializer(
lambda x: f"{float(x):.2f}",
return_type=str,
),
]

CustomDecimal_7Digits = Annotated[
Decimal, Field(
max_digits=7,
decimal_places=2
), PlainSerializer(
lambda x: f"{float(x):.2f}",
return_type=str,
),
]
50 changes: 22 additions & 28 deletions python/src/schemas/schema_V2024_04_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
ConfigDict,
Field,
ValidationInfo,
condecimal,
conint,
constr,
field_serializer,
field_validator,
)

from src.schemas.project_types import NAME_BY_PROJECT
from src.schemas.custom_types import (
CustomDecimal_7Digits,
CustomDecimal_12Digits,
CustomDecimal_13Digits,
)


class StateAbbreviation(str, Enum):
Expand Down Expand Up @@ -137,14 +141,14 @@ class BaseProjectRow(BaseModel):
serialization_alias="Capital Asset Owenership Type",
json_schema_extra={"column": "F"},
)
Total_CPF_Funding_for_Project__c: condecimal(max_digits=13, decimal_places=2) = (
Total_CPF_Funding_for_Project__c: CustomDecimal_13Digits = (
Field(
...,
serialization_alias="Total CPF Funding for Project",
json_schema_extra={"column": "G"},
)
)
Total_from_all_funding_sources__c: condecimal(max_digits=13, decimal_places=2) = (
Total_from_all_funding_sources__c: CustomDecimal_13Digits = (
Field(
...,
serialization_alias="Total From all Funding Sources",
Expand All @@ -157,22 +161,22 @@ class BaseProjectRow(BaseModel):
max_length=3000,
json_schema_extra={"column": "I"},
)
Current_Period_Obligation__c: condecimal(max_digits=12, decimal_places=2) = Field(
Current_Period_Obligation__c: CustomDecimal_12Digits = Field(
...,
serialization_alias="Current Period Obligation",
json_schema_extra={"column": "J"},
)
Current_Period_Expenditure__c: condecimal(max_digits=12, decimal_places=2) = Field(
Current_Period_Expenditure__c: CustomDecimal_12Digits = Field(
...,
serialization_alias="Current Period Expenditure",
json_schema_extra={"column": "K"},
)
Cumulative_Obligation__c: condecimal(max_digits=12, decimal_places=2) = Field(
Cumulative_Obligation__c: CustomDecimal_12Digits = Field(
...,
serialization_alias="Cumulative Obligation",
json_schema_extra={"column": "L"},
)
Cumulative_Expenditure__c: condecimal(max_digits=12, decimal_places=2) = Field(
Cumulative_Expenditure__c: CustomDecimal_12Digits = Field(
...,
serialization_alias="Cumulative Expenditure",
json_schema_extra={"column": "M"},
Expand Down Expand Up @@ -240,9 +244,7 @@ class BaseProjectRow(BaseModel):
max_length=50,
json_schema_extra={"column": "Z"},
)
Amount_of_Matching_Funds__c: Optional[
condecimal(max_digits=12, decimal_places=2)
] = Field(
Amount_of_Matching_Funds__c: Optional[CustomDecimal_12Digits] = Field(
default=None,
serialization_alias="Amount of Matching Funds",
json_schema_extra={"column": "AA"},
Expand Down Expand Up @@ -623,16 +625,14 @@ class Project1BRow(BaseProjectRow, AddressFields):
serialization_alias="Laptops (Actual)",
json_schema_extra={"column": "CC"},
)
Laptops_Expenditures_Planned__c: condecimal(max_digits=13, decimal_places=2) = (
Laptops_Expenditures_Planned__c: CustomDecimal_13Digits = (
Field(
...,
serialization_alias="Laptops Expenditure (Planned)",
json_schema_extra={"column": "CD"},
)
)
Laptops_Expenditures_Actual__c: Optional[
condecimal(max_digits=13, decimal_places=2)
] = Field(
Laptops_Expenditures_Actual__c: Optional[CustomDecimal_13Digits] = Field(
default=None,
serialization_alias="Laptops Expenditure (Actual)",
json_schema_extra={"column": "CE"},
Expand All @@ -645,14 +645,12 @@ class Project1BRow(BaseProjectRow, AddressFields):
serialization_alias="Tablets (Actual)",
json_schema_extra={"column": "CG"},
)
Tablet_Expenditures_Planned__c: condecimal(max_digits=13, decimal_places=2) = Field(
Tablet_Expenditures_Planned__c: CustomDecimal_13Digits = Field(
...,
serialization_alias="Tablets Expenditure (Planned)",
json_schema_extra={"column": "CH"},
)
Tablets_Expenditures_Actual__c: Optional[
condecimal(max_digits=13, decimal_places=2)
] = Field(
Tablets_Expenditures_Actual__c: Optional[CustomDecimal_13Digits] = Field(
default=None,
serialization_alias="Tablets Expenditure (Actual)",
json_schema_extra={"column": "CI"},
Expand All @@ -667,15 +665,13 @@ class Project1BRow(BaseProjectRow, AddressFields):
serialization_alias="Desktop Computers (Actual)",
json_schema_extra={"column": "CK"},
)
Desktop_Computers_Expenditures_Planned__c: condecimal(
max_digits=13, decimal_places=2
) = Field(
Desktop_Computers_Expenditures_Planned__c: CustomDecimal_13Digits = Field(
...,
serialization_alias="Desktop Computers Expenditure (Planned)",
json_schema_extra={"column": "CL"},
)
Desktop_Computers_Expenditures_Actual__c: Optional[
condecimal(max_digits=13, decimal_places=2)
CustomDecimal_13Digits
] = Field(
default=None,
serialization_alias="Desktop Computers Expenditure (Actual)",
Expand All @@ -691,15 +687,15 @@ class Project1BRow(BaseProjectRow, AddressFields):
serialization_alias="Public WiFi (Actual)",
json_schema_extra={"column": "CO"},
)
Public_WiFi_Expenditures_Planned__c: condecimal(max_digits=13, decimal_places=2) = (
Public_WiFi_Expenditures_Planned__c: CustomDecimal_13Digits = (
Field(
...,
serialization_alias="Public Wifi Expenditures (Planned)",
json_schema_extra={"column": "CP"},
)
)
Public_WiFi_Expenditures_Actual__c: Optional[
condecimal(max_digits=13, decimal_places=2)
CustomDecimal_13Digits
] = Field(
default=None,
serialization_alias="Public Wifi Expenditures (Actual)",
Expand All @@ -715,14 +711,12 @@ class Project1BRow(BaseProjectRow, AddressFields):
serialization_alias="Other Devices (Actual)",
json_schema_extra={"column": "CS"},
)
Other_Expenditures_Planned__c: condecimal(max_digits=7, decimal_places=2) = Field(
Other_Expenditures_Planned__c: CustomDecimal_7Digits = Field(
...,
serialization_alias="Other Expenditures (Planned)",
json_schema_extra={"column": "CT"},
)
Other_Expenditures_Actual__c: Optional[
condecimal(max_digits=7, decimal_places=2)
] = Field(
Other_Expenditures_Actual__c: Optional[CustomDecimal_7Digits] = Field(
default=None,
serialization_alias="Other Expenditures (Actual)",
json_schema_extra={"column": "CU"},
Expand Down
Loading

0 comments on commit 1d7daac

Please sign in to comment.