Compare commits

..

3 Commits

Author SHA1 Message Date
fb7e46efdb Adding Parent Account validation 2025-02-02 22:55:57 +01:00
778bdc2c74 Adding family concept and routes 2025-02-02 22:09:00 +01:00
0b150abae4 Adding empty value on foreign key 2025-02-02 22:08:38 +01:00
4 changed files with 87 additions and 17 deletions

View File

@@ -19,6 +19,14 @@ def create_account(account: AccountCreate, session: SessionDep, current_user=Dep
def read_accounts(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_accounts())
@router.get("")
def read_assets(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_assets())
@router.get("")
def read_liabilities(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_liabilities())
@router.get("/{account_id}")
def read_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
account = Account.get(session, account_id)

View File

@@ -16,9 +16,17 @@ def create_category(category: CategoryCreate, session: SessionDep, current_user=
return result
@router.get("")
def read_categorys(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
def read_categories(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_categories())
@router.get("expenses")
def read_expenses(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_expenses())
@router.get("incomes")
def read_incomes(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
return paginate(session, Account.list_incomes())
@router.get("/{category_id}")
def read_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
category = Account.get(session, category_id)

View File

@@ -14,9 +14,10 @@ class AccountBase(SQLModel):
parent_account_id: Optional[UUID] = Field(default=None, foreign_key="account.id")
class AccountBaseId(AccountBase):
id: UUID | None = Field(default=uuid4(), primary_key=True)
id: UUID | None = Field(default_factory=uuid4, primary_key=True)
family: str = Field(index=True)
type: str = Field(index=True)
path: str = Field(index=True)
path: str = Field(index=True, unique=True)
class Account(AccountBaseId, table=True):
parent_account: Optional["Account"] = Relationship(
@@ -30,7 +31,7 @@ class Account(AccountBaseId, table=True):
def get_root_path(self):
root = "/Categories" if self.is_category() else "/Accounts"
return f"{root}/{self.type}/"
return f"{root}/{self.family}/"
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 }/%'"
@@ -39,28 +40,61 @@ class Account(AccountBaseId, table=True):
def is_category(self):
return self.type in [v.value for v in CategoryType]
def get_parent(self, session):
if self.parent_account_id is None:
return None
self.parent_account = self.get(session, self.parent_account_id)
return self.parent_account
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()
self.parent_account = self.get(session, self.parent_account_id)
return self.parent_account.get_child_path()
def get_family(self):
if self.type in Asset:
return "Asset"
if self.type in Liability:
return "Liability"
return self.type
@classmethod
def schema_to_model(cls, session, schema, model=None):
try:
if model:
model = cls.model_validate(model, update=schema)
else:
schema['path'] = ""
schema.path = ""
schema.family = ""
model = cls.model_validate(schema)
except Exception as e:
print(e)
model.family = model.get_family()
cls.validate_parent(session, model)
model.path = model.get_path(session)
return model
@classmethod
def validate_parent(cls, session, model):
if model.parent_account_id is None:
return True
parent = model.get_parent(session)
if not parent:
raise ValueError("Parent account not found.")
if parent.family != model.family:
raise ValueError("Account family mismatch with parent account..")
if parent.path.startswith(model.path):
raise ValueError("Parent Account is descendant")
return True
@classmethod
def create(cls, account, session):
account_db = cls.schema_to_model(session, account)
@@ -80,12 +114,28 @@ class Account(AccountBaseId, table=True):
Account.type.not_in([v.value for v in CategoryType])
)
@classmethod
def list_assets(cls):
return cls.list().where(Account.family == "Asset")
@classmethod
def list_liabilities(cls):
return cls.list().where(Account.family == "Liability")
@classmethod
def list_categories(cls):
return cls.list().where(
Account.type.in_([v.value for v in CategoryType])
)
@classmethod
def list_expenses(cls):
return cls.list().where(Account.family == "Expense")
@classmethod
def list_incomes(cls):
return cls.list().where(Account.family == "Income")
@classmethod
def get(cls, session, account_id):
return session.get(Account, account_id)
@@ -94,7 +144,7 @@ class Account(AccountBaseId, table=True):
def update(cls, session, account_db, account_data):
previous_path = account_db.path
account_db.sqlmodel_update(cls.schema_to_model(session, account_data, account_db))
if previous_path != account_db.path:
if previous_path != account_db.path or account_data['name'] != account_db.name:
account_db.update_children_path(session, previous_path)
session.add(account_db)
session.commit()
@@ -142,9 +192,12 @@ class Liability(Enum):
CreditCard = "CreditCard"
Loan = "Loan"
class AccountWrite(AccountBase):
type: Asset | Liability = Field()
class BaseAccountWrite(AccountBase):
path: SkipJsonSchema[str] = Field(default="")
family: SkipJsonSchema[str] = Field(default="")
class AccountWrite(BaseAccountWrite):
type: Asset | Liability = Field()
parent_account_id: UUID | None = PydField(default=None, json_schema_extra={
"foreign_key": {
"reference": {
@@ -165,7 +218,7 @@ class CategoryType(Enum):
Income = "Income"
Expense = "Expense"
class CategoryWrite(AccountBase):
class CategoryWrite(BaseAccountWrite):
type: CategoryType = Field()
class CategoryCreate(CategoryWrite):

View File

@@ -13,6 +13,11 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
id: props.value != null ? props.value : undefined
});
const empty_option: BaseRecord = {
id: null
}
empty_option[labelField] = "(None)"
const [inputValue, setInputValue] = useState<string>("");
const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null);
const [debouncedInputValue, setDebouncedInputValue] = useState<string>(inputValue);
@@ -31,10 +36,6 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
const options = listResult.data?.data || [];
if (! props.required) {
const empty_option: BaseRecord = {
id: null
}
empty_option[labelField] = "(None)"
options.unshift(empty_option);
}
const isLoading = listResult.isLoading || valueResult.isLoading;
@@ -47,7 +48,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
<Autocomplete
value={selectedValue}
onChange={(event, newValue) => {
setSelectedValue(newValue);
setSelectedValue(newValue ? newValue : empty_option);
props.onChange(newValue ? newValue.id : null);
return true;
}}