Implemented fixtures & Implementing Resource pattern

This commit is contained in:
2025-02-11 22:57:11 +01:00
parent ed6be838fe
commit 171875f915
6 changed files with 224 additions and 39 deletions

View File

@@ -12,13 +12,15 @@ class Account(AccountBaseId, table=True):
sa_relationship_kwargs=dict(remote_side='Account.id')
)
children_accounts: list["Account"] = Relationship(back_populates='parent_account')
transaction_splits: list["Split"] = Relationship(back_populates='account')
def get_child_path(self):
return f"{self.path}{self.name}/"
def get_child_path(self, child):
return f"{self.path}{child.name}/"
def get_root_path(self):
root = "/Categories" if self.is_category() else "/Accounts"
return f"{root}/{self.family}/"
return f"{root}/{self.family}/{self.name}/"
def update_children_path(self, session, old_path):
request = f"UPDATE {self.__tablename__} SET path=REPLACE(path, '{old_path}', '{self.path}') WHERE path LIKE '{old_path}{self.name }/%'"
@@ -32,7 +34,7 @@ class Account(AccountBaseId, table=True):
if not path:
return None
return session.exec(select(cls).where(cls.path == path))
return session.exec(select(cls).where(cls.path == path)).first()
def get_parent(self, session):
@@ -47,15 +49,15 @@ class Account(AccountBaseId, table=True):
self.path = self.get_root_path()
else:
self.parent_account = self.get(session, self.parent_account_id)
self.path = self.parent_account.get_child_path()
self.path = self.parent_account.get_child_path(self)
return self.path
def compute_family(self):
if self.type in Asset:
self.family = AccountFamily.Asset
self.family = AccountFamily.Asset.value
elif self.type in Liability:
self.family = AccountFamily.Liability
self.family = AccountFamily.Liability.value
else:
self.family = self.type
return self.family
@@ -74,7 +76,7 @@ class Account(AccountBaseId, table=True):
model.compute_family()
model.validate_parent(session)
model.path = model.get_path(session)
model.compute_path(session)
return model
def validate_parent(self, session):
@@ -88,7 +90,7 @@ class Account(AccountBaseId, table=True):
if parent.family != self.family:
raise ValueError("Account family mismatch with parent account..")
if parent.path.startswith(self.path):
if self.path and parent.path.startswith(self.path):
raise ValueError("Parent Account is descendant")
return True
@@ -114,9 +116,15 @@ class Account(AccountBaseId, table=True):
return account_db
@classmethod
def select(cls):
return select(Account)
@classmethod
def list(cls, filters):
return filters.sort(filters.filter(select(Account)))
return filters.sort(filters.filter(
cls.select()
))
@classmethod
def list_accounts(cls, filters):