Compare commits
3 Commits
1e8731d78b
...
fb7e46efdb
| Author | SHA1 | Date | |
|---|---|---|---|
| fb7e46efdb | |||
| 778bdc2c74 | |||
| 0b150abae4 |
@@ -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]:
|
def read_accounts(session: SessionDep, current_user=Depends(get_current_user)) -> Page[AccountRead]:
|
||||||
return paginate(session, Account.list_accounts())
|
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}")
|
@router.get("/{account_id}")
|
||||||
def read_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
|
def read_account(account_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
|
||||||
account = Account.get(session, account_id)
|
account = Account.get(session, account_id)
|
||||||
|
|||||||
@@ -16,9 +16,17 @@ def create_category(category: CategoryCreate, session: SessionDep, current_user=
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
@router.get("")
|
@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())
|
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}")
|
@router.get("/{category_id}")
|
||||||
def read_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
|
def read_category(category_id: UUID, session: SessionDep, current_user=Depends(get_current_user)) -> AccountRead:
|
||||||
category = Account.get(session, category_id)
|
category = Account.get(session, category_id)
|
||||||
|
|||||||
@@ -14,9 +14,10 @@ class AccountBase(SQLModel):
|
|||||||
parent_account_id: Optional[UUID] = Field(default=None, foreign_key="account.id")
|
parent_account_id: Optional[UUID] = Field(default=None, foreign_key="account.id")
|
||||||
|
|
||||||
class AccountBaseId(AccountBase):
|
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)
|
type: str = Field(index=True)
|
||||||
path: str = Field(index=True)
|
path: str = Field(index=True, unique=True)
|
||||||
|
|
||||||
class Account(AccountBaseId, table=True):
|
class Account(AccountBaseId, table=True):
|
||||||
parent_account: Optional["Account"] = Relationship(
|
parent_account: Optional["Account"] = Relationship(
|
||||||
@@ -30,7 +31,7 @@ class Account(AccountBaseId, table=True):
|
|||||||
|
|
||||||
def get_root_path(self):
|
def get_root_path(self):
|
||||||
root = "/Categories" if self.is_category() else "/Accounts"
|
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):
|
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 }/%'"
|
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):
|
def is_category(self):
|
||||||
return self.type in [v.value for v in CategoryType]
|
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):
|
def get_path(self, session):
|
||||||
if self.parent_account_id is None:
|
if self.parent_account_id is None:
|
||||||
return self.get_root_path()
|
return self.get_root_path()
|
||||||
|
|
||||||
parent = self.get(session, self.parent_account_id)
|
self.parent_account = self.get(session, self.parent_account_id)
|
||||||
return parent.get_child_path()
|
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
|
@classmethod
|
||||||
def schema_to_model(cls, session, schema, model=None):
|
def schema_to_model(cls, session, schema, model=None):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if model:
|
if model:
|
||||||
model = cls.model_validate(model, update=schema)
|
model = cls.model_validate(model, update=schema)
|
||||||
else:
|
else:
|
||||||
schema['path'] = ""
|
schema.path = ""
|
||||||
|
schema.family = ""
|
||||||
model = cls.model_validate(schema)
|
model = cls.model_validate(schema)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
model.family = model.get_family()
|
||||||
|
cls.validate_parent(session, model)
|
||||||
model.path = model.get_path(session)
|
model.path = model.get_path(session)
|
||||||
return model
|
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
|
@classmethod
|
||||||
def create(cls, account, session):
|
def create(cls, account, session):
|
||||||
account_db = cls.schema_to_model(session, account)
|
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])
|
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
|
@classmethod
|
||||||
def list_categories(cls):
|
def list_categories(cls):
|
||||||
return cls.list().where(
|
return cls.list().where(
|
||||||
Account.type.in_([v.value for v in CategoryType])
|
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
|
@classmethod
|
||||||
def get(cls, session, account_id):
|
def get(cls, session, account_id):
|
||||||
return session.get(Account, 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):
|
def update(cls, session, account_db, account_data):
|
||||||
previous_path = account_db.path
|
previous_path = account_db.path
|
||||||
account_db.sqlmodel_update(cls.schema_to_model(session, account_data, account_db))
|
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)
|
account_db.update_children_path(session, previous_path)
|
||||||
session.add(account_db)
|
session.add(account_db)
|
||||||
session.commit()
|
session.commit()
|
||||||
@@ -142,9 +192,12 @@ class Liability(Enum):
|
|||||||
CreditCard = "CreditCard"
|
CreditCard = "CreditCard"
|
||||||
Loan = "Loan"
|
Loan = "Loan"
|
||||||
|
|
||||||
class AccountWrite(AccountBase):
|
class BaseAccountWrite(AccountBase):
|
||||||
type: Asset | Liability = Field()
|
|
||||||
path: SkipJsonSchema[str] = Field(default="")
|
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={
|
parent_account_id: UUID | None = PydField(default=None, json_schema_extra={
|
||||||
"foreign_key": {
|
"foreign_key": {
|
||||||
"reference": {
|
"reference": {
|
||||||
@@ -165,7 +218,7 @@ class CategoryType(Enum):
|
|||||||
Income = "Income"
|
Income = "Income"
|
||||||
Expense = "Expense"
|
Expense = "Expense"
|
||||||
|
|
||||||
class CategoryWrite(AccountBase):
|
class CategoryWrite(BaseAccountWrite):
|
||||||
type: CategoryType = Field()
|
type: CategoryType = Field()
|
||||||
|
|
||||||
class CategoryCreate(CategoryWrite):
|
class CategoryCreate(CategoryWrite):
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
|
|||||||
id: props.value != null ? props.value : undefined
|
id: props.value != null ? props.value : undefined
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const empty_option: BaseRecord = {
|
||||||
|
id: null
|
||||||
|
}
|
||||||
|
empty_option[labelField] = "(None)"
|
||||||
|
|
||||||
const [inputValue, setInputValue] = useState<string>("");
|
const [inputValue, setInputValue] = useState<string>("");
|
||||||
const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null);
|
const [selectedValue, setSelectedValue] = useState(valueResult.data?.data || null);
|
||||||
const [debouncedInputValue, setDebouncedInputValue] = useState<string>(inputValue);
|
const [debouncedInputValue, setDebouncedInputValue] = useState<string>(inputValue);
|
||||||
@@ -31,10 +36,6 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
|
|||||||
|
|
||||||
const options = listResult.data?.data || [];
|
const options = listResult.data?.data || [];
|
||||||
if (! props.required) {
|
if (! props.required) {
|
||||||
const empty_option: BaseRecord = {
|
|
||||||
id: null
|
|
||||||
}
|
|
||||||
empty_option[labelField] = "(None)"
|
|
||||||
options.unshift(empty_option);
|
options.unshift(empty_option);
|
||||||
}
|
}
|
||||||
const isLoading = listResult.isLoading || valueResult.isLoading;
|
const isLoading = listResult.isLoading || valueResult.isLoading;
|
||||||
@@ -47,7 +48,7 @@ export const ForeignKeyWidget = (props: WidgetProps) => {
|
|||||||
<Autocomplete
|
<Autocomplete
|
||||||
value={selectedValue}
|
value={selectedValue}
|
||||||
onChange={(event, newValue) => {
|
onChange={(event, newValue) => {
|
||||||
setSelectedValue(newValue);
|
setSelectedValue(newValue ? newValue : empty_option);
|
||||||
props.onChange(newValue ? newValue.id : null);
|
props.onChange(newValue ? newValue.id : null);
|
||||||
return true;
|
return true;
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user