diff --git a/__pycache__/__init__.cpython-39.pyc b/__pycache__/__init__.cpython-39.pyc index 4eb254d..467dbcc 100644 Binary files a/__pycache__/__init__.cpython-39.pyc and b/__pycache__/__init__.cpython-39.pyc differ diff --git a/__pycache__/crud.cpython-39.pyc b/__pycache__/crud.cpython-39.pyc index 2e195a0..78b148c 100644 Binary files a/__pycache__/crud.cpython-39.pyc and b/__pycache__/crud.cpython-39.pyc differ diff --git a/__pycache__/lnurl.cpython-39.pyc b/__pycache__/lnurl.cpython-39.pyc index f5cae4b..1fe4cd5 100644 Binary files a/__pycache__/lnurl.cpython-39.pyc and b/__pycache__/lnurl.cpython-39.pyc differ diff --git a/__pycache__/migrations.cpython-39.pyc b/__pycache__/migrations.cpython-39.pyc index 57756e2..eb1535d 100644 Binary files a/__pycache__/migrations.cpython-39.pyc and b/__pycache__/migrations.cpython-39.pyc differ diff --git a/__pycache__/models.cpython-39.pyc b/__pycache__/models.cpython-39.pyc index 40cedbd..8830466 100644 Binary files a/__pycache__/models.cpython-39.pyc and b/__pycache__/models.cpython-39.pyc differ diff --git a/__pycache__/tasks.cpython-39.pyc b/__pycache__/tasks.cpython-39.pyc index c2a8302..c4ca1db 100644 Binary files a/__pycache__/tasks.cpython-39.pyc and b/__pycache__/tasks.cpython-39.pyc differ diff --git a/__pycache__/views.cpython-39.pyc b/__pycache__/views.cpython-39.pyc index ad52342..ca80ade 100644 Binary files a/__pycache__/views.cpython-39.pyc and b/__pycache__/views.cpython-39.pyc differ diff --git a/__pycache__/views_api.cpython-39.pyc b/__pycache__/views_api.cpython-39.pyc index 848aa24..a2ad321 100644 Binary files a/__pycache__/views_api.cpython-39.pyc and b/__pycache__/views_api.cpython-39.pyc differ diff --git a/crud.py b/crud.py index f02bf92..a6bfe88 100644 --- a/crud.py +++ b/crud.py @@ -1,7 +1,7 @@ from typing import List, Optional, Union from lnbits.helpers import urlsafe_short_hash - +from lnbits.lnurl import encode as lnurl_encode from . import db from .models import CreateMyExtensionData, MyExtension from loguru import logger @@ -28,9 +28,14 @@ async def create_myextension(wallet_id: str, data: CreateMyExtensionData) -> MyE return myextension -async def get_myextension(myextension_id: str) -> Optional[MyExtension]: +async def get_myextension(myextension_id: str, req: Request) -> Optional[MyExtension]: row = await db.fetchone("SELECT * FROM myextension.maintable WHERE id = ?", (myextension_id,)) - return MyExtension(**row) if row else None + if not row: + return None + rowAmended = MyExtension(**row) + rowAmended.lnurlpay = lnurl_encode(req.url_for("myextension.api_lnurl_pay", myextension_id=row.id)._url) + rowAmended.lnurlwithdraw = lnurl_encode(req.url_for("myextension.api_lnurl_withdraw", myextension_id=row.id)._url) + return rowAmended async def get_myextensions(wallet_ids: Union[str, List[str]], req: Request) -> List[MyExtension]: if isinstance(wallet_ids, str): @@ -41,10 +46,9 @@ async def get_myextensions(wallet_ids: Union[str, List[str]], req: Request) -> L f"SELECT * FROM myextension.maintable WHERE wallet IN ({q})", (*wallet_ids,) ) 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("myextension.api_lnurl_pay", myextension_id=row.id) - row.lnurlwithdraw = req.url_for("myextension.api_lnurl_withdraw", myextension_id=row.id) + row.lnurlpay = lnurl_encode(req.url_for("myextension.api_lnurl_pay", myextension_id=row.id)._url) + row.lnurlwithdraw = lnurl_encode(req.url_for("myextension.api_lnurl_withdraw", myextension_id=row.id)._url) return tempRows async def update_myextension(myextension_id: str, **kwargs) -> MyExtension: diff --git a/lnurl.py b/lnurl.py index 93305e5..058e2d6 100644 --- a/lnurl.py +++ b/lnurl.py @@ -1,5 +1,6 @@ -# Maybe your extensions needs some LNURL stuff, if so checkout LNURLp/LNURLw extensions/lnurl library in LNbits (to keep things simple the below examples are raw LNURLs) - +# Maybe your extensions needs some LNURL stuff. +# Here is a very simple example of how to do it. +# Feel free to delete this file if you don't need it. from http import HTTPStatus from fastapi import Depends, Query, Request diff --git a/models.py b/models.py index fc44926..513fe77 100644 --- a/models.py +++ b/models.py @@ -18,13 +18,13 @@ class CreateMyExtensionData(BaseModel): class MyExtension(BaseModel): id: str - wallet: str - name: str + wallet: Optional[str] + name: Optional[str] total: Optional[int] lnurlpayamount: Optional[int] lnurlwithdrawamount: Optional[int] - lnurlpay: str - lnurlwithdraw: str + lnurlpay: Optional[str] + lnurlwithdraw: Optional[str] @classmethod def from_row(cls, row: Row) -> "MyExtension": diff --git a/templates/myextension/index.html b/templates/myextension/index.html index 131f81a..0bd9e57 100644 --- a/templates/myextension/index.html +++ b/templates/myextension/index.html @@ -67,17 +67,6 @@ target="_blank" >Open public page - - Click to copy${props.row.id.substring(0,6)}... - @@ -163,22 +152,42 @@ + + - - -
-

