mypyfication and format
This commit is contained in:
parent
87933d1f8b
commit
9291b54ef7
4 changed files with 84 additions and 64 deletions
|
|
@ -209,7 +209,7 @@ async def delete_shop_stall(stall_id: str) -> None:
|
||||||
###Orders
|
###Orders
|
||||||
|
|
||||||
|
|
||||||
async def create_shop_order(data: createOrder, invoiceid: str) -> Orders:
|
async def create_shop_order(data: createOrder, invoiceid: str):
|
||||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||||
method = db.execute if db.type == SQLITE else db.fetchone
|
method = db.execute if db.type == SQLITE else db.fetchone
|
||||||
|
|
||||||
|
|
@ -234,9 +234,6 @@ async def create_shop_order(data: createOrder, invoiceid: str) -> Orders:
|
||||||
return result._result_proxy.lastrowid
|
return result._result_proxy.lastrowid
|
||||||
else:
|
else:
|
||||||
return result[0]
|
return result[0]
|
||||||
# link = await get_shop_order(link.id)
|
|
||||||
# assert link, "Newly created link couldn't be retrieved"
|
|
||||||
# return link
|
|
||||||
|
|
||||||
|
|
||||||
async def create_shop_order_details(order_id: str, data: List[createOrderDetails]):
|
async def create_shop_order_details(order_id: str, data: List[createOrderDetails]):
|
||||||
|
|
@ -278,7 +275,7 @@ async def get_shop_order_invoiceid(invoice_id: str) -> Optional[Orders]:
|
||||||
return Orders(**row) if row else None
|
return Orders(**row) if row else None
|
||||||
|
|
||||||
|
|
||||||
async def set_shop_order_paid(payment_hash: str) -> Orders:
|
async def set_shop_order_paid(payment_hash: str):
|
||||||
await db.execute(
|
await db.execute(
|
||||||
"""
|
"""
|
||||||
UPDATE shop.orders
|
UPDATE shop.orders
|
||||||
|
|
@ -380,7 +377,7 @@ async def create_shop_market(data: CreateMarket):
|
||||||
return market
|
return market
|
||||||
|
|
||||||
|
|
||||||
async def create_shop_market_stalls(market_id: str, data: List[CreateMarketStalls]):
|
async def create_shop_market_stalls(market_id: str, data: List[str]):
|
||||||
for stallid in data:
|
for stallid in data:
|
||||||
id = urlsafe_short_hash()
|
id = urlsafe_short_hash()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ async def wait_for_paid_invoices():
|
||||||
|
|
||||||
|
|
||||||
async def on_invoice_paid(payment: Payment) -> None:
|
async def on_invoice_paid(payment: Payment) -> None:
|
||||||
if payment.extra.get("tag") != "shop":
|
if payment.extra and payment.extra.get("tag") != "shop":
|
||||||
return
|
return
|
||||||
|
|
||||||
order = await get_shop_order_invoiceid(payment.payment_hash)
|
order = await get_shop_order_invoiceid(payment.payment_hash)
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,14 @@ import json
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from fastapi import BackgroundTasks, Query, Request, WebSocket, WebSocketDisconnect
|
from fastapi import (
|
||||||
from fastapi.params import Depends
|
BackgroundTasks,
|
||||||
|
Depends,
|
||||||
|
Query,
|
||||||
|
Request,
|
||||||
|
WebSocket,
|
||||||
|
WebSocketDisconnect,
|
||||||
|
)
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
|
@ -42,6 +48,7 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||||
user=user.id, data=SetSettings(currency="sat", fiat_base_multiplier=1)
|
user=user.id, data=SetSettings(currency="sat", fiat_base_multiplier=1)
|
||||||
)
|
)
|
||||||
settings = await get_shop_settings(user.id)
|
settings = await get_shop_settings(user.id)
|
||||||
|
assert settings
|
||||||
return shop_renderer().TemplateResponse(
|
return shop_renderer().TemplateResponse(
|
||||||
"shop/index.html",
|
"shop/index.html",
|
||||||
{"request": request, "user": user.dict(), "currency": settings.currency},
|
{"request": request, "user": user.dict(), "currency": settings.currency},
|
||||||
|
|
@ -52,26 +59,28 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||||
async def stall(request: Request, stall_id):
|
async def stall(request: Request, stall_id):
|
||||||
stall = await get_shop_stall(stall_id)
|
stall = await get_shop_stall(stall_id)
|
||||||
products = await get_shop_products(stall_id)
|
products = await get_shop_products(stall_id)
|
||||||
zones = []
|
|
||||||
for id in stall.shippingzones.split(","):
|
|
||||||
z = await get_shop_zone(id)
|
|
||||||
z = z.dict()
|
|
||||||
zones.append({"label": z["countries"], "cost": z["cost"], "value": z["id"]})
|
|
||||||
|
|
||||||
if not stall:
|
if not stall:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND, detail="Stall does not exist."
|
status_code=HTTPStatus.NOT_FOUND, detail="Stall does not exist."
|
||||||
)
|
)
|
||||||
|
|
||||||
stall = stall.dict()
|
zones = []
|
||||||
|
for id in stall.shippingzones.split(","):
|
||||||
|
zone = await get_shop_zone(id)
|
||||||
|
assert zone
|
||||||
|
z = zone.dict()
|
||||||
|
zones.append({"label": z["countries"], "cost": z["cost"], "value": z["id"]})
|
||||||
|
|
||||||
stall["zones"] = zones
|
_stall = stall.dict()
|
||||||
|
|
||||||
|
_stall["zones"] = zones
|
||||||
|
|
||||||
return shop_renderer().TemplateResponse(
|
return shop_renderer().TemplateResponse(
|
||||||
"shop/stall.html",
|
"shop/stall.html",
|
||||||
{
|
{
|
||||||
"request": request,
|
"request": request,
|
||||||
"stall": stall,
|
"stall": _stall,
|
||||||
"products": [product.dict() for product in products],
|
"products": [product.dict() for product in products],
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
@ -109,9 +118,12 @@ async def order_chat(
|
||||||
keys: str = Query(None),
|
keys: str = Query(None),
|
||||||
):
|
):
|
||||||
stall = await get_shop_stall(merch)
|
stall = await get_shop_stall(merch)
|
||||||
|
assert stall
|
||||||
order = await get_shop_order_invoiceid(invoice_id)
|
order = await get_shop_order_invoiceid(invoice_id)
|
||||||
|
assert order
|
||||||
_order = await get_shop_order_details(order.id)
|
_order = await get_shop_order_details(order.id)
|
||||||
products = await get_shop_products(stall.id)
|
products = await get_shop_products(stall.id)
|
||||||
|
assert products
|
||||||
|
|
||||||
return shop_renderer().TemplateResponse(
|
return shop_renderer().TemplateResponse(
|
||||||
"shop/order.html",
|
"shop/order.html",
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,8 @@ from http import HTTPStatus
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from fastapi import Request
|
from fastapi import Body, Depends, Query, Request
|
||||||
from fastapi.param_functions import Body, Query
|
|
||||||
from fastapi.params import Depends
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from secp256k1 import PrivateKey, PublicKey
|
|
||||||
from starlette.exceptions import HTTPException
|
from starlette.exceptions import HTTPException
|
||||||
|
|
||||||
from lnbits.core.crud import get_user
|
from lnbits.core.crud import get_user
|
||||||
|
|
@ -63,6 +60,7 @@ from .crud import (
|
||||||
)
|
)
|
||||||
from .models import (
|
from .models import (
|
||||||
CreateMarket,
|
CreateMarket,
|
||||||
|
CreateMarketStalls,
|
||||||
Orders,
|
Orders,
|
||||||
Products,
|
Products,
|
||||||
SetSettings,
|
SetSettings,
|
||||||
|
|
@ -86,7 +84,8 @@ async def api_shop_products(
|
||||||
wallet_ids = [wallet.wallet.id]
|
wallet_ids = [wallet.wallet.id]
|
||||||
|
|
||||||
if all_stalls:
|
if all_stalls:
|
||||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
user = await get_user(wallet.wallet.user)
|
||||||
|
wallet_ids = user.wallet_ids if user else []
|
||||||
|
|
||||||
stalls = [stall.id for stall in await get_shop_stalls(wallet_ids)]
|
stalls = [stall.id for stall in await get_shop_stalls(wallet_ids)]
|
||||||
|
|
||||||
|
|
@ -106,7 +105,11 @@ async def api_shop_product_create(
|
||||||
# For fiat currencies,
|
# For fiat currencies,
|
||||||
# we multiply by data.fiat_base_multiplier (usually 100) to save the value in cents.
|
# we multiply by data.fiat_base_multiplier (usually 100) to save the value in cents.
|
||||||
settings = await get_shop_settings(user=wallet.wallet.user)
|
settings = await get_shop_settings(user=wallet.wallet.user)
|
||||||
|
assert settings
|
||||||
|
|
||||||
stall = await get_shop_stall(stall_id=data.stall)
|
stall = await get_shop_stall(stall_id=data.stall)
|
||||||
|
assert stall
|
||||||
|
|
||||||
if stall.currency != "sat":
|
if stall.currency != "sat":
|
||||||
data.price *= settings.fiat_base_multiplier
|
data.price *= settings.fiat_base_multiplier
|
||||||
|
|
||||||
|
|
@ -122,7 +125,7 @@ async def api_shop_product_create(
|
||||||
product = await update_shop_product(product_id, **data.dict())
|
product = await update_shop_product(product_id, **data.dict())
|
||||||
else:
|
else:
|
||||||
product = await create_shop_product(data=data)
|
product = await create_shop_product(data=data)
|
||||||
|
assert product
|
||||||
return product.dict()
|
return product.dict()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -136,6 +139,8 @@ async def api_shop_products_delete(
|
||||||
return {"message": "Product does not exist."}
|
return {"message": "Product does not exist."}
|
||||||
|
|
||||||
stall = await get_shop_stall(product.stall)
|
stall = await get_shop_stall(product.stall)
|
||||||
|
assert stall
|
||||||
|
|
||||||
if stall.wallet != wallet.wallet.id:
|
if stall.wallet != wallet.wallet.id:
|
||||||
return {"message": "Not your Shop."}
|
return {"message": "Not your Shop."}
|
||||||
|
|
||||||
|
|
@ -201,7 +206,8 @@ async def api_shop_stalls(
|
||||||
wallet_ids = [wallet.wallet.id]
|
wallet_ids = [wallet.wallet.id]
|
||||||
|
|
||||||
if all_wallets:
|
if all_wallets:
|
||||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
user = await get_user(wallet.wallet.user)
|
||||||
|
wallet_ids = user.wallet_ids if user else []
|
||||||
|
|
||||||
return [stall.dict() for stall in await get_shop_stalls(wallet_ids)]
|
return [stall.dict() for stall in await get_shop_stalls(wallet_ids)]
|
||||||
|
|
||||||
|
|
@ -225,7 +231,7 @@ async def api_shop_stall_create(
|
||||||
stall = await update_shop_stall(stall_id, **data.dict())
|
stall = await update_shop_stall(stall_id, **data.dict())
|
||||||
else:
|
else:
|
||||||
stall = await create_shop_stall(data=data)
|
stall = await create_shop_stall(data=data)
|
||||||
|
assert stall
|
||||||
return stall.dict()
|
return stall.dict()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -254,16 +260,17 @@ async def api_shop_orders(
|
||||||
):
|
):
|
||||||
wallet_ids = [wallet.wallet.id]
|
wallet_ids = [wallet.wallet.id]
|
||||||
if all_wallets:
|
if all_wallets:
|
||||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
user = await get_user(wallet.wallet.user)
|
||||||
|
wallet_ids = user.wallet_ids if user else []
|
||||||
|
|
||||||
orders = await get_shop_orders(wallet_ids)
|
orders = await get_shop_orders(wallet_ids)
|
||||||
if not orders:
|
if not orders:
|
||||||
return
|
return
|
||||||
orders_with_details = []
|
orders_with_details = []
|
||||||
for order in orders:
|
for order in orders:
|
||||||
order = order.dict()
|
_order = order.dict()
|
||||||
order["details"] = await get_shop_order_details(order["id"])
|
_order["details"] = await get_shop_order_details(_order["id"])
|
||||||
orders_with_details.append(order)
|
orders_with_details.append(_order)
|
||||||
try:
|
try:
|
||||||
return orders_with_details # [order for order in orders]
|
return orders_with_details # [order for order in orders]
|
||||||
# return [order.dict() for order in await get_shop_orders(wallet_ids)]
|
# return [order.dict() for order in await get_shop_orders(wallet_ids)]
|
||||||
|
|
@ -273,10 +280,12 @@ async def api_shop_orders(
|
||||||
|
|
||||||
@shop_ext.get("/api/v1/orders/{order_id}")
|
@shop_ext.get("/api/v1/orders/{order_id}")
|
||||||
async def api_shop_order_by_id(order_id: str):
|
async def api_shop_order_by_id(order_id: str):
|
||||||
order = (await get_shop_order(order_id)).dict()
|
order = await get_shop_order(order_id)
|
||||||
order["details"] = await get_shop_order_details(order_id)
|
assert order
|
||||||
|
_order = order.dict()
|
||||||
|
_order["details"] = await get_shop_order_details(order_id)
|
||||||
|
|
||||||
return order
|
return _order
|
||||||
|
|
||||||
|
|
||||||
@shop_ext.post("/api/v1/orders")
|
@shop_ext.post("/api/v1/orders")
|
||||||
|
|
@ -336,18 +345,18 @@ async def api_shop_order_delete(
|
||||||
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
@shop_ext.get("/api/v1/orders/paid/{order_id}")
|
# @shop_ext.get("/api/v1/orders/paid/{order_id}")
|
||||||
async def api_shop_order_paid(
|
# async def api_shop_order_paid(
|
||||||
order_id, wallet: WalletTypeInfo = Depends(require_admin_key)
|
# order_id, wallet: WalletTypeInfo = Depends(require_admin_key)
|
||||||
):
|
# ):
|
||||||
await db.execute(
|
# await db.execute(
|
||||||
"UPDATE shop.orders SET paid = ? WHERE id = ?",
|
# "UPDATE shop.orders SET paid = ? WHERE id = ?",
|
||||||
(
|
# (
|
||||||
True,
|
# True,
|
||||||
order_id,
|
# order_id,
|
||||||
),
|
# ),
|
||||||
)
|
# )
|
||||||
return "", HTTPStatus.OK
|
# return "", HTTPStatus.OK
|
||||||
|
|
||||||
|
|
||||||
@shop_ext.get("/api/v1/order/pubkey/{payment_hash}/{pubkey}")
|
@shop_ext.get("/api/v1/order/pubkey/{payment_hash}/{pubkey}")
|
||||||
|
|
@ -375,33 +384,33 @@ async def api_shop_order_shipped(
|
||||||
###List products based on stall id
|
###List products based on stall id
|
||||||
|
|
||||||
|
|
||||||
@shop_ext.get("/api/v1/stall/products/{stall_id}")
|
# @shop_ext.get("/api/v1/stall/products/{stall_id}")
|
||||||
async def api_shop_stall_products(
|
# async def api_shop_stall_products(
|
||||||
stall_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
# stall_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||||
):
|
# ):
|
||||||
|
|
||||||
rows = await db.fetchone("SELECT * FROM shop.stalls WHERE id = ?", (stall_id,))
|
# rows = await db.fetchone("SELECT * FROM shop.stalls WHERE id = ?", (stall_id,))
|
||||||
if not rows:
|
# if not rows:
|
||||||
return {"message": "Stall does not exist."}
|
# return {"message": "Stall does not exist."}
|
||||||
|
|
||||||
products = db.fetchone("SELECT * FROM shop.products WHERE wallet = ?", (rows[1],))
|
# products = db.fetchone("SELECT * FROM shop.products WHERE wallet = ?", (rows[1],))
|
||||||
if not products:
|
# if not products:
|
||||||
return {"message": "No products"}
|
# return {"message": "No products"}
|
||||||
|
|
||||||
return [products.dict() for products in await get_shop_products(rows[1])]
|
# return [products.dict() for products in await get_shop_products(rows[1])]
|
||||||
|
|
||||||
|
|
||||||
###Check a product has been shipped
|
###Check a product has been shipped
|
||||||
|
|
||||||
|
|
||||||
@shop_ext.get("/api/v1/stall/checkshipped/{checking_id}")
|
# @shop_ext.get("/api/v1/stall/checkshipped/{checking_id}")
|
||||||
async def api_shop_stall_checkshipped(
|
# async def api_shop_stall_checkshipped(
|
||||||
checking_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
# checking_id, wallet: WalletTypeInfo = Depends(get_key_type)
|
||||||
):
|
# ):
|
||||||
rows = await db.fetchone(
|
# rows = await db.fetchone(
|
||||||
"SELECT * FROM shop.orders WHERE invoiceid = ?", (checking_id,)
|
# "SELECT * FROM shop.orders WHERE invoiceid = ?", (checking_id,)
|
||||||
)
|
# )
|
||||||
return {"shipped": rows["shipped"]}
|
# return {"shipped": rows["shipped"]}
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
@ -426,7 +435,7 @@ async def api_shop_market_stalls(market_id: str):
|
||||||
|
|
||||||
@shop_ext.post("/api/v1/markets")
|
@shop_ext.post("/api/v1/markets")
|
||||||
@shop_ext.put("/api/v1/markets/{market_id}")
|
@shop_ext.put("/api/v1/markets/{market_id}")
|
||||||
async def api_shop_stall_create(
|
async def api_shop_market_create(
|
||||||
data: CreateMarket,
|
data: CreateMarket,
|
||||||
market_id: str = None,
|
market_id: str = None,
|
||||||
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||||
|
|
@ -443,6 +452,7 @@ async def api_shop_stall_create(
|
||||||
else:
|
else:
|
||||||
market = await create_shop_market(data=data)
|
market = await create_shop_market(data=data)
|
||||||
|
|
||||||
|
assert market
|
||||||
await create_shop_market_stalls(market_id=market.id, data=data.stalls)
|
await create_shop_market_stalls(market_id=market.id, data=data.stalls)
|
||||||
|
|
||||||
return market.dict()
|
return market.dict()
|
||||||
|
|
@ -494,6 +504,7 @@ async def api_set_settings(
|
||||||
return {"message": "Not your Shop."}
|
return {"message": "Not your Shop."}
|
||||||
|
|
||||||
settings = await get_shop_settings(user=usr)
|
settings = await get_shop_settings(user=usr)
|
||||||
|
assert settings
|
||||||
|
|
||||||
if settings.user != wallet.wallet.user:
|
if settings.user != wallet.wallet.user:
|
||||||
return {"message": "Not your Shop."}
|
return {"message": "Not your Shop."}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue