This commit is contained in:
Tiago vasconcelos 2022-07-12 15:25:25 +01:00
parent 7eb0d13590
commit 00e6aced53
5 changed files with 106 additions and 73 deletions

View file

@ -11,9 +11,7 @@ from lnbits.helpers import template_renderer
from lnbits.settings import LNBITS_ADMIN_EXTENSIONS from lnbits.settings import LNBITS_ADMIN_EXTENSIONS
from lnbits.tasks import catch_everything_and_restart from lnbits.tasks import catch_everything_and_restart
diagonalley_ext: APIRouter = APIRouter( diagonalley_ext: APIRouter = APIRouter(prefix="/diagonalley", tags=["diagonalley"])
prefix="/diagonalley", tags=["diagonalley"]
)
db = Database("ext_diagonalley") db = Database("ext_diagonalley")
# if 'nostradmin' not in LNBITS_ADMIN_EXTENSIONS: # if 'nostradmin' not in LNBITS_ADMIN_EXTENSIONS:
# @diagonalley_ext.get("/", response_class=HTMLResponse) # @diagonalley_ext.get("/", response_class=HTMLResponse)
@ -25,6 +23,7 @@ db = Database("ext_diagonalley")
def diagonalley_renderer(): def diagonalley_renderer():
return template_renderer(["lnbits/extensions/diagonalley/templates"]) return template_renderer(["lnbits/extensions/diagonalley/templates"])
from .tasks import wait_for_paid_invoices from .tasks import wait_for_paid_invoices
from .views import * # noqa from .views import * # noqa
from .views_api import * # noqa from .views_api import * # noqa

View file