- ${ urlDialog.data.lnurlpay }
${ - urlDialog.data.shareUrl } -

+ + +
+
+ + + +
+
+ lnurlpay + +
+
+ lnurlwithdraw + +
+
+ + + + +
+ +
- Copy URL + Close
@@ -186,6 +195,7 @@
{% endblock %} {% block scripts %} {{ window_vars(user) }} {% endblock %} diff --git a/views_api.py b/views_api.py index c270ed2..800ab76 100644 --- a/views_api.py +++ b/views_api.py @@ -38,7 +38,9 @@ from .models import CreateMyExtensionData @myextension_ext.get("/api/v1/temps", status_code=HTTPStatus.OK) async def api_myextensions( - req: Request, all_wallets: bool = Query(False), wallet: WalletTypeInfo = Depends(get_key_type) + req: Request, all_wallets: + bool = Query(False), + wallet: WalletTypeInfo = Depends(get_key_type) ): wallet_ids = [wallet.wallet.id] if all_wallets: @@ -46,14 +48,27 @@ async def api_myextensions( wallet_ids = user.wallet_ids if user else [] return [myextension.dict() for myextension in await get_myextensions(wallet_ids, req)] +## Get a single record -## Get a specific record belonging to a user +@myextension_ext.get("/api/v1/temps/{myextension_id}", status_code=HTTPStatus.OK) +async def api_myextension( + req: Request, + myextension_id: str, + WalletTypeInfo = Depends(get_key_type)): + myextension = await get_myextension(myextension_id, req) + if not myextension: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="MyExtension does not exist." + ) + return myextension.dict() + +## update a record @myextension_ext.put("/api/v1/temps/{myextension_id}") async def api_myextension_update( data: CreateMyExtensionData, myextension_id: str, - wallet: WalletTypeInfo = Depends(require_admin_key), + wallet: WalletTypeInfo = Depends(get_key_type), ): if not myextension_id: raise HTTPException(