From c7717a611a06ab357982f32627a06248e92c2b15 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 7 Mar 2021 17:03:01 -0300 Subject: [PATCH] UI to change the wordlist. --- lnbits/extensions/offlineshop/crud.py | 7 +++++ .../extensions/offlineshop/static/js/index.js | 19 +++++++++++++ .../templates/offlineshop/index.html | 27 +++++++++++++++++++ lnbits/extensions/offlineshop/views_api.py | 23 ++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/lnbits/extensions/offlineshop/crud.py b/lnbits/extensions/offlineshop/crud.py index 0544edeaa..2d9376dd0 100644 --- a/lnbits/extensions/offlineshop/crud.py +++ b/lnbits/extensions/offlineshop/crud.py @@ -32,6 +32,13 @@ async def get_or_create_shop_by_wallet(wallet: str) -> Optional[Shop]: return Shop(**dict(row)) if row else None +async def set_wordlist(shop: int, wordlist: str) -> Optional[Shop]: + await db.execute( + "UPDATE shops SET wordlist = ? WHERE id = ?", (wordlist, shop), + ) + return await get_shop(shop) + + async def add_item(shop: int, name: str, description: str, image: Optional[str], price: int, unit: str,) -> int: result = await db.execute( """ diff --git a/lnbits/extensions/offlineshop/static/js/index.js b/lnbits/extensions/offlineshop/static/js/index.js index a55cf4368..84b6f8650 100644 --- a/lnbits/extensions/offlineshop/static/js/index.js +++ b/lnbits/extensions/offlineshop/static/js/index.js @@ -85,6 +85,25 @@ new Vue({ LNbits.utils.notifyApiError(err) }) }, + async updateWordlist() { + try { + await LNbits.api.request( + 'PUT', + '/offlineshop/api/v1/offlineshop/wordlist', + this.selectedWallet.inkey, + {wordlist: this.offlineshop.wordlist} + ) + this.$q.notify({ + message: `Wordlist updated. Counter reset.`, + timeout: 700 + }) + } catch (err) { + LNbits.utils.notifyApiError(err) + return + } + + this.loadShop() + }, async sendItem() { let {id, name, image, description, price, unit} = this.itemDialog.data const data = { diff --git a/lnbits/extensions/offlineshop/templates/offlineshop/index.html b/lnbits/extensions/offlineshop/templates/offlineshop/index.html index 6ede54544..8de5a8ede 100644 --- a/lnbits/extensions/offlineshop/templates/offlineshop/index.html +++ b/lnbits/extensions/offlineshop/templates/offlineshop/index.html @@ -90,6 +90,10 @@ +
+
Wallet Shop
+
+
+ + + +
+
Wordlist
+
+ +
+
+ +
+
+ + Update Wordlist + + Reset +
+
+
+
+
diff --git a/lnbits/extensions/offlineshop/views_api.py b/lnbits/extensions/offlineshop/views_api.py index 430b23f0e..41aa2cf6f 100644 --- a/lnbits/extensions/offlineshop/views_api.py +++ b/lnbits/extensions/offlineshop/views_api.py @@ -7,11 +7,13 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request from . import offlineshop_ext from .crud import ( get_or_create_shop_by_wallet, + set_wordlist, add_item, update_item, get_items, delete_item_from_shop, ) +from .models import ShopCounter @offlineshop_ext.route("/api/v1/offlineshop", methods=["GET"]) @@ -70,3 +72,24 @@ async def api_delete_item(item_id): shop = await get_or_create_shop_by_wallet(g.wallet.id) await delete_item_from_shop(shop.id, item_id) return "", HTTPStatus.NO_CONTENT + + +@offlineshop_ext.route("/api/v1/offlineshop/wordlist", methods=["PUT"]) +@api_check_wallet_key("invoice") +@api_validate_post_request( + schema={"wordlist": {"type": "string", "empty": True, "nullable": True, "required": True},} +) +async def api_set_wordlist(): + wordlist = g.data["wordlist"].split("\n") if g.data["wordlist"] else None + wordlist = [word.strip() for word in wordlist if word.strip()] + + shop = await get_or_create_shop_by_wallet(g.wallet.id) + if not shop: + return "", HTTPStatus.NOT_FOUND + + updated_shop = await set_wordlist(shop.id, "\n".join(wordlist)) + if not updated_shop: + return "", HTTPStatus.NOT_FOUND + + ShopCounter.reset(updated_shop) + return "", HTTPStatus.OK