lnbits-legend/lnbits/extensions/admin/views_api.py

71 lines
2.5 KiB
Python
Raw Normal View History

2022-03-07 05:03:32 +00:00
from http import HTTPStatus
2022-03-12 14:18:09 +00:00
2022-03-22 11:42:28 +00:00
# from config import conf
2022-03-12 14:18:09 +00:00
from fastapi import Body, Depends, Request
from starlette.exceptions import HTTPException
2022-03-07 05:03:32 +00:00
from lnbits.core.crud import get_wallet
2022-03-12 14:18:09 +00:00
from lnbits.decorators import WalletTypeInfo, require_admin_key
from lnbits.extensions.admin import admin_ext
2022-04-14 16:37:13 +01:00
from lnbits.extensions.admin.models import Admin, Funding, UpdateAdminSettings
2022-03-22 11:42:28 +00:00
from lnbits.helpers import removeEmptyString
from lnbits.requestvars import g
2022-03-12 14:18:09 +00:00
2022-04-14 16:37:13 +01:00
from .crud import get_admin, update_admin, update_funding, update_wallet_balance
2022-03-12 14:18:09 +00:00
2022-03-07 05:03:32 +00:00
2022-03-12 14:18:09 +00:00
@admin_ext.get("/api/v1/admin/{wallet_id}/{topup_amount}", status_code=HTTPStatus.OK)
2022-03-22 10:28:44 +00:00
async def api_update_balance(wallet_id, topup_amount: int, g: WalletTypeInfo = Depends(require_admin_key)):
2022-03-07 05:03:32 +00:00
try:
wallet = await get_wallet(wallet_id)
except:
2022-03-12 14:18:09 +00:00
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not allowed: not an admin"
)
2022-03-22 11:42:28 +00:00
2022-03-22 10:28:44 +00:00
await update_wallet_balance(wallet_id=wallet_id, amount=int(topup_amount))
2022-03-12 14:18:09 +00:00
return {"status": "Success"}
2022-03-07 05:03:32 +00:00
2022-03-12 14:18:09 +00:00
@admin_ext.post("/api/v1/admin/", status_code=HTTPStatus.OK)
async def api_update_admin(
request: Request,
data: UpdateAdminSettings = Body(...),
2022-03-22 11:42:28 +00:00
w: WalletTypeInfo = Depends(require_admin_key)
2022-03-12 14:18:09 +00:00
):
2022-03-07 05:03:32 +00:00
admin = await get_admin()
2022-03-12 14:18:09 +00:00
print(data)
2022-03-22 11:42:28 +00:00
if not admin.user == w.wallet.user:
2022-03-12 14:18:09 +00:00
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not allowed: not an admin"
)
2022-03-22 11:42:28 +00:00
updated = await update_admin(user=w.wallet.user, **data.dict())
updated.admin_users = removeEmptyString(updated.admin_users.split(','))
updated.allowed_users = removeEmptyString(updated.allowed_users.split(','))
updated.admin_ext = removeEmptyString(updated.admin_ext.split(','))
updated.disabled_ext = removeEmptyString(updated.disabled_ext.split(','))
updated.theme = removeEmptyString(updated.theme.split(','))
updated.ad_space = removeEmptyString(updated.ad_space.split(','))
g().admin_conf = g().admin_conf.copy(update=updated.dict())
print(g().admin_conf)
2022-03-12 14:18:09 +00:00
return {"status": "Success"}
2022-04-14 16:37:13 +01:00
@admin_ext.post("/api/v1/admin/funding/", status_code=HTTPStatus.OK)
async def api_update_funding(
request: Request,
data: Funding = Body(...),
w: WalletTypeInfo = Depends(require_admin_key)
):
admin = await get_admin()
if not admin.user == w.wallet.user:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not allowed: not an admin"
)
funding = await update_funding(data=data)
return funding