@ -24,9 +24,8 @@ from .models import (
###Products ###Products
async def create_diagonalley_product(
data: createProduct async def create_diagonalley_product(data: createProduct) -> Products:
) -> Products:
product_id = urlsafe_short_hash() product_id = urlsafe_short_hash()
await db.execute( await db.execute(
f""" f"""
@ -93,10 +92,7 @@ async def delete_diagonalley_product(product_id: str) -> None:
###zones ###zones
async def create_diagonalley_zone( async def create_diagonalley_zone(user, data: createZones) -> Zones:
user,
data: createZones
) -> Zones:
zone_id = urlsafe_short_hash() zone_id = urlsafe_short_hash()
await db.execute( await db.execute(
f""" f"""
@ -133,7 +129,9 @@ async def get_diagonalley_zone(zone_id: str) -> Optional[Zones]:
async def get_diagonalley_zones(user: str) -> List[Zones]: async def get_diagonalley_zones(user: str) -> List[Zones]:
rows = await db.fetchall('SELECT * FROM diagonalley.zones WHERE "user" = ?', (user,)) rows = await db.fetchall(
'SELECT * FROM diagonalley.zones WHERE "user" = ?', (user,)
)
return [Zones(**row) for row in rows] return [Zones(**row) for row in rows]
@ -144,9 +142,7 @@ async def delete_diagonalley_zone(zone_id: str) -> None:
###Stalls ###Stalls
async def create_diagonalley_stall( async def create_diagonalley_stall(data: createStalls) -> Stalls:
data: createStalls
) -> Stalls:
stall_id = urlsafe_short_hash() stall_id = urlsafe_short_hash()
await db.execute( await db.execute(
f""" f"""
@ -168,7 +164,8 @@ async def create_diagonalley_stall(
data.publickey, data.publickey,
data.privatekey, data.privatekey,
data.relays, data.relays,
data.shippingzones), data.shippingzones,
),
) )
stall = await get_diagonalley_stall(stall_id) stall = await get_diagonalley_stall(stall_id)
@ -210,9 +207,7 @@ async def delete_diagonalley_stall(stall_id: str) -> None:
###Orders ###Orders
async def create_diagonalley_order( async def create_diagonalley_order(data: createOrder) -> Orders:
data: createOrder
) -> Orders:
order_id = urlsafe_short_hash() order_id = urlsafe_short_hash()
await db.execute( await db.execute(

View file

@ -13,6 +13,7 @@ class Stalls(BaseModel):
relays: Optional[str] relays: Optional[str]
shippingzones: str shippingzones: str
class createStalls(BaseModel): class createStalls(BaseModel):
wallet: str = Query(...) wallet: str = Query(...)
name: str = Query(...) name: str = Query(...)
@ -21,6 +22,7 @@ class createStalls(BaseModel):
relays: str = Query(None) relays: str = Query(None)
shippingzones: str = Query(...) shippingzones: str = Query(...)
class createProduct(BaseModel): class createProduct(BaseModel):
stall: str = Query(None) stall: str = Query(None)
product: str = Query(None) product: str = Query(None)
@ -30,6 +32,7 @@ class createProduct(BaseModel):
price: int = Query(0, ge=0) price: int = Query(0, ge=0)
quantity: int = Query(0, ge=0) quantity: int = Query(0, ge=0)
class Products(BaseModel): class Products(BaseModel):
id: str id: str
stall: str stall: str
@ -40,10 +43,12 @@ class Products(BaseModel):
price: int price: int
quantity: int quantity: int
class createZones(BaseModel): class createZones(BaseModel):
cost: int = Query(0, ge=0) cost: int = Query(0, ge=0)
countries: str = Query(...) countries: str = Query(...)
class Zones(BaseModel): class Zones(BaseModel):
id: str id: str
user: str user: str
@ -61,6 +66,7 @@ class createOrder(BaseModel):
email: str = Query(...) email: str = Query(...)
invoiceid: str = Query(...) invoiceid: str = Query(...)
class Orders(BaseModel): class Orders(BaseModel):
id: str id: str
productid: str productid: str

View file

@ -1,4 +1,3 @@
from http import HTTPStatus from http import HTTPStatus
from fastapi import Request from fastapi import Request
@ -15,12 +14,14 @@ from .crud import get_diagonalley_products
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
@diagonalley_ext.get("/", response_class=HTMLResponse) @diagonalley_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_user_exists)): async def index(request: Request, user: User = Depends(check_user_exists)):
return diagonalley_renderer().TemplateResponse( return diagonalley_renderer().TemplateResponse(
"diagonalley/index.html", {"request": request, "user": user.dict()} "diagonalley/index.html", {"request": request, "user": user.dict()}
) )
@diagonalley_ext.get("/{stall_id}", response_class=HTMLResponse) @diagonalley_ext.get("/{stall_id}", response_class=HTMLResponse)
async def display(request: Request, stall_id): async def display(request: Request, stall_id):
product = await get_diagonalley_products(stall_id) product = await get_diagonalley_products(stall_id)
@ -31,5 +32,9 @@ async def display(request: Request, stall_id):
) )
return diagonalley_renderer().TemplateResponse( return diagonalley_renderer().TemplateResponse(
"diagonalley/stall.html", "diagonalley/stall.html",
{"stall": [product.dict() for product in await get_diagonalley_products(stall_id)]} {
"stall": [
product.dict() for product in await get_diagonalley_products(stall_id)
]
},
) )

View file

@ -65,36 +65,35 @@ async def api_copilot_retrieve(
return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}} return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}}
""" """
@diagonalley_ext.get("/api/v1/products") @diagonalley_ext.get("/api/v1/products")
async def api_diagonalley_products( async def api_diagonalley_products(
req: Request, req: Request,
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
all_stalls: bool = Query(False) all_stalls: bool = Query(False),
): ):
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 wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return ([product.dict() for product in await get_diagonalley_products(wallet_ids)]) return [product.dict() for product in await get_diagonalley_products(wallet_ids)]
@diagonalley_ext.post("/api/v1/products") @diagonalley_ext.post("/api/v1/products")
@diagonalley_ext.put("/api/v1/products/{product_id}") @diagonalley_ext.put("/api/v1/products/{product_id}")
async def api_diagonalley_product_create( async def api_diagonalley_product_create(
product_id, product_id, data: createProduct, wallet: WalletTypeInfo = Depends(get_key_type)
data: createProduct,
wallet: WalletTypeInfo = Depends(get_key_type)
): ):
if product_id: if product_id:
product = await get_diagonalley_product(product_id) product = await get_diagonalley_product(product_id)
if not product: if not product:
return ({"message": "Withdraw product does not exist."}) return {"message": "Withdraw product does not exist."}
if product.wallet != wallet.wallet.id: if product.wallet != wallet.wallet.id:
return ({"message": "Not your withdraw product."}) return {"message": "Not your withdraw product."}
product = await update_diagonalley_product(product_id, **data.dict()) product = await update_diagonalley_product(product_id, **data.dict())
else: else:
@ -104,14 +103,16 @@ async def api_diagonalley_product_create(
@diagonalley_ext.delete("/api/v1/products/{product_id}") @diagonalley_ext.delete("/api/v1/products/{product_id}")
async def api_diagonalley_products_delete(product_id, wallet: WalletTypeInfo = Depends(require_admin_key)): async def api_diagonalley_products_delete(
product_id, wallet: WalletTypeInfo = Depends(require_admin_key)
):
product = await get_diagonalley_product(product_id) product = await get_diagonalley_product(product_id)
if not product: if not product:
return ({"message": "Product does not exist."}) return {"message": "Product does not exist."}
if product.wallet != wallet.wallet.id: if product.wallet != wallet.wallet.id:
return ({"message": "Not your Diagon Alley."}) return {"message": "Not your Diagon Alley."}
await delete_diagonalley_product(product_id) await delete_diagonalley_product(product_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT) raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@ -125,38 +126,41 @@ async def api_diagonalley_zones(wallet: WalletTypeInfo = Depends(get_key_type)):
return await get_diagonalley_zones(wallet.wallet.user) return await get_diagonalley_zones(wallet.wallet.user)
@diagonalley_ext.post("/api/v1/zones") @diagonalley_ext.post("/api/v1/zones")
async def api_diagonalley_zone_create( async def api_diagonalley_zone_create(
data: createZones, data: createZones, wallet: WalletTypeInfo = Depends(get_key_type)
wallet: WalletTypeInfo = Depends(get_key_type)
): ):
zone = await create_diagonalley_zone(user=wallet.wallet.user, data=data) zone = await create_diagonalley_zone(user=wallet.wallet.user, data=data)
return zone.dict() return zone.dict()
@diagonalley_ext.post("/api/v1/zones/{zone_id}") @diagonalley_ext.post("/api/v1/zones/{zone_id}")
async def api_diagonalley_zone_update( async def api_diagonalley_zone_update(
data: createZones, data: createZones,
zone_id: str = Query(None), zone_id: str = Query(None),
wallet: WalletTypeInfo = Depends(require_admin_key) wallet: WalletTypeInfo = Depends(require_admin_key),
): ):
zone = await get_diagonalley_zone(zone_id) zone = await get_diagonalley_zone(zone_id)
if not zone: if not zone:
return ({"message": "Zone does not exist."}) return {"message": "Zone does not exist."}
if zone.user != wallet.wallet.user: if zone.user != wallet.wallet.user:
return ({"message": "Not your record."}) return {"message": "Not your record."}
zone = await update_diagonalley_zone(zone_id, **data.dict()) zone = await update_diagonalley_zone(zone_id, **data.dict())
return zone return zone
@diagonalley_ext.delete("/api/v1/zones/{zone_id}") @diagonalley_ext.delete("/api/v1/zones/{zone_id}")
async def api_diagonalley_zone_delete(zone_id, wallet: WalletTypeInfo = Depends(require_admin_key)): async def api_diagonalley_zone_delete(
zone_id, wallet: WalletTypeInfo = Depends(require_admin_key)
):
zone = await get_diagonalley_zone(zone_id) zone = await get_diagonalley_zone(zone_id)
if not zone: if not zone:
return ({"message": "zone does not exist."}) return {"message": "zone does not exist."}
if zone.user != wallet.wallet.user: if zone.user != wallet.wallet.user:
return ({"message": "Not your zone."}) return {"message": "Not your zone."}
await delete_diagonalley_zone(zone_id) await delete_diagonalley_zone(zone_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT) raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@ -166,27 +170,33 @@ async def api_diagonalley_zone_delete(zone_id, wallet: WalletTypeInfo = Depends(
@diagonalley_ext.get("/api/v1/stalls") @diagonalley_ext.get("/api/v1/stalls")
async def api_diagonalley_stalls(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)): async def api_diagonalley_stalls(
wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
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 wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return ([stall.dict() for stall in await get_diagonalley_stalls(wallet_ids)]) return [stall.dict() for stall in await get_diagonalley_stalls(wallet_ids)]
@diagonalley_ext.post("/api/v1/stalls") @diagonalley_ext.post("/api/v1/stalls")
@diagonalley_ext.put("/api/v1/stalls/{stall_id}") @diagonalley_ext.put("/api/v1/stalls/{stall_id}")
async def api_diagonalley_stall_create(data: createStalls, stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_stall_create(
data: createStalls,
stall_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type),
):
if stall_id: if stall_id:
stall = await get_diagonalley_stall(stall_id) stall = await get_diagonalley_stall(stall_id)
if not stall: if not stall:
return ({"message": "Withdraw stall does not exist."}) return {"message": "Withdraw stall does not exist."}
if stall.wallet != wallet.wallet.id: if stall.wallet != wallet.wallet.id:
return ({"message": "Not your withdraw stall."}) return {"message": "Not your withdraw stall."}
stall = await update_diagonalley_stall(stall_id, **data.dict()) stall = await update_diagonalley_stall(stall_id, **data.dict())
else: else:
@ -196,14 +206,16 @@ async def api_diagonalley_stall_create(data: createStalls, stall_id: str = Query
@diagonalley_ext.delete("/api/v1/stalls/{stall_id}") @diagonalley_ext.delete("/api/v1/stalls/{stall_id}")
async def api_diagonalley_stall_delete(stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key)): async def api_diagonalley_stall_delete(
stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key)
):
stall = await get_diagonalley_stall(stall_id) stall = await get_diagonalley_stall(stall_id)
if not stall: if not stall:
return ({"message": "Stall does not exist."}) return {"message": "Stall does not exist."}
if stall.wallet != wallet.wallet.id: if stall.wallet != wallet.wallet.id:
return ({"message": "Not your Stall."}) return {"message": "Not your Stall."}
await delete_diagonalley_stall(stall_id) await delete_diagonalley_stall(stall_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT) raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
@ -213,33 +225,39 @@ async def api_diagonalley_stall_delete(stall_id: str = Query(None), wallet: Wall
@diagonalley_ext.get("/api/v1/orders") @diagonalley_ext.get("/api/v1/orders")
async def api_diagonalley_orders(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)): async def api_diagonalley_orders(
wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)
):
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 wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
try: try:
return ([order.dict() for order in await get_diagonalley_orders(wallet_ids)]) return [order.dict() for order in await get_diagonalley_orders(wallet_ids)]
except: except:
return ({"message": "We could not retrieve the orders."}) return {"message": "We could not retrieve the orders."}
@diagonalley_ext.post("/api/v1/orders") @diagonalley_ext.post("/api/v1/orders")
async def api_diagonalley_order_create(data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_order_create(
data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)
):
order = await create_diagonalley_order(wallet_id=wallet.wallet.id, data=data) order = await create_diagonalley_order(wallet_id=wallet.wallet.id, data=data)
return order.dict() return order.dict()
@diagonalley_ext.delete("/api/v1/orders/{order_id}") @diagonalley_ext.delete("/api/v1/orders/{order_id}")
async def api_diagonalley_order_delete(order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_order_delete(
order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
):
order = await get_diagonalley_order(order_id) order = await get_diagonalley_order(order_id)
if not order: if not order:
return ({"message": "Order does not exist."}) return {"message": "Order does not exist."}
if order.wallet != wallet.wallet.id: if order.wallet != wallet.wallet.id:
return ({"message": "Not your Order."}) return {"message": "Not your Order."}
await delete_diagonalley_order(order_id) await delete_diagonalley_order(order_id)
@ -247,7 +265,9 @@ async def api_diagonalley_order_delete(order_id: str = Query(None), wallet: Wall
@diagonalley_ext.get("/api/v1/orders/paid/{order_id}") @diagonalley_ext.get("/api/v1/orders/paid/{order_id}")
async def api_diagonalley_order_paid(order_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key)): async def api_diagonalley_order_paid(
order_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key)
):
await db.execute( await db.execute(
"UPDATE diagonalley.orders SET paid = ? WHERE id = ?", "UPDATE diagonalley.orders SET paid = ? WHERE id = ?",
( (
@ -259,7 +279,9 @@ async def api_diagonalley_order_paid(order_id: str = Query(None), wallet: Wallet
@diagonalley_ext.get("/api/v1/orders/shipped/{order_id}") @diagonalley_ext.get("/api/v1/orders/shipped/{order_id}")
async def api_diagonalley_order_shipped(order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_order_shipped(
order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
):
await db.execute( await db.execute(
"UPDATE diagonalley.orders SET shipped = ? WHERE id = ?", "UPDATE diagonalley.orders SET shipped = ? WHERE id = ?",
( (
@ -271,47 +293,53 @@ async def api_diagonalley_order_shipped(order_id: str = Query(None), wallet: Wal
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,) "SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
) )
return ([order.dict() for order in get_diagonalley_orders(order["wallet"])]) return [order.dict() for order in get_diagonalley_orders(order["wallet"])]
###List products based on stall id ###List products based on stall id
@diagonalley_ext.get("/api/v1/stall/products/{stall_id}") @diagonalley_ext.get("/api/v1/stall/products/{stall_id}")
async def api_diagonalley_stall_products(stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_stall_products(
stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)
):
rows = await db.fetchone( rows = await db.fetchone(
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,) "SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
) )
print(rows[1]) print(rows[1])
if not rows: if not rows:
return ({"message": "Stall does not exist."}) return {"message": "Stall does not exist."}
products = db.fetchone( products = db.fetchone(
"SELECT * FROM diagonalley.products WHERE wallet = ?", (rows[1],) "SELECT * FROM diagonalley.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_diagonalley_products(rows[1])]) return [products.dict() for products in await get_diagonalley_products(rows[1])]
###Check a product has been shipped ###Check a product has been shipped
@diagonalley_ext.get("/api/v1/stall/checkshipped/{checking_id}") @diagonalley_ext.get("/api/v1/stall/checkshipped/{checking_id}")
async def api_diagonalley_stall_checkshipped(checking_id, wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_stall_checkshipped(
checking_id, wallet: WalletTypeInfo = Depends(get_key_type)
):
rows = await db.fetchone( rows = await db.fetchone(
"SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,) "SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,)
) )
return ({"shipped": rows["shipped"]}) return {"shipped": rows["shipped"]}
###Place order ###Place order
@diagonalley_ext.post("/api/v1/stall/order/{stall_id}") @diagonalley_ext.post("/api/v1/stall/order/{stall_id}")
async def api_diagonalley_stall_order(stall_id, data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)): async def api_diagonalley_stall_order(
stall_id, data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)
):
product = await get_diagonalley_product(data.productid) product = await get_diagonalley_product(data.productid)
shipping = await get_diagonalley_stall(stall_id) shipping = await get_diagonalley_stall(stall_id)
@ -345,4 +373,4 @@ async def api_diagonalley_stall_order(stall_id, data: createOrder, wallet: Walle
False, False,
), ),
) )
return ({"checking_id": checking_id, "payment_request": payment_request}) return {"checking_id": checking_id, "payment_request": payment_request}