I want them to turn black
This commit is contained in:
@@ -21,12 +21,9 @@ withdraw_ext: APIRouter = APIRouter(
|
||||
# "withdraw", __name__, static_folder="static", template_folder="templates"
|
||||
)
|
||||
|
||||
|
||||
def withdraw_renderer():
|
||||
return template_renderer(
|
||||
[
|
||||
"lnbits/extensions/withdraw/templates",
|
||||
]
|
||||
)
|
||||
return template_renderer(["lnbits/extensions/withdraw/templates"])
|
||||
|
||||
|
||||
from .views_api import * # noqa
|
||||
|
||||
@@ -7,9 +7,7 @@ from .models import WithdrawLink, HashCheck, CreateWithdrawData
|
||||
|
||||
|
||||
async def create_withdraw_link(
|
||||
data: CreateWithdrawData,
|
||||
wallet_id: str,
|
||||
usescsv: str,
|
||||
data: CreateWithdrawData, wallet_id: str, usescsv: str
|
||||
) -> WithdrawLink:
|
||||
link_id = urlsafe_short_hash()
|
||||
await db.execute(
|
||||
@@ -115,10 +113,7 @@ def chunks(lst, n):
|
||||
yield lst[i : i + n]
|
||||
|
||||
|
||||
async def create_hash_check(
|
||||
the_hash: str,
|
||||
lnurl_id: str,
|
||||
) -> HashCheck:
|
||||
async def create_hash_check(the_hash: str, lnurl_id: str) -> HashCheck:
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO withdraw.hash_check (
|
||||
@@ -127,10 +122,7 @@ async def create_hash_check(
|
||||
)
|
||||
VALUES (?, ?)
|
||||
""",
|
||||
(
|
||||
the_hash,
|
||||
lnurl_id,
|
||||
),
|
||||
(the_hash, lnurl_id),
|
||||
)
|
||||
hashCheck = await get_hash_check(the_hash, lnurl_id)
|
||||
return hashCheck
|
||||
|
||||
@@ -14,14 +14,17 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link
|
||||
# FOR LNURLs WHICH ARE NOT UNIQUE
|
||||
|
||||
|
||||
@withdraw_ext.get("/api/v1/lnurl/{unique_hash}", status_code=HTTPStatus.OK, name="withdraw.api_lnurl_response")
|
||||
@withdraw_ext.get(
|
||||
"/api/v1/lnurl/{unique_hash}",
|
||||
status_code=HTTPStatus.OK,
|
||||
name="withdraw.api_lnurl_response",
|
||||
)
|
||||
async def api_lnurl_response(request: Request, unique_hash):
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Withdraw link does not exist."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
||||
)
|
||||
# return ({"status": "ERROR", "reason": "LNURL-withdraw not found."},
|
||||
# HTTPStatus.OK,
|
||||
@@ -39,23 +42,22 @@ async def api_lnurl_response(request: Request, unique_hash):
|
||||
return link.lnurl_response(request).dict()
|
||||
|
||||
|
||||
|
||||
# CALLBACK
|
||||
|
||||
|
||||
@withdraw_ext.get("/api/v1/lnurl/cb/{unique_hash}", name="withdraw.api_lnurl_callback")
|
||||
async def api_lnurl_callback(request: Request,
|
||||
unique_hash: str=Query(...),
|
||||
k1: str = Query(...),
|
||||
payment_request: str = Query(..., alias="pr")
|
||||
):
|
||||
async def api_lnurl_callback(
|
||||
request: Request,
|
||||
unique_hash: str = Query(...),
|
||||
k1: str = Query(...),
|
||||
payment_request: str = Query(..., alias="pr"),
|
||||
):
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
now = int(datetime.now().timestamp())
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="LNURL-withdraw not found."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
||||
)
|
||||
# return (
|
||||
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
|
||||
@@ -73,10 +75,7 @@ async def api_lnurl_callback(request: Request,
|
||||
# )
|
||||
|
||||
if link.k1 != k1:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail="Bad request."
|
||||
)
|
||||
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.")
|
||||
# return {"status": "ERROR", "reason": "Bad request."}, HTTPStatus.OK
|
||||
|
||||
if now < link.open_time:
|
||||
@@ -123,17 +122,21 @@ async def api_lnurl_callback(request: Request,
|
||||
|
||||
return {"status": "OK"}
|
||||
|
||||
|
||||
# FOR LNURLs WHICH ARE UNIQUE
|
||||
|
||||
|
||||
@withdraw_ext.get("/api/v1/lnurl/{unique_hash}/{id_unique_hash}", status_code=HTTPStatus.OK, name="withdraw.api_lnurl_multi_response")
|
||||
@withdraw_ext.get(
|
||||
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
|
||||
status_code=HTTPStatus.OK,
|
||||
name="withdraw.api_lnurl_multi_response",
|
||||
)
|
||||
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="LNURL-withdraw not found."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
||||
)
|
||||
# return (
|
||||
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
|
||||
@@ -158,8 +161,7 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
|
||||
found = True
|
||||
if not found:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="LNURL-withdraw not found."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
|
||||
)
|
||||
# return (
|
||||
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
|
||||
|
||||
@@ -5,13 +5,14 @@ from sqlite3 import Row
|
||||
from pydantic import BaseModel
|
||||
import shortuuid # type: ignore
|
||||
|
||||
|
||||
class CreateWithdrawData(BaseModel):
|
||||
title: str = Query(...)
|
||||
min_withdrawable: int = Query(..., ge=1)
|
||||
max_withdrawable: int = Query(..., ge=1)
|
||||
uses: int = Query(..., ge=1)
|
||||
wait_time: int = Query(..., ge=1)
|
||||
is_unique: bool
|
||||
title: str = Query(...)
|
||||
min_withdrawable: int = Query(..., ge=1)
|
||||
max_withdrawable: int = Query(..., ge=1)
|
||||
uses: int = Query(..., ge=1)
|
||||
wait_time: int = Query(..., ge=1)
|
||||
is_unique: bool
|
||||
|
||||
|
||||
class WithdrawLink(BaseModel):
|
||||
@@ -49,17 +50,15 @@ class WithdrawLink(BaseModel):
|
||||
url = req.url_for(
|
||||
"withdraw.api_lnurl_multi_response",
|
||||
unique_hash=self.unique_hash,
|
||||
id_unique_hash=multihash
|
||||
id_unique_hash=multihash,
|
||||
)
|
||||
else:
|
||||
url = req.url_for(
|
||||
"withdraw.api_lnurl_response",
|
||||
unique_hash=self.unique_hash
|
||||
"withdraw.api_lnurl_response", unique_hash=self.unique_hash
|
||||
)
|
||||
|
||||
return lnurl_encode(url)
|
||||
|
||||
|
||||
def lnurl_response(self, req: Request) -> LnurlWithdrawResponse:
|
||||
url = req.url_for(
|
||||
name="withdraw.api_lnurl_callback", unique_hash=self.unique_hash
|
||||
|
||||
@@ -15,11 +15,14 @@ from lnbits.core.models import User
|
||||
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
|
||||
@withdraw_ext.get("/", response_class=HTMLResponse)
|
||||
# @validate_uuids(["usr"], required=True)
|
||||
# @check_user_exists()
|
||||
async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||
return withdraw_renderer().TemplateResponse("withdraw/index.html", {"request":request,"user": user.dict()})
|
||||
return withdraw_renderer().TemplateResponse(
|
||||
"withdraw/index.html", {"request": request, "user": user.dict()}
|
||||
)
|
||||
|
||||
|
||||
@withdraw_ext.get("/{link_id}", response_class=HTMLResponse)
|
||||
@@ -28,13 +31,20 @@ async def display(request: Request, link_id):
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Withdraw link does not exist."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return "Withdraw link does not exist." #probably here is where we should return the 404??
|
||||
print("LINK", link)
|
||||
return withdraw_renderer().TemplateResponse("withdraw/display.html", {"request":request,"link":link.dict(), "lnurl": link.lnurl(req=request), "unique":True})
|
||||
return withdraw_renderer().TemplateResponse(
|
||||
"withdraw/display.html",
|
||||
{
|
||||
"request": request,
|
||||
"link": link.dict(),
|
||||
"lnurl": link.lnurl(req=request),
|
||||
"unique": True,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@withdraw_ext.get("/img/{link_id}", response_class=StreamingResponse)
|
||||
@@ -42,8 +52,7 @@ async def img(request: Request, link_id):
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Withdraw link does not exist."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return "Withdraw link does not exist."
|
||||
@@ -63,7 +72,8 @@ async def img(request: Request, link_id):
|
||||
"Cache-Control": "no-cache, no-store, must-revalidate",
|
||||
"Pragma": "no-cache",
|
||||
"Expires": "0",
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@withdraw_ext.get("/print/{link_id}", response_class=HTMLResponse)
|
||||
@@ -71,15 +81,17 @@ async def print_qr(request: Request, link_id):
|
||||
link = await get_withdraw_link(link_id)
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Withdraw link does not exist."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return "Withdraw link does not exist."
|
||||
|
||||
if link.uses == 0:
|
||||
|
||||
return withdraw_renderer().TemplateResponse("withdraw/print_qr.html", {"request":request,"link":link.dict(), unique:False})
|
||||
return withdraw_renderer().TemplateResponse(
|
||||
"withdraw/print_qr.html",
|
||||
{"request": request, "link": link.dict(), unique: False},
|
||||
)
|
||||
links = []
|
||||
count = 0
|
||||
|
||||
@@ -87,8 +99,7 @@ async def print_qr(request: Request, link_id):
|
||||
linkk = await get_withdraw_link(link_id, count)
|
||||
if not linkk:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Withdraw link does not exist."
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Withdraw link does not exist."
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return "Withdraw link does not exist."
|
||||
@@ -97,4 +108,6 @@ async def print_qr(request: Request, link_id):
|
||||
page_link = list(chunks(links, 2))
|
||||
linked = list(chunks(page_link, 5))
|
||||
print("LINKED", linked)
|
||||
return withdraw_renderer().TemplateResponse("withdraw/print_qr.html", {"request":request,"link":linked, "unique":True})
|
||||
return withdraw_renderer().TemplateResponse(
|
||||
"withdraw/print_qr.html", {"request": request, "link": linked, "unique": True}
|
||||
)
|
||||
|
||||
@@ -28,7 +28,11 @@ from .crud import (
|
||||
|
||||
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
|
||||
# @api_check_wallet_key("invoice")
|
||||
async def api_links(req: Request, wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
async def api_links(
|
||||
req: Request,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
all_wallets: bool = Query(False),
|
||||
):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if all_wallets:
|
||||
@@ -36,12 +40,9 @@ async def api_links(req: Request, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
|
||||
try:
|
||||
return [
|
||||
{
|
||||
**link.dict(),
|
||||
**{"lnurl": link.lnurl(req)},
|
||||
}
|
||||
for link in await get_withdraw_links(wallet_ids)
|
||||
]
|
||||
{**link.dict(), **{"lnurl": link.lnurl(req)}}
|
||||
for link in await get_withdraw_links(wallet_ids)
|
||||
]
|
||||
|
||||
except LnurlInvalidUrl:
|
||||
raise HTTPException(
|
||||
@@ -59,21 +60,20 @@ async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_ty
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
detail="Withdraw link does not exist.",
|
||||
status_code=HTTPStatus.NOT_FOUND
|
||||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return {"message": "Withdraw link does not exist."}
|
||||
|
||||
if link.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.",
|
||||
status_code=HTTPStatus.FORBIDDEN
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
# response.status_code = HTTPStatus.FORBIDDEN
|
||||
# return {"message": "Not your withdraw link."}
|
||||
return {**link, **{"lnurl": link.lnurl(request)}}
|
||||
|
||||
|
||||
# class CreateData(BaseModel):
|
||||
# title: str = Query(...)
|
||||
# min_withdrawable: int = Query(..., ge=1)
|
||||
@@ -82,14 +82,20 @@ async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_ty
|
||||
# wait_time: int = Query(..., ge=1)
|
||||
# is_unique: bool
|
||||
|
||||
|
||||
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
||||
@withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
||||
# @api_check_wallet_key("admin")
|
||||
async def api_link_create_or_update(req: Request, data: CreateWithdrawData, link_id: str = None, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_link_create_or_update(
|
||||
req: Request,
|
||||
data: CreateWithdrawData,
|
||||
link_id: str = None,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
):
|
||||
if data.max_withdrawable < data.min_withdrawable:
|
||||
raise HTTPException(
|
||||
detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
|
||||
status_code=HTTPStatus.BAD_REQUEST
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
)
|
||||
# response.status_code = HTTPStatus.BAD_REQUEST
|
||||
# return {
|
||||
@@ -108,15 +114,13 @@ async def api_link_create_or_update(req: Request, data: CreateWithdrawData, link
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
detail="Withdraw link does not exist.",
|
||||
status_code=HTTPStatus.NOT_FOUND
|
||||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return {"message": "Withdraw link does not exist."}
|
||||
if link.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.",
|
||||
status_code=HTTPStatus.FORBIDDEN
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
# response.status_code = HTTPStatus.FORBIDDEN
|
||||
# return {"message": "Not your withdraw link."}
|
||||
@@ -137,16 +141,14 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
detail="Withdraw link does not exist.",
|
||||
status_code=HTTPStatus.NOT_FOUND
|
||||
detail="Withdraw link does not exist.", status_code=HTTPStatus.NOT_FOUND
|
||||
)
|
||||
# response.status_code = HTTPStatus.NOT_FOUND
|
||||
# return {"message": "Withdraw link does not exist."}
|
||||
|
||||
if link.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
detail="Not your withdraw link.",
|
||||
status_code=HTTPStatus.FORBIDDEN
|
||||
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
|
||||
)
|
||||
# response.status_code = HTTPStatus.FORBIDDEN
|
||||
# return {"message": "Not your withdraw link."}
|
||||
@@ -158,6 +160,8 @@ async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type
|
||||
|
||||
@withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK)
|
||||
# @api_check_wallet_key("invoice")
|
||||
async def api_hash_retrieve(the_hash, lnurl_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_hash_retrieve(
|
||||
the_hash, lnurl_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
hashCheck = await get_hash_check(the_hash, lnurl_id)
|
||||
return hashCheck
|
||||
|
||||
Reference in New Issue
Block a user