Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Aug 2, 2024
1 parent 9782058 commit 76c4833
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 52 deletions.
16 changes: 6 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter

from lnbits.db import Database
from lnbits.helpers import template_renderer

db = Database("ext_tipjar")
from .crud import db
from .views import tipjar_generic_router
from .views_api import tipjar_api_router

tipjar_ext: APIRouter = APIRouter(prefix="/tipjar", tags=["tipjar"])
tipjar_ext.include_router(tipjar_generic_router)
tipjar_ext.include_router(tipjar_api_router)

tipjar_static_files = [
{
Expand All @@ -15,9 +16,4 @@
]


def tipjar_renderer():
return template_renderer(["tipjar/templates"])


from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403
__all__ = ["db", "tipjar_ext", "tipjar_static_files"]
21 changes: 11 additions & 10 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import List, Optional
from typing import Optional

from lnbits.db import SQLITE
from lnbits.db import SQLITE, Database

from . import db
from .models import Tip, TipJar, createTipJar
from .models import CreateTipJar, Tip, TipJar

db = Database("ext_tipjar")


async def create_tip(
id: str, wallet: str, message: str, name: str, sats: int, tipjar: str
tip_id: str, wallet: str, message: str, name: str, sats: int, tipjar: str
) -> Tip:
"""Create a new Tip"""
await db.execute(
Expand All @@ -22,15 +23,15 @@ async def create_tip(
)
VALUES (?, ?, ?, ?, ?, ?)
""",
(id, wallet, name, message, sats, tipjar),
(tip_id, wallet, name, message, sats, tipjar),
)

tip = await get_tip(id)
tip = await get_tip(tip_id)
assert tip, "Newly created tip couldn't be retrieved"
return tip


async def create_tipjar(data: createTipJar) -> TipJar:
async def create_tipjar(data: CreateTipJar) -> TipJar:
"""Create a new TipJar"""

returning = "" if db.type == SQLITE else "RETURNING ID"
Expand All @@ -52,7 +53,7 @@ async def create_tipjar(data: createTipJar) -> TipJar:
if db.type == SQLITE:
tipjar_id = result._result_proxy.lastrowid
else:
tipjar_id = result[0]
tipjar_id = result[0] # type: ignore

tipjar = await get_tipjar(tipjar_id)
assert tipjar
Expand Down Expand Up @@ -87,7 +88,7 @@ async def get_tip(tip_id: str) -> Optional[Tip]:
return Tip(**row) if row else None


async def get_tipjar_tips(tipjar_id: int) -> List[Tip]:
async def get_tipjar_tips(tipjar_id: int) -> list[Tip]:
"""Return all Tips for a tipjar"""
rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE tipjar = ?", (tipjar_id,))
return [Tip(**row) for row in rows]
Expand Down
3 changes: 1 addition & 2 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import httpx

from lnbits.app import settings
from lnbits.settings import settings


async def create_charge(data: dict, api_key: str) -> str:
Expand Down
6 changes: 3 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydantic import BaseModel


class createTip(BaseModel):
class CreateTip(BaseModel):
id: str
wallet: str
sats: int
Expand All @@ -28,14 +28,14 @@ def from_row(cls, row: Row) -> "Tip":
return cls(**dict(row))


class createTipJar(BaseModel):
class CreateTipJar(BaseModel):
name: str
wallet: str
webhook: Optional[str]
onchain: Optional[str]


class createTips(BaseModel):
class CreateTips(BaseModel):
name: str
sats: str
tipjar: str
Expand Down
16 changes: 10 additions & 6 deletions views.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
from http import HTTPStatus

from fastapi import Depends, Request
from fastapi import APIRouter, Depends, Request
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException

from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from lnbits.helpers import template_renderer
from starlette.exceptions import HTTPException

from . import tipjar_ext, tipjar_renderer
from .crud import get_tipjar

templates = Jinja2Templates(directory="templates")
tipjar_generic_router = APIRouter()


def tipjar_renderer():
return template_renderer(["tipjar/templates"])


@tipjar_ext.get("/")
@tipjar_generic_router.get("/")
async def index(request: Request, user: User = Depends(check_user_exists)):
return tipjar_renderer().TemplateResponse(
"tipjar/index.html", {"request": request, "user": user.dict()}
)


@tipjar_ext.get("/{tipjar_id}")
@tipjar_generic_router.get("/{tipjar_id}")
async def tip(request: Request, tipjar_id: int):
"""Return the donation form for the Tipjar corresponding to id"""
tipjar = await get_tipjar(tipjar_id)
Expand Down
44 changes: 23 additions & 21 deletions views_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from http import HTTPStatus

from fastapi import Depends, Query
from starlette.exceptions import HTTPException

from fastapi import APIRouter, Depends, HTTPException
from lnbits.core.crud import get_user, get_wallet
from lnbits.decorators import WalletTypeInfo, get_key_type
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import get_key_type

from . import tipjar_ext
from .crud import (
create_tip,
create_tipjar,
Expand All @@ -21,16 +19,20 @@
update_tipjar,
)
from .helpers import create_charge, delete_charge
from .models import createTip, createTipJar, createTips
from .models import CreateTip, CreateTipJar, CreateTips

tipjar_api_router = APIRouter()


@tipjar_ext.post("/api/v1/tipjars")
async def api_create_tipjar(data: createTipJar):
@tipjar_api_router.post("/api/v1/tipjars")
async def api_create_tipjar(data: CreateTipJar):
"""Create a tipjar, which holds data about how/where to post tips"""
try:
tipjar = await create_tipjar(data)
except Exception as e:
raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e))
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc

return tipjar.dict()

Expand All @@ -39,8 +41,8 @@ async def user_from_wallet(wallet: WalletTypeInfo = Depends(get_key_type)):
return wallet.wallet.user


@tipjar_ext.post("/api/v1/tips")
async def api_create_tip(data: createTips):
@tipjar_api_router.post("/api/v1/tips")
async def api_create_tip(data: CreateTips):
"""Take data from tip form and return satspay charge"""
sats = int(data.sats)
message = data.message
Expand Down Expand Up @@ -82,7 +84,7 @@ async def api_create_tip(data: createTips):
)

await create_tip(
id=charge_id,
tip_id=charge_id,
wallet=tipjar.wallet,
message=message,
name=name,
Expand All @@ -93,7 +95,7 @@ async def api_create_tip(data: createTips):
return {"redirect_url": f"/satspay/{charge_id}"}


@tipjar_ext.get("/api/v1/tipjars")
@tipjar_api_router.get("/api/v1/tipjars")
async def api_get_tipjars(wallet: WalletTypeInfo = Depends(get_key_type)):
"""Return list of all tipjars assigned to wallet with given invoice key"""
user = await get_user(wallet.wallet.user)
Expand All @@ -106,7 +108,7 @@ async def api_get_tipjars(wallet: WalletTypeInfo = Depends(get_key_type)):
return [tipjar.dict() for tipjar in tipjars]


@tipjar_ext.get("/api/v1/tips")
@tipjar_api_router.get("/api/v1/tips")
async def api_get_tips(wallet: WalletTypeInfo = Depends(get_key_type)):
"""Return list of all tips assigned to wallet with given invoice key"""
user = await get_user(wallet.wallet.user)
Expand All @@ -119,9 +121,9 @@ async def api_get_tips(wallet: WalletTypeInfo = Depends(get_key_type)):
return [tip.dict() for tip in tips]


@tipjar_ext.put("/api/v1/tips/{tip_id}")
@tipjar_api_router.put("/api/v1/tips/{tip_id}")
async def api_update_tip(
data: createTip,
data: CreateTip,
tip_id: str,
wallet: WalletTypeInfo = Depends(get_key_type),
):
Expand All @@ -147,9 +149,9 @@ async def api_update_tip(
return tip.dict()


@tipjar_ext.put("/api/v1/tipjars/{tipjar_id}")
@tipjar_api_router.put("/api/v1/tipjars/{tipjar_id}")
async def api_update_tipjar(
data: createTipJar,
data: CreateTipJar,
tipjar_id: int,
wallet: WalletTypeInfo = Depends(get_key_type),
):
Expand All @@ -175,7 +177,7 @@ async def api_update_tipjar(
return tipjar.dict()


@tipjar_ext.delete("/api/v1/tips/{tip_id}")
@tipjar_api_router.delete("/api/v1/tips/{tip_id}")
async def api_delete_tip(tip_id: str, wallet: WalletTypeInfo = Depends(get_key_type)):
"""Delete the tip with the given tip_id"""
tip = await get_tip(tip_id)
Expand All @@ -194,7 +196,7 @@ async def api_delete_tip(tip_id: str, wallet: WalletTypeInfo = Depends(get_key_t
return "", HTTPStatus.NO_CONTENT


@tipjar_ext.delete("/api/v1/tipjars/{tipjar_id}")
@tipjar_api_router.delete("/api/v1/tipjars/{tipjar_id}")
async def api_delete_tipjar(
tipjar_id: int, wallet: WalletTypeInfo = Depends(get_key_type)
):
Expand Down

0 comments on commit 76c4833

Please sign in to comment.