black
This commit is contained in:
@@ -5,10 +5,8 @@ from lnbits.helpers import template_renderer
|
||||
|
||||
db = Database("ext_paywall")
|
||||
|
||||
paywall_ext: APIRouter = APIRouter(
|
||||
prefix="/paywall",
|
||||
tags=["Paywall"]
|
||||
)
|
||||
paywall_ext: APIRouter = APIRouter(prefix="/paywall", tags=["Paywall"])
|
||||
|
||||
|
||||
def paywall_renderer():
|
||||
return template_renderer(["lnbits/extensions/paywall/templates"])
|
||||
|
||||
@@ -6,17 +6,22 @@ from . import db
|
||||
from .models import CreatePaywall, Paywall
|
||||
|
||||
|
||||
async def create_paywall(
|
||||
wallet_id: str,
|
||||
data: CreatePaywall
|
||||
) -> Paywall:
|
||||
async def create_paywall(wallet_id: str, data: CreatePaywall) -> Paywall:
|
||||
paywall_id = urlsafe_short_hash()
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO paywall.paywalls (id, wallet, url, memo, description, amount, remembers)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(paywall_id, wallet_id, data.url, data.memo, data.description, data.amount, int(data.remembers)),
|
||||
(
|
||||
paywall_id,
|
||||
wallet_id,
|
||||
data.url,
|
||||
data.memo,
|
||||
data.description,
|
||||
data.amount,
|
||||
int(data.remembers),
|
||||
),
|
||||
)
|
||||
|
||||
paywall = await get_paywall(paywall_id)
|
||||
|
||||
@@ -13,12 +13,15 @@ class CreatePaywall(BaseModel):
|
||||
amount: int = Query(..., ge=0)
|
||||
remembers: bool = Query(...)
|
||||
|
||||
|
||||
class CreatePaywallInvoice(BaseModel):
|
||||
amount: int = Query(..., ge=1)
|
||||
|
||||
|
||||
class CheckPaywallInvoice(BaseModel):
|
||||
payment_hash: str = Query(...)
|
||||
|
||||
|
||||
class Paywall(BaseModel):
|
||||
id: str
|
||||
wallet: str
|
||||
|
||||
@@ -13,7 +13,9 @@ from .crud import get_paywall
|
||||
|
||||
@paywall_ext.get("/")
|
||||
async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||
return paywall_renderer().TemplateResponse("paywall/index.html", {"request": request, "user": user.dict()})
|
||||
return paywall_renderer().TemplateResponse(
|
||||
"paywall/index.html", {"request": request, "user": user.dict()}
|
||||
)
|
||||
|
||||
|
||||
@paywall_ext.get("/{paywall_id}")
|
||||
@@ -23,4 +25,6 @@ async def display(request: Request, paywall_id):
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Paywall does not exist."
|
||||
)
|
||||
return paywall_renderer().TemplateResponse("paywall/display.html", {"request": request, "paywall": paywall})
|
||||
return paywall_renderer().TemplateResponse(
|
||||
"paywall/display.html", {"request": request, "paywall": paywall}
|
||||
)
|
||||
|
||||
@@ -13,7 +13,9 @@ from .models import CheckPaywallInvoice, CreatePaywall, CreatePaywallInvoice
|
||||
|
||||
|
||||
@paywall_ext.get("/api/v1/paywalls")
|
||||
async def api_paywalls(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
async def api_paywalls(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
|
||||
):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if all_wallets:
|
||||
@@ -23,47 +25,51 @@ async def api_paywalls(wallet: WalletTypeInfo = Depends(get_key_type), all_walle
|
||||
|
||||
|
||||
@paywall_ext.post("/api/v1/paywalls")
|
||||
async def api_paywall_create(data: CreatePaywall, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_paywall_create(
|
||||
data: CreatePaywall, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
paywall = await create_paywall(wallet_id=wallet.wallet.id, data=data)
|
||||
return paywall.dict()
|
||||
|
||||
|
||||
@paywall_ext.delete("/api/v1/paywalls/{paywall_id}")
|
||||
async def api_paywall_delete(paywall_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_paywall_delete(
|
||||
paywall_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
paywall = await get_paywall(paywall_id)
|
||||
|
||||
if not paywall:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Paywall does not exist."
|
||||
)
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Paywall does not exist."
|
||||
)
|
||||
|
||||
if paywall.wallet != wallet.wallet.id:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="Not your paywall."
|
||||
)
|
||||
status_code=HTTPStatus.FORBIDDEN, detail="Not your paywall."
|
||||
)
|
||||
|
||||
await delete_paywall(paywall_id)
|
||||
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
@paywall_ext.post("/api/v1/paywalls/{paywall_id}/invoice")
|
||||
async def api_paywall_create_invoice(paywall_id, data: CreatePaywallInvoice, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_paywall_create_invoice(
|
||||
paywall_id,
|
||||
data: CreatePaywallInvoice,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
):
|
||||
paywall = await get_paywall(paywall_id)
|
||||
print("PAYW", paywall)
|
||||
print("DATA", data)
|
||||
|
||||
|
||||
if data.amount < paywall.amount:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=f"Minimum amount is {paywall.amount} sat."
|
||||
)
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
detail=f"Minimum amount is {paywall.amount} sat.",
|
||||
)
|
||||
|
||||
try:
|
||||
amount = (
|
||||
data.amount if data.amount > paywall.amount else paywall.amount
|
||||
)
|
||||
amount = data.amount if data.amount > paywall.amount else paywall.amount
|
||||
payment_hash, payment_request = await create_invoice(
|
||||
wallet_id=paywall.wallet,
|
||||
amount=amount,
|
||||
@@ -71,10 +77,7 @@ async def api_paywall_create_invoice(paywall_id, data: CreatePaywallInvoice, wal
|
||||
extra={"tag": "paywall"},
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
detail=str(e)
|
||||
)
|
||||
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
|
||||
|
||||
return {"payment_hash": payment_hash, "payment_request": payment_request}
|
||||
|
||||
@@ -85,9 +88,8 @@ async def api_paywal_check_invoice(data: CheckPaywallInvoice, paywall_id):
|
||||
payment_hash = data.payment_hash
|
||||
if not paywall:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Paywall does not exist."
|
||||
)
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Paywall does not exist."
|
||||
)
|
||||
|
||||
try:
|
||||
status = await check_invoice_status(paywall.wallet, payment_hash)
|
||||
|
||||
Reference in New Issue
Block a user