db.add changes
Added _add_model helper function to reduce code duplication Added add_accounts for future use
This commit is contained in:
parent
9f64305050
commit
6bc8eb1413
@ -5,27 +5,42 @@ from sqlalchemy.future import Engine
|
|||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
|
|
||||||
def add_account(engine: Engine, account: models.Account) -> bool:
|
def _add_model(
|
||||||
"""Adds account to the database. Returns true on success,
|
session: sqlmodel.Session, model: models.Account | models.MasterPass
|
||||||
|
) -> bool:
|
||||||
|
"""Adds model to the session. Returns true on success,
|
||||||
false otherwise"""
|
false otherwise"""
|
||||||
try:
|
try:
|
||||||
with sqlmodel.Session(engine) as session:
|
session.add(model)
|
||||||
session.add(account)
|
|
||||||
session.commit()
|
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def add_account(engine: Engine, account: models.Account) -> bool:
|
||||||
|
"""Adds account to the database. Returns true on success,
|
||||||
|
false otherwise"""
|
||||||
|
with sqlmodel.Session(engine) as session:
|
||||||
|
result = _add_model(session, account)
|
||||||
|
session.commit()
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def add_master_pass(engine: Engine, master_pass: models.MasterPass) -> bool:
|
def add_master_pass(engine: Engine, master_pass: models.MasterPass) -> bool:
|
||||||
"""Adds master password the database. Returns true on success,
|
"""Adds master password the database. Returns true on success,
|
||||||
false otherwise"""
|
false otherwise"""
|
||||||
try:
|
with sqlmodel.Session(engine) as session:
|
||||||
with sqlmodel.Session(engine) as session:
|
result = _add_model(session, master_pass)
|
||||||
session.add(master_pass)
|
session.commit()
|
||||||
session.commit()
|
return result
|
||||||
except IntegrityError:
|
|
||||||
return False
|
|
||||||
else:
|
def add_accounts(
|
||||||
return True
|
engine: Engine,
|
||||||
|
accounts: list[models.Account],
|
||||||
|
) -> list[bool]:
|
||||||
|
with sqlmodel.Session(engine) as session:
|
||||||
|
result = [_add_model(session, account) for account in accounts]
|
||||||
|
session.commit()
|
||||||
|
return result
|
||||||
|
Reference in New Issue
Block a user