UI to change the wordlist.

This commit is contained in:
fiatjaf
2021-03-07 17:03:01 -03:00
parent 773103f893
commit c7717a611a
4 changed files with 76 additions and 0 deletions

View File

@@ -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(
"""

View File

@@ -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 = {

View File

@@ -90,6 +90,10 @@
<q-card class="q-pa-sm col-5">
<q-card-section class="q-pa-none text-center">
<div class="row">
<h5 class="text-subtitle1 q-my-none">Wallet Shop</h5>
</div>
<q-form class="q-gutter-md">
<q-select
filled
@@ -114,6 +118,29 @@
</div>
</q-card-section>
</q-card>
<q-card class="q-pa-sm col-5">
<q-card-section class="q-pa-none text-center">
<div class="row">
<h5 class="text-subtitle1 q-my-none">Wordlist</h5>
</div>
<q-form class="q-gutter-md q-y-md" @submit="updateWordlist">
<div class="row">
<div class="col q-mx-lg">
<q-input v-model="offlineshop.wordlist" dense filled autogrow />
</div>
<div class="col q-mx-lg">
<q-btn unelevated color="deep-purple" type="submit">
Update Wordlist
</q-btn>
<q-btn @click="loadShop" flat color="grey" class="q-ml-auto"
>Reset</q-btn
>
</div>
</div>
</q-form>
</q-card-section>
</q-card>
</div>
<div class="col-12 col-md-5 q-gutter-y-md">

View File

@@ -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