From 220dc699cf50e1b296b87a34e1c6ac61d882a966 Mon Sep 17 00:00:00 2001 From: Black Coffee Date: Mon, 24 Oct 2022 15:53:43 +0100 Subject: [PATCH] Settings now save and load and have an insert or delete for new / existing settings --- lnbits/extensions/satspay/crud.py | 13 ++++++++++++ lnbits/extensions/satspay/models.py | 4 ++++ .../satspay/templates/satspay/index.html | 20 ++++++++++++++++++- lnbits/extensions/satspay/views_api.py | 12 +++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lnbits/extensions/satspay/crud.py b/lnbits/extensions/satspay/crud.py index 3e45f773..c6f0d5b8 100644 --- a/lnbits/extensions/satspay/crud.py +++ b/lnbits/extensions/satspay/crud.py @@ -102,6 +102,19 @@ async def get_charges(user: str) -> List[Charges]: ) return [Charges.from_row(row) for row in rows] +async def get_settings(user: str) -> SatsPaySettings: + row = await db.fetchone( + """SELECT * FROM satspay.settings WHERE "user" = ?""", + (user,), + ) + logger.debug('Settings data') + logger.debug(row) + + if row: + return SatsPaySettings.from_row(row) + else: + return None + async def delete_charge(charge_id: str) -> None: await db.execute("DELETE FROM satspay.charges WHERE id = ?", (charge_id,)) diff --git a/lnbits/extensions/satspay/models.py b/lnbits/extensions/satspay/models.py index 2c391f1f..d5f01b21 100644 --- a/lnbits/extensions/satspay/models.py +++ b/lnbits/extensions/satspay/models.py @@ -74,3 +74,7 @@ class Charges(BaseModel): return self.webhook and self.paid and self.config.webhook_success == False class SatsPaySettings(BaseModel): custom_css: str = Query(None) + + @classmethod + def from_row(cls, row: Row) -> "SatsPaySettings": + return cls(**dict(row)) diff --git a/lnbits/extensions/satspay/templates/satspay/index.html b/lnbits/extensions/satspay/templates/satspay/index.html index 648da73f..2c2d2eca 100644 --- a/lnbits/extensions/satspay/templates/satspay/index.html +++ b/lnbits/extensions/satspay/templates/satspay/index.html @@ -9,7 +9,7 @@ >New charge - SatsPay settings @@ -442,6 +442,7 @@ mixins: [windowMixin], data: function () { return { + settings: {}, filter: '', balance: null, walletLinks: [], @@ -622,6 +623,21 @@ LNbits.utils.notifyApiError(error) } }, + + getSettings: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + '/satspay/api/v1/settings', + this.g.user.wallets[0].inkey + ) + console.log('Settings data', data) + this.formDialogSettings.data = data + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, + sendFormDataSettings: function () { const wallet = this.g.user.wallets[0].inkey const data = this.formDialogSettings.data @@ -700,6 +716,7 @@ } }, saveSettings: async function (wallet, data) { + console.log('wallet', wallet) try { const resp = await LNbits.api.request( 'POST', @@ -768,6 +785,7 @@ } }, created: async function () { + await this.getSettings() await this.getCharges() await this.getWalletConfig() await this.getWalletLinks() diff --git a/lnbits/extensions/satspay/views_api.py b/lnbits/extensions/satspay/views_api.py index ee9bb997..5731a7e4 100644 --- a/lnbits/extensions/satspay/views_api.py +++ b/lnbits/extensions/satspay/views_api.py @@ -22,6 +22,7 @@ from .crud import ( get_charges, update_charge, save_settings, + get_settings, ) from .helpers import call_webhook, public_charge from .helpers import compact_charge @@ -153,3 +154,14 @@ async def api_settings_save( logger.debug(wallet.wallet.user) await save_settings(user=wallet.wallet.user, data=data) return True + + +@satspay_ext.get("/api/v1/settings") +async def api_settings_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): + logger.debug('HERHEHRHEHRHEHR') + try: + return await get_settings(wallet.wallet.user) + except HTTPException: + logger.error('Error loading satspay settings') + logger.error(HTTPException) + return ""