[WIP] trying to validate empty parent account
This commit is contained in:
@@ -1,21 +1,55 @@
|
|||||||
|
from typing import Optional
|
||||||
from uuid import UUID, uuid4
|
from uuid import UUID, uuid4
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from sqlmodel import Field, SQLModel, select
|
from fastapi_filter.contrib.sqlalchemy import Filter
|
||||||
|
from pydantic.json_schema import SkipJsonSchema
|
||||||
|
from sqlmodel import Field, SQLModel, select, Relationship
|
||||||
|
from pydantic import Field as PydField
|
||||||
|
|
||||||
|
|
||||||
class AccountBase(SQLModel):
|
class AccountBase(SQLModel):
|
||||||
name: str = Field(index=True)
|
name: str = Field(index=True)
|
||||||
type: str = Field(index=True)
|
parent_account_id: Optional[UUID] = Field(default=None, foreign_key="account.id")
|
||||||
|
|
||||||
class AccountBaseId(AccountBase):
|
class AccountBaseId(AccountBase):
|
||||||
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
type: str = Field(index=True)
|
||||||
|
path: str = Field(index=True)
|
||||||
|
|
||||||
class Account(AccountBaseId, table=True):
|
class Account(AccountBaseId, table=True):
|
||||||
|
parent_account: Optional["Account"] = Relationship(
|
||||||
|
back_populates="children_accounts",
|
||||||
|
sa_relationship_kwargs=dict(remote_side='Account.id')
|
||||||
|
)
|
||||||
|
children_accounts: list["Account"] = Relationship(back_populates='parent_account')
|
||||||
|
|
||||||
|
def get_child_path(self):
|
||||||
|
return f"{self.path}/{self.name}"
|
||||||
|
|
||||||
|
def get_root_path(self):
|
||||||
|
root = "/Categories" if self.is_category() else "/Accounts"
|
||||||
|
return f"{root}/{self.type}"
|
||||||
|
|
||||||
|
def is_category(self):
|
||||||
|
return self.type in [v.value for v in CategoryType]
|
||||||
|
|
||||||
|
def get_path(self, session):
|
||||||
|
if self.parent_account_id is None:
|
||||||
|
return self.get_root_path()
|
||||||
|
|
||||||
|
parent = self.get(session, self.parent_account_id)
|
||||||
|
return parent.get_child_path()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def schema_to_model(cls, session, schema):
|
||||||
|
model = cls.model_validate(schema)
|
||||||
|
model.path = model.get_path(session)
|
||||||
|
return model
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, account, session):
|
def create(cls, account, session):
|
||||||
account_db = cls.model_validate(account)
|
account_db = cls.schema_to_model(session, account)
|
||||||
session.add(account_db)
|
session.add(account_db)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(account_db)
|
session.refresh(account_db)
|
||||||
@@ -42,10 +76,19 @@ class Account(AccountBaseId, table=True):
|
|||||||
def get(cls, session, account_id):
|
def get(cls, session, account_id):
|
||||||
return session.get(Account, account_id)
|
return session.get(Account, account_id)
|
||||||
|
|
||||||
|
def update_children_path(self, session):
|
||||||
|
for child in self.children_accounts:
|
||||||
|
child.path = self.get_child_path()
|
||||||
|
session.add(child)
|
||||||
|
child.update_children_path(session)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def update(cls, session, account_db, account_data):
|
def update(cls, session, account_db, account_data):
|
||||||
account_db.sqlmodel_update(cls.model_validate(account_data))
|
previous_path = account_db.path
|
||||||
|
account_db.sqlmodel_update(cls.schema_to_model(session, account_data))
|
||||||
session.add(account_db)
|
session.add(account_db)
|
||||||
|
if previous_path != account_db.path:
|
||||||
|
account_db.update_children_path(session, account_db)
|
||||||
session.commit()
|
session.commit()
|
||||||
session.refresh(account_db)
|
session.refresh(account_db)
|
||||||
return account_db
|
return account_db
|
||||||
@@ -84,15 +127,25 @@ class Asset(Enum):
|
|||||||
Checkings = "Checkings"
|
Checkings = "Checkings"
|
||||||
Savings = "Savings"
|
Savings = "Savings"
|
||||||
Cash = "Cash"
|
Cash = "Cash"
|
||||||
|
Investment = "Investment"
|
||||||
|
|
||||||
class Liability(Enum):
|
class Liability(Enum):
|
||||||
Liability = "Liability"
|
Liability = "Liability"
|
||||||
CreditCard = "CreditCard"
|
CreditCard = "CreditCard"
|
||||||
Loan = "Loan"
|
Loan = "Loan"
|
||||||
Investment = "Investment"
|
|
||||||
|
|
||||||
class AccountWrite(AccountBase):
|
class AccountWrite(AccountBase):
|
||||||
type: Asset | Liability = Field()
|
type: Asset | Liability = Field()
|
||||||
|
path: SkipJsonSchema[str] = Field(default="")
|
||||||
|
parent_account_id: UUID = PydField(default=None, json_schema_extra={
|
||||||
|
"foreign_key": {
|
||||||
|
"reference": {
|
||||||
|
"resource": "accounts",
|
||||||
|
"schema": "AccountRead",
|
||||||
|
"label": "name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
class AccountCreate(AccountWrite):
|
class AccountCreate(AccountWrite):
|
||||||
pass
|
pass
|
||||||
@@ -111,4 +164,11 @@ class CategoryCreate(CategoryWrite):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
class CategoryUpdate(CategoryWrite):
|
class CategoryUpdate(CategoryWrite):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class AccountFilter(Filter):
|
||||||
|
name__like: Optional[str] = None
|
||||||
|
|
||||||
|
class Constants(Filter.Constants):
|
||||||
|
model = Account
|
||||||
|
search_model_fields = ["name"]
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class PayeeBaseId(PayeeBase):
|
|||||||
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
|
||||||
|
|
||||||
class Payee(PayeeBaseId, table=True):
|
class Payee(PayeeBaseId, table=True):
|
||||||
default_account: Account | None = Relationship()
|
default_account: Optional[Account] = Relationship()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, payee, session):
|
def create(cls, payee, session):
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
|
|||||||
|
|
||||||
const valueResult = useOne({
|
const valueResult = useOne({
|
||||||
resource: resource,
|
resource: resource,
|
||||||
id: props.value
|
id: props.value != "" ? props.value : undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
const [inputValue, setInputValue] = useState<string>("");
|
const [inputValue, setInputValue] = useState<string>("");
|
||||||
@@ -42,6 +42,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
|
|||||||
onChange={(event, newValue) => {
|
onChange={(event, newValue) => {
|
||||||
setSelectedValue(newValue)
|
setSelectedValue(newValue)
|
||||||
props.onChange(newValue ? newValue.id : "")
|
props.onChange(newValue ? newValue.id : "")
|
||||||
|
return true
|
||||||
}}
|
}}
|
||||||
//inputValue={inputValue}
|
//inputValue={inputValue}
|
||||||
onInputChange={(event, newInputValue) => setInputValue(newInputValue)}
|
onInputChange={(event, newInputValue) => setInputValue(newInputValue)}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export const AccountList: React.FC = () => {
|
|||||||
() => [
|
() => [
|
||||||
{ field: "id", headerName: "ID" },
|
{ field: "id", headerName: "ID" },
|
||||||
{ field: "name", headerName: "Name", flex: 1 },
|
{ field: "name", headerName: "Name", flex: 1 },
|
||||||
|
{ field: "path", headerName: "path", flex: 1 },
|
||||||
{ field: "type", headerName: "Type", flex: 0.3 },
|
{ field: "type", headerName: "Type", flex: 0.3 },
|
||||||
{
|
{
|
||||||
field: "actions",
|
field: "actions",
|
||||||
|
|||||||
Reference in New Issue
Block a user