lnbits-legend/lnbits/extensions/captcha/crud.py

52 lines
1.4 KiB
Python
Raw Normal View History

2020-12-30 15:50:08 -08:00
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import Captcha
async def create_captcha(
*,
wallet_id: str,
url: str,
memo: str,
description: Optional[str] = None,
amount: int = 0,
remembers: bool = True,
2020-12-30 15:50:08 -08:00
) -> Captcha:
captcha_id = urlsafe_short_hash()
await db.execute(
"""
2021-06-21 23:22:52 -03:00
INSERT INTO captcha.captchas (id, wallet, url, memo, description, amount, remembers)
2020-12-30 15:50:08 -08:00
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(captcha_id, wallet_id, url, memo, description, amount, int(remembers)),
)
captcha = await get_captcha(captcha_id)
assert captcha, "Newly created captcha couldn't be retrieved"
return captcha
async def get_captcha(captcha_id: str) -> Optional[Captcha]:
2021-06-21 23:22:52 -03:00
row = await db.fetchone("SELECT * FROM captcha.captchas WHERE id = ?", (captcha_id,))
2020-12-30 15:50:08 -08:00
return Captcha.from_row(row) if row else None
async def get_captchas(wallet_ids: Union[str, List[str]]) -> List[Captcha]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
2021-06-21 23:22:52 -03:00
f"SELECT * FROM captcha.captchas WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-12-30 15:50:08 -08:00
return [Captcha.from_row(row) for row in rows]
async def delete_captcha(captcha_id: str) -> None:
2021-06-21 23:22:52 -03:00
await db.execute("DELETE FROM captcha.captchas WHERE id = ?", (captcha_id,))