Adding auto creation of opening transaction on create

This commit is contained in:
2025-02-12 23:13:26 +01:00
parent 825fa41bd9
commit f4bf1a2b30
6 changed files with 56 additions and 26 deletions

View File

@@ -7,8 +7,6 @@ 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
@@ -16,26 +14,24 @@ class MonetaryAmount:
assert source is MonetaryAmount
return core_schema.no_info_after_validator_function(
cls._validate,
core_schema.float_schema(multiple_of=0.01),
core_schema.decimal_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),
return_schema=core_schema.decimal_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
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@staticmethod
def _serialize(value: 'CompressedString') -> str:
def _validate(value: Decimal) -> 'MonetaryAmount':
if value.as_tuple()[2] < -2:
raise ValueError(f'{value} has more than two decimal places.')
return value
@staticmethod
def _serialize(value: 'MonetaryAmount') -> str:
return value.amount