Adding a common moneratary amount field type
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
from typing import Optional, Any
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import computed_field
|
|
||||||
from sqlmodel import Relationship
|
from sqlmodel import Relationship
|
||||||
from sqlalchemy.sql import text
|
|
||||||
|
|
||||||
from account.enums import CategoryFamily, Asset, Liability, AccountFamily
|
from account.enums import CategoryFamily, Asset, Liability, AccountFamily
|
||||||
from account.schemas import AccountBaseId
|
from account.schemas import AccountBaseId
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ from decimal import Decimal
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
|
|
||||||
from pydantic.json_schema import SkipJsonSchema
|
|
||||||
from sqlmodel import Field, SQLModel
|
from sqlmodel import Field, SQLModel
|
||||||
from pydantic import Field as PydField
|
from pydantic import Field as PydField
|
||||||
|
from pydantic.json_schema import SkipJsonSchema
|
||||||
|
|
||||||
from account.enums import Asset, Liability, CategoryFamily
|
from account.enums import Asset, Liability, CategoryFamily
|
||||||
|
from core.types import MonetaryAmount
|
||||||
|
|
||||||
class AccountBase(SQLModel):
|
class AccountBase(SQLModel):
|
||||||
name: str = Field(index=True)
|
name: str = Field(index=True)
|
||||||
@@ -39,7 +40,7 @@ class AccountWrite(BaseAccountWrite):
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
opening_date: date = Field()
|
opening_date: date = Field()
|
||||||
opening_balance: Decimal = Field(decimal_places=2, default=0)
|
opening_balance: MonetaryAmount = Field()
|
||||||
|
|
||||||
class AccountCreate(AccountWrite):
|
class AccountCreate(AccountWrite):
|
||||||
pass
|
pass
|
||||||
|
|||||||
0
api/app/core/__init__.py
Normal file
0
api/app/core/__init__.py
Normal file
41
api/app/core/types.py
Normal file
41
api/app/core/types.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from decimal import Decimal
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from pydantic import GetCoreSchemaHandler
|
||||||
|
from pydantic_core import core_schema
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MonetaryAmount:
|
||||||
|
amount: Decimal# = Field(decimal_places=2, default=0)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __get_pydantic_core_schema__(
|
||||||
|
cls, source: type[Any], handler: GetCoreSchemaHandler
|
||||||
|
) -> core_schema.CoreSchema:
|
||||||
|
assert source is MonetaryAmount
|
||||||
|
return core_schema.no_info_after_validator_function(
|
||||||
|
cls._validate,
|
||||||
|
core_schema.float_schema(multiple_of=0.01),
|
||||||
|
serialization=core_schema.plain_serializer_function_ser_schema(
|
||||||
|
cls._serialize,
|
||||||
|
info_arg=False,
|
||||||
|
return_schema=core_schema.float_schema(multiple_of=0.01),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate(value: str) -> 'CompressedString':
|
||||||
|
inverse_dictionary: dict[str, int] = {}
|
||||||
|
text: list[int] = []
|
||||||
|
for word in value.split(' '):
|
||||||
|
if word not in inverse_dictionary:
|
||||||
|
inverse_dictionary[word] = len(inverse_dictionary)
|
||||||
|
text.append(inverse_dictionary[word])
|
||||||
|
return MonetaryAmount(
|
||||||
|
{v: k for k, v in inverse_dictionary.items()}, text
|
||||||
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _serialize(value: 'CompressedString') -> str:
|
||||||
|
return value.amount
|
||||||
Reference in New Issue
Block a user