From 0711f583d6b98043c2b792cf71fd880e0995220f Mon Sep 17 00:00:00 2001 From: benarc Date: Tue, 16 Jan 2024 16:38:53 +0000 Subject: [PATCH] changed name to myextension --- README.md | 8 +- __init__.py | 13 ++- config.json | 4 +- crud.py | 48 ++++----- lnurl.py | 98 +++++++++--------- manifest.json | 4 +- migrations.py | 8 +- models.py | 11 +- static/image/{temp.png => myextension.png} | Bin tasks.py | 22 ++-- .../{temp => myextension}/_api_docs.html | 20 ++-- .../_myextension.html} | 4 +- templates/{temp => myextension}/index.html | 62 +++++------ .../myextension.html} | 0 views.py | 56 +++++----- views_api.py | 88 ++++++++-------- 16 files changed, 221 insertions(+), 225 deletions(-) rename static/image/{temp.png => myextension.png} (100%) rename templates/{temp => myextension}/_api_docs.html (78%) rename templates/{temp/_temp.html => myextension/_myextension.html} (62%) rename templates/{temp => myextension}/index.html (85%) rename templates/{temp/temp.html => myextension/myextension.html} (100%) diff --git a/README.md b/README.md index 46afad4..a8fd38c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ `The README.md typically serves as a guide for using the extension.` -# Temp - An [LNbits](https://github.com/lnbits/lnbits) Extension +# MyExtension - An [LNbits](https://github.com/lnbits/lnbits) Extension ## A Starter Template for Your Own Extension @@ -9,11 +9,11 @@ Ready to start hacking? Once you've forked this extension, you can incorporate f ### How to Use This Template > This guide assumes you're using this extension as a base for a new one, and have installed LNbits using https://github.com/lnbits/lnbits/blob/main/docs/guide/installation.md#option-1-recommended-poetry. -1. Install and enable the extension either through the official LNbits manifest or by adding https://raw.githubusercontent.com/lnbits/temp/main/manifest.json to `"Server"/"Server"/"Extension Sources"`. ![Extension Sources](https://i.imgur.com/MUGwAU3.png) ![Extension Enable](https://i.imgur.com/hHXn6d2.png) +1. Install and enable the extension either through the official LNbits manifest or by adding https://raw.githubusercontent.com/lnbits/myextension/main/manifest.json to `"Server"/"Server"/"Extension Sources"`. ![Extension Sources](https://i.imgur.com/MUGwAU3.png) ![Extension Enable](https://i.imgur.com/hHXn6d2.png) 2. `Ctrl c` shut down your LNbits installation. -3. Download the extension files from https://github.com/lnbits/temp to a folder outside of `/lnbits`, and initialize the folder with `git`. Alternatively, create a repo, copy the temp extension files into it, then `git clone` the extension to a location outside of `/lnbits`. +3. Download the extension files from https://github.com/lnbits/myextension to a folder outside of `/lnbits`, and initialize the folder with `git`. Alternatively, create a repo, copy the myextension extension files into it, then `git clone` the extension to a location outside of `/lnbits`. 4. Remove the installed extension from `lnbits/lnbits/extensions`. 5. Create a symbolic link using `ln -s /home/ben/Projects/ /home/ben/Projects/lnbits/lnbits/extensions`. 6. Restart your LNbits installation. You can now modify your extension and `git push` changes to a repo. -7. When you're ready to share your manifest so others can install it, edit `/lnbits/temp/manifest.json` to include the git credentials of your extension. +7. When you're ready to share your manifest so others can install it, edit `/lnbits/myextension/manifest.json` to include the git credentials of your extension. 8. IMPORTANT: If you want your extension to be added to the official LNbits manifest, please follow the guidelines here: https://github.com/lnbits/lnbits-extensions#important \ No newline at end of file diff --git a/__init__.py b/__init__.py index 879bc30..be5149f 100644 --- a/__init__.py +++ b/__init__.py @@ -6,28 +6,27 @@ from lnbits.db import Database from lnbits.helpers import template_renderer from lnbits.tasks import catch_everything_and_restart -db = Database("ext_tempextension") +db = Database("ext_myextension") -temp_ext: APIRouter = APIRouter( - prefix="/temp", tags=["Temp"] +myextension_ext: APIRouter = APIRouter( + prefix="/myextension", tags=["MyExtension"] ) temp_static_files = [ { - "path": "/temp/static", + "path": "/myextension/static", "name": "temp_static", } ] -def temp_renderer(): - return template_renderer(["temp/templates"]) +def myextension_renderer(): + return template_renderer(["myextension/templates"]) from .lnurl import * from .tasks import wait_for_paid_invoices from .views import * from .views_api import * - def temp_start(): loop = asyncio.get_event_loop() loop.create_task(catch_everything_and_restart(wait_for_paid_invoices)) \ No newline at end of file diff --git a/config.json b/config.json index eb94eab..4a75bb2 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { - "name": "Temp", + "name": "MyExtension", "short_description": "Minimal extension to build on", - "tile": "/temp/static/image/temp.png", + "tile": "/myextension/static/image/myextension.png", "contributors": ["arcbtc"], "min_lnbits_version": "0.0.1" } diff --git a/crud.py b/crud.py index 778555c..f02bf92 100644 --- a/crud.py +++ b/crud.py @@ -3,58 +3,58 @@ from typing import List, Optional, Union from lnbits.helpers import urlsafe_short_hash from . import db -from .models import CreateTempData, Temp +from .models import CreateMyExtensionData, MyExtension from loguru import logger from fastapi import Request from lnurl import encode as lnurl_encode -async def create_temp(wallet_id: str, data: CreateTempData) -> Temp: - temp_id = urlsafe_short_hash() +async def create_myextension(wallet_id: str, data: CreateMyExtensionData) -> MyExtension: + myextension_id = urlsafe_short_hash() await db.execute( """ - INSERT INTO tempextension.temp (id, wallet, name, lnurlpayamount, lnurlwithdrawamount) + INSERT INTO myextension.maintable (id, wallet, name, lnurlpayamount, lnurlwithdrawamount) VALUES (?, ?, ?, ?, ?) """, ( - temp_id, + myextension_id, wallet_id, data.name, data.lnurlpayamount, data.lnurlwithdrawamount ), ) - temp = await get_temp(temp_id) - assert temp, "Newly created temp couldn't be retrieved" - return temp + myextension = await get_myextension(myextension_id) + assert myextension, "Newly created table couldn't be retrieved" + return myextension -async def get_temp(temp_id: str) -> Optional[Temp]: - row = await db.fetchone("SELECT * FROM tempextension.temp WHERE id = ?", (temp_id,)) - return Temp(**row) if row else None +async def get_myextension(myextension_id: str) -> Optional[MyExtension]: + row = await db.fetchone("SELECT * FROM myextension.maintable WHERE id = ?", (myextension_id,)) + return MyExtension(**row) if row else None -async def get_temps(wallet_ids: Union[str, List[str]], req: Request) -> List[Temp]: +async def get_myextensions(wallet_ids: Union[str, List[str]], req: Request) -> List[MyExtension]: if isinstance(wallet_ids, str): wallet_ids = [wallet_ids] q = ",".join(["?"] * len(wallet_ids)) rows = await db.fetchall( - f"SELECT * FROM tempextension.temp WHERE wallet IN ({q})", (*wallet_ids,) + f"SELECT * FROM myextension.maintable WHERE wallet IN ({q})", (*wallet_ids,) ) - tempRows = [Temp(**row) for row in rows] - logger.debug(req.url_for("temp.api_lnurl_pay", temp_id=row.id)) + tempRows = [MyExtension(**row) for row in rows] + logger.debug(req.url_for("myextension.api_lnurl_pay", myextension_id=row.id)) for row in tempRows: - row.lnurlpay = req.url_for("temp.api_lnurl_pay", temp_id=row.id) - row.lnurlwithdraw = req.url_for("temp.api_lnurl_withdraw", temp_id=row.id) + row.lnurlpay = req.url_for("myextension.api_lnurl_pay", myextension_id=row.id) + row.lnurlwithdraw = req.url_for("myextension.api_lnurl_withdraw", myextension_id=row.id) return tempRows -async def update_temp(temp_id: str, **kwargs) -> Temp: +async def update_myextension(myextension_id: str, **kwargs) -> MyExtension: q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) await db.execute( - f"UPDATE tempextension.temp SET {q} WHERE id = ?", (*kwargs.values(), temp_id) + f"UPDATE myextension.maintable SET {q} WHERE id = ?", (*kwargs.values(), myextension_id) ) - temp = await get_temp(temp_id) - assert temp, "Newly updated temp couldn't be retrieved" - return temp + myextension = await get_myextension(myextension_id) + assert myextension, "Newly updated myextension couldn't be retrieved" + return myextension -async def delete_temp(temp_id: str) -> None: - await db.execute("DELETE FROM tempextension.temp WHERE id = ?", (temp_id,)) \ No newline at end of file +async def delete_myextension(myextension_id: str) -> None: + await db.execute("DELETE FROM myextension.maintable WHERE id = ?", (myextension_id,)) \ No newline at end of file diff --git a/lnurl.py b/lnurl.py index ecb0045..93305e5 100644 --- a/lnurl.py +++ b/lnurl.py @@ -3,8 +3,8 @@ from http import HTTPStatus from fastapi import Depends, Query, Request -from . import temp_ext -from .crud import get_temp +from . import myextension_ext +from .crud import get_myextension from lnbits.core.services import create_invoice @@ -14,48 +14,48 @@ from lnbits.core.services import create_invoice ################################################# ################################################# -@temp_ext.get( - "/api/v1/lnurl/pay/{temp_id}", +@myextension_ext.get( + "/api/v1/lnurl/pay/{myextension_id}", status_code=HTTPStatus.OK, - name="temp.api_lnurl_pay", + name="myextension.api_lnurl_pay", ) async def api_lnurl_pay( request: Request, - temp_id: str, + myextension_id: str, ): - temp = await get_temp(temp_id) - if not temp: - return {"status": "ERROR", "reason": "No temp found"} + myextension = await get_myextension(myextension_id) + if not myextension: + return {"status": "ERROR", "reason": "No myextension found"} return { - "callback": str(request.url_for("temp.api_lnurl_pay_callback", temp_id=temp_id)), - "maxSendable": temp.lnurlpayamount, - "minSendable": temp.lnurlpayamount, - "metadata":"[[\"text/plain\", \"" + temp.name + "\"]]", + "callback": str(request.url_for("myextension.api_lnurl_pay_callback", myextension_id=myextension_id)), + "maxSendable": myextension.lnurlpayamount, + "minSendable": myextension.lnurlpayamount, + "metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", "tag": "payRequest" } -@temp_ext.get( - "/api/v1/lnurl/pay/cb/{temp_id}", +@myextension_ext.get( + "/api/v1/lnurl/pay/cb/{myextension_id}", status_code=HTTPStatus.OK, - name="temp.api_lnurl_pay_callback", + name="myextension.api_lnurl_pay_callback", ) async def api_lnurl_pay_cb( request: Request, - temp_id: str, + myextension_id: str, amount: int = Query(...), ): - temp = await get_temp(temp_id) - if not temp: - return {"status": "ERROR", "reason": "No temp found"} + myextension = await get_myextension(myextension_id) + if not myextension: + return {"status": "ERROR", "reason": "No myextension found"} payment_request = await create_invoice( - wallet_id=temp.wallet, + wallet_id=myextension.wallet, amount=int(amount / 1000), - memo=temp.name, - unhashed_description="[[\"text/plain\", \"" + temp.name + "\"]]".encode(), + memo=myextension.name, + unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), extra= { - "tag": "temp", - "link": temp_id, + "tag": "myextension", + "link": myextension_id, "extra": request.query_params.get("amount"), }, ) @@ -68,50 +68,50 @@ async def api_lnurl_pay_cb( ################################################# -@temp_ext.get( - "/api/v1/lnurl/withdraw/{temp_id}", +@myextension_ext.get( + "/api/v1/lnurl/withdraw/{myextension_id}", status_code=HTTPStatus.OK, - name="temp.api_lnurl_withdraw", + name="myextension.api_lnurl_withdraw", ) async def api_lnurl_pay( request: Request, - temp_id: str, + myextension_id: str, ): - temp = await get_temp(temp_id) - if not temp: - return {"status": "ERROR", "reason": "No temp found"} + myextension = await get_myextension(myextension_id) + if not myextension: + return {"status": "ERROR", "reason": "No myextension found"} return { - "callback": str(request.url_for("temp.api_lnurl_withdraw_callback", temp_id=temp_id)), - "maxSendable": temp.lnurlwithdrawamount, - "minSendable": temp.lnurlwithdrawamount, + "callback": str(request.url_for("myextension.api_lnurl_withdraw_callback", myextension_id=myextension_id)), + "maxSendable": myextension.lnurlwithdrawamount, + "minSendable": myextension.lnurlwithdrawamount, "k1": "", - "defaultDescription": temp.name, - "metadata":"[[\"text/plain\", \"" + temp.name + "\"]]", + "defaultDescription": myextension.name, + "metadata":"[[\"text/plain\", \"" + myextension.name + "\"]]", "tag": "withdrawRequest" } -@temp_ext.get( - "/api/v1/lnurl/pay/cb/{temp_id}", +@myextension_ext.get( + "/api/v1/lnurl/pay/cb/{myextension_id}", status_code=HTTPStatus.OK, - name="temp.api_lnurl_withdraw_callback", + name="myextension.api_lnurl_withdraw_callback", ) async def api_lnurl_pay_cb( request: Request, - temp_id: str, + myextension_id: str, amount: int = Query(...), ): - temp = await get_temp(temp_id) - if not temp: - return {"status": "ERROR", "reason": "No temp found"} + myextension = await get_myextension(myextension_id) + if not myextension: + return {"status": "ERROR", "reason": "No myextension found"} payment_request = await create_invoice( - wallet_id=temp.wallet, + wallet_id=myextension.wallet, amount=int(amount / 1000), - memo=temp.name, - unhashed_description="[[\"text/plain\", \"" + temp.name + "\"]]".encode(), + memo=myextension.name, + unhashed_description="[[\"text/plain\", \"" + myextension.name + "\"]]".encode(), extra= { - "tag": "temp", - "link": temp_id, + "tag": "myextension", + "link": myextension_id, "extra": request.query_params.get("amount"), }, ) diff --git a/manifest.json b/manifest.json index b31672d..3e05b16 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { "repos": [ { - "id": "temp", + "id": "myextension", "organisation": "lnbits", - "repository": "temp" + "repository": "myextension" } ] } diff --git a/migrations.py b/migrations.py index 961b933..de2300d 100644 --- a/migrations.py +++ b/migrations.py @@ -6,7 +6,7 @@ async def m001_initial(db): """ await db.execute( """ - CREATE TABLE tempextension.temp ( + CREATE TABLE myextension.maintable ( id TEXT PRIMARY KEY, wallet TEXT NOT NULL, name TEXT NOT NULL, @@ -24,7 +24,7 @@ async def m002_addtip_wallet(db): """ await db.execute( """ - ALTER TABLE tempextension.temp ADD lnurlwithdrawamount INTEGER DEFAULT 0; + ALTER TABLE myextension.maintable ADD lnurlwithdrawamount INTEGER DEFAULT 0; """ ) @@ -36,7 +36,7 @@ async def m004_addtip_wallet(db): """ await db.execute( """ - ALTER TABLE tempextension.temp ADD lnurlwithdraw TEXT; + ALTER TABLE myextension.maintable ADD lnurlwithdraw TEXT; """ ) @@ -48,6 +48,6 @@ async def m005_addtip_wallet(db): """ await db.execute( """ - ALTER TABLE tempextension.temp ADD lnurlpay TEXT; + ALTER TABLE myextension.maintable ADD lnurlpay TEXT; """ ) \ No newline at end of file diff --git a/models.py b/models.py index c2cdf60..fc44926 100644 --- a/models.py +++ b/models.py @@ -9,14 +9,14 @@ from fastapi import Request from lnbits.lnurl import encode as lnurl_encode from urllib.parse import urlparse -class CreateTempData(BaseModel): +class CreateMyExtensionData(BaseModel): wallet: Optional[str] name: Optional[str] total: Optional[int] lnurlpayamount: Optional[int] lnurlwithdrawamount: Optional[int] -class Temp(BaseModel): +class MyExtension(BaseModel): id: str wallet: str name: str @@ -27,8 +27,5 @@ class Temp(BaseModel): lnurlwithdraw: str @classmethod - def from_row(cls, row: Row) -> "Temp": - return cls(**dict(row)) - -class CreateUpdateTempData(BaseModel): - items: List[Temp] \ No newline at end of file + def from_row(cls, row: Row) -> "MyExtension": + return cls(**dict(row)) \ No newline at end of file diff --git a/static/image/temp.png b/static/image/myextension.png similarity index 100% rename from static/image/temp.png rename to static/image/myextension.png diff --git a/tasks.py b/tasks.py index 8b952ea..e7bcf3e 100644 --- a/tasks.py +++ b/tasks.py @@ -7,7 +7,7 @@ from lnbits.core.services import create_invoice, pay_invoice, websocketUpdater from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener -from .crud import get_temp +from .crud import get_myextension ####################################### @@ -29,31 +29,31 @@ async def wait_for_paid_invoices(): # do somethhing when an invoice related top this extension is paid async def on_invoice_paid(payment: Payment) -> None: - if payment.extra.get("tag") != "temp": + if payment.extra.get("tag") != "myextension": return - temp_id = payment.extra.get("tempId") - assert temp_id + myextension_id = payment.extra.get("tempId") + assert myextension_id - temp = await get_temp(temp_id) - assert temp + myextension = await get_myextension(myextension_id) + assert myextension # update something data_to_update = { - "total": temp.total + payment.amount + "total": myextension.total + payment.amount } - await update_temp(temp_id=temp_id, **data_to_update.dict()) + await update_myextension(myextension_id=myextension_id, **data_to_update.dict()) - # here we could send some data to a websocket on wss:///api/v1/ws/ + # here we could send some data to a websocket on wss:///api/v1/ws/ some_payment_data = { - "name": temp.name, + "name": myextension.name, "amount": payment.amount, "fee": payment.fee, "checking_id": payment.checking_id } - await websocketUpdater(temp_id, str(some_payment_data)) + await websocketUpdater(myextension_id, str(some_payment_data)) diff --git a/templates/temp/_api_docs.html b/templates/myextension/_api_docs.html similarity index 78% rename from templates/temp/_api_docs.html rename to templates/myextension/_api_docs.html index abe4fc0..fa2b07c 100644 --- a/templates/temp/_api_docs.html +++ b/templates/myextension/_api_docs.html @@ -4,11 +4,11 @@ label="API info" :content-inset-level="0.5" > - - + + - GET /temp/api/v1/temps + GET /myextension/api/v1/temps
Headers
{"X-Api-Key": <invoice_key>}
Body (application/json)
@@ -18,16 +18,16 @@ [<temp_object>, ...]
Curl example
curl -X GET {{ request.base_url }}temp/api/v1/temps -H "X-Api-Key: + >curl -X GET {{ request.base_url }}myextension/api/v1/temps -H "X-Api-Key: <invoice_key>"
- + - POST /temp/api/v1/temps + POST /myextension/api/v1/temps
Headers
{"X-Api-Key": <invoice_key>}
Body (application/json)
@@ -43,7 +43,7 @@ >
Curl example
curl -X POST {{ request.base_url }}temp/api/v1/temps -d '{"name": + >curl -X POST {{ request.base_url }}myextension/api/v1/temps -d '{"name": <string>, "currency": <string>}' -H "Content-type: application/json" -H "X-Api-Key: <admin_key>" @@ -55,14 +55,14 @@ group="api" dense expand-separator - label="Delete a Temp" + label="Delete a MyExtension" class="q-pb-md" > DELETE - /temp/api/v1/temps/<temp_id>
Headers
{"X-Api-Key": <admin_key>}
@@ -71,7 +71,7 @@
Curl example
curl -X DELETE {{ request.base_url - }}temp/api/v1/temps/<temp_id> -H "X-Api-Key: <admin_key>" + }}myextension/api/v1/temps/<myextension_id> -H "X-Api-Key: <admin_key>"
diff --git a/templates/temp/_temp.html b/templates/myextension/_myextension.html similarity index 62% rename from templates/temp/_temp.html rename to templates/myextension/_myextension.html index 82781cd..a2b9679 100644 --- a/templates/temp/_temp.html +++ b/templates/myextension/_myextension.html @@ -1,8 +1,8 @@ - +

- Temp extension is a basic extension that you can use to get started building your own extension + MyExtension extension is a basic extension that you can use to get started building your own extension

Created by diff --git a/templates/temp/index.html b/templates/myextension/index.html similarity index 85% rename from templates/temp/index.html rename to templates/myextension/index.html index 2ef805b..131f81a 100644 --- a/templates/temp/index.html +++ b/templates/myextension/index.html @@ -5,7 +5,7 @@ New TempNew MyExtension @@ -14,7 +14,7 @@
-
Temp
+
MyExtension
Export to CSV @@ -28,13 +28,13 @@ :columns="tempsTable.columns" :pagination.sync="tempsTable.pagination" > - + ${ col.label } - +