34 lines
892 B
Python
34 lines
892 B
Python
from dataclasses import dataclass, field
|
|
from uuid import UUID, uuid4
|
|
|
|
|
|
@dataclass
|
|
class Transaction:
|
|
id: UUID = field(default_factory=uuid4)
|
|
date: str = ""
|
|
account: str = ""
|
|
category: str = ""
|
|
amount: float = 0.0
|
|
description: str = ""
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": str(self.id),
|
|
"date": self.date,
|
|
"account": self.account,
|
|
"category": self.category,
|
|
"amount": self.amount,
|
|
"description": self.description,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict) -> "Transaction":
|
|
return cls(
|
|
id=UUID(data["id"]),
|
|
date=data["date"],
|
|
account=data.get("account", ""),
|
|
category=data.get("category", ""),
|
|
amount=data.get("amount", 0.0),
|
|
description=data.get("description", ""),
|
|
)
|