fix: main page and creating a user and a wallet
The wallet page will still not renders correctly, but the backend does create the user his first wallet.
This commit is contained in:
parent
de4d3b012c
commit
f119053953
3 changed files with 47 additions and 49 deletions
|
|
@ -54,20 +54,14 @@ async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[
|
||||||
""",
|
""",
|
||||||
(user_id,),
|
(user_id,),
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
return (
|
return User(
|
||||||
User(
|
id = user['id'],
|
||||||
**{
|
email = user['email'],
|
||||||
**user,
|
extensions = [e[0] for e in extensions],
|
||||||
**{
|
wallets = [Wallet(**w) for w in wallets])
|
||||||
"extensions": [e[0] for e in extensions],
|
|
||||||
"wallets": [Wallet(**w) for w in wallets],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if user
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def update_user_extension(
|
async def update_user_extension(
|
||||||
|
|
|
||||||
|
|
@ -10,22 +10,6 @@ from pydantic import BaseModel
|
||||||
from lnbits.settings import WALLET
|
from lnbits.settings import WALLET
|
||||||
|
|
||||||
|
|
||||||
class User(BaseModel):
|
|
||||||
id: str
|
|
||||||
email: str
|
|
||||||
extensions: List[str] = []
|
|
||||||
wallets: List["Wallet"] = []
|
|
||||||
password: Optional[str] = None
|
|
||||||
|
|
||||||
@property
|
|
||||||
def wallet_ids(self) -> List[str]:
|
|
||||||
return [wallet.id for wallet in self.wallets]
|
|
||||||
|
|
||||||
def get_wallet(self, wallet_id: str) -> Optional["Wallet"]:
|
|
||||||
w = [wallet for wallet in self.wallets if wallet.id == wallet_id]
|
|
||||||
return w[0] if w else None
|
|
||||||
|
|
||||||
|
|
||||||
class Wallet(BaseModel):
|
class Wallet(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
name: str
|
name: str
|
||||||
|
|
@ -73,6 +57,22 @@ class Wallet(BaseModel):
|
||||||
return await get_wallet_payment(self.id, payment_hash)
|
return await get_wallet_payment(self.id, payment_hash)
|
||||||
|
|
||||||
|
|
||||||
|
class User(BaseModel):
|
||||||
|
id: str
|
||||||
|
email: Optional[str] = None
|
||||||
|
extensions: List[str] = []
|
||||||
|
wallets: List[Wallet] = []
|
||||||
|
password: Optional[str] = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def wallet_ids(self) -> List[str]:
|
||||||
|
return [wallet.id for wallet in self.wallets]
|
||||||
|
|
||||||
|
def get_wallet(self, wallet_id: str) -> Optional["Wallet"]:
|
||||||
|
w = [wallet for wallet in self.wallets if wallet.id == wallet_id]
|
||||||
|
return w[0] if w else None
|
||||||
|
|
||||||
|
|
||||||
class Payment(BaseModel):
|
class Payment(BaseModel):
|
||||||
checking_id: str
|
checking_id: str
|
||||||
pending: bool
|
pending: bool
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
|
from lnbits.core.models import Wallet
|
||||||
from fastapi.params import Query
|
from fastapi.params import Query
|
||||||
from fastapi.routing import APIRouter
|
from fastapi.routing import APIRouter
|
||||||
|
from fastapi.responses import RedirectResponse
|
||||||
|
from fastapi import status
|
||||||
from lnbits.requestvars import g
|
from lnbits.requestvars import g
|
||||||
from os import path
|
from os import path
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
|
@ -60,11 +63,15 @@ async def extensions(enable: str, disable: str):
|
||||||
return await templates.TemplateResponse("core/extensions.html", {"request": request, "user": get_user(g.user.id)})
|
return await templates.TemplateResponse("core/extensions.html", {"request": request, "user": get_user(g.user.id)})
|
||||||
|
|
||||||
|
|
||||||
@core_html_routes.get("/wallet")
|
@core_html_routes.get("/wallet", response_class=HTMLResponse)
|
||||||
#Not sure how to validate
|
#Not sure how to validate
|
||||||
@validate_uuids(["usr", "wal"])
|
@validate_uuids(["usr", "wal"])
|
||||||
async def wallet(request: Request, usr: Optional[str] = Query(None),
|
async def wallet(request: Request,
|
||||||
wal: Optional[str]=Query(None, description=""), nme: Optional[str]=Query(None)):
|
usr: Optional[str] = Query(None),
|
||||||
|
wal: Optional[str] = Query(None),
|
||||||
|
nme: Optional[str] = Query(None),
|
||||||
|
):
|
||||||
|
|
||||||
user_id = usr
|
user_id = usr
|
||||||
wallet_id = wal
|
wallet_id = wal
|
||||||
wallet_name = nme
|
wallet_name = nme
|
||||||
|
|
@ -77,30 +84,27 @@ async def wallet(request: Request, usr: Optional[str] = Query(None),
|
||||||
# nothing: create everything
|
# nothing: create everything
|
||||||
|
|
||||||
if not user_id:
|
if not user_id:
|
||||||
user = await get_user((await create_account()).id)
|
usr = await get_user((await create_account()).id)
|
||||||
else:
|
else:
|
||||||
user = await get_user(user_id)
|
usr = await get_user(user_id)
|
||||||
if not user:
|
if not usr:
|
||||||
abort(HTTPStatus.NOT_FOUND, "User does not exist.")
|
return g().templates.TemplateResponse("error.html", {"request": request, "err": "User does not exist."})
|
||||||
return
|
|
||||||
|
|
||||||
if LNBITS_ALLOWED_USERS and user_id not in LNBITS_ALLOWED_USERS:
|
if LNBITS_ALLOWED_USERS and user_id not in LNBITS_ALLOWED_USERS:
|
||||||
abort(HTTPStatus.UNAUTHORIZED, "User not authorized.")
|
return g().templates.TemplateResponse("error.html", {"request": request, "err": "User not authorized."})
|
||||||
|
|
||||||
if not wallet_id:
|
if not wallet_id:
|
||||||
if user.wallets and not wallet_name:
|
if usr.wallets and not wallet_name:
|
||||||
wallet = user.wallets[0]
|
wal = usr.wallets[0]
|
||||||
else:
|
else:
|
||||||
wallet = await create_wallet(user_id=user.id, wallet_name=wallet_name)
|
wal = await create_wallet(user_id=usr.id, wallet_name=wallet_name)
|
||||||
|
|
||||||
return redirect(url_for("core.wallet", usr=user.id, wal=wallet.id))
|
return RedirectResponse(f"/wallet?usr={usr.id}&wal={wal.id}", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
||||||
|
|
||||||
wallet = user.get_wallet(wallet_id)
|
wal = usr.get_wallet(wallet_id)
|
||||||
if not wallet:
|
if not wal:
|
||||||
abort(HTTPStatus.FORBIDDEN, "Not your wallet.")
|
return g().templates.TemplateResponse("error.html", {"request": request, ...})
|
||||||
|
|
||||||
return await templates.TemplateResponse(
|
return g().templates.TemplateResponse(
|
||||||
"core/wallet.html", {"request":request,"user":user, "wallet":wallet, "service_fee":service_fee}
|
"core/wallet.html", {"request":request,"user":usr, "wallet":wal, "service_fee":service_fee}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue