lnbits-legend/lnbits/extensions/withdraw/crud.py
fiatjaf d3fc52cd49 migrate to sqlalchemy-aio.
a big refactor that:

- fixes some issues that might have happened (or not) with asynchronous
    reactions to payments;
- paves the way to https://github.com/lnbits/lnbits/issues/121;
- uses more async/await notation which just looks nice; and
- makes it simple(r?) for one extension to modify stuff from other extensions.
2020-11-21 23:02:14 -03:00

100 lines
2.8 KiB
Python

from datetime import datetime
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import WithdrawLink
async def create_withdraw_link(
*,
wallet_id: str,
title: str,
min_withdrawable: int,
max_withdrawable: int,
uses: int,
wait_time: int,
is_unique: bool,
usescsv: str,
) -> WithdrawLink:
link_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO withdraw_link (
id,
wallet,
title,
min_withdrawable,
max_withdrawable,
uses,
wait_time,
is_unique,
unique_hash,
k1,
open_time,
usescsv
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
link_id,
wallet_id,
title,
min_withdrawable,
max_withdrawable,
uses,
wait_time,
int(is_unique),
urlsafe_short_hash(),
urlsafe_short_hash(),
int(datetime.now().timestamp()) + wait_time,
usescsv,
),
)
link = await get_withdraw_link(link_id, 0)
assert link, "Newly created link couldn't be retrieved"
return link
async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
row = await db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
link = []
for item in row:
link.append(item)
link.append(num)
return WithdrawLink._make(link)
async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
row = await db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,))
link = []
for item in row:
link.append(item)
link.append(num)
return WithdrawLink._make(link)
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(f"SELECT * FROM withdraw_link WHERE wallet IN ({q})", (*wallet_ids,))
return [WithdrawLink.from_row(row) for row in rows]
async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(f"UPDATE withdraw_link SET {q} WHERE id = ?", (*kwargs.values(), link_id))
row = await db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
return WithdrawLink.from_row(row) if row else None
async def delete_withdraw_link(link_id: str) -> None:
await db.execute("DELETE FROM withdraw_link WHERE id = ?", (link_id,))
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i : i + n]