2022-09-28 17:02:15 +01:00
|
|
|
import json
|
2022-01-28 16:22:54 +00:00
|
|
|
from http import HTTPStatus
|
2022-09-28 11:12:34 +01:00
|
|
|
from typing import List
|
2022-01-27 12:24:38 +00:00
|
|
|
|
2022-09-28 17:02:15 +01:00
|
|
|
from fastapi import BackgroundTasks, Query, Request, WebSocket, WebSocketDisconnect
|
2022-01-27 12:24:38 +00:00
|
|
|
from fastapi.params import Depends
|
|
|
|
from fastapi.templating import Jinja2Templates
|
2022-08-18 10:49:04 +01:00
|
|
|
from loguru import logger
|
2022-08-16 12:07:56 +01:00
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.responses import HTMLResponse
|
|
|
|
|
2022-01-28 16:22:54 +00:00
|
|
|
from lnbits.core.models import User
|
|
|
|
from lnbits.decorators import check_user_exists # type: ignore
|
2022-12-20 08:21:48 +00:00
|
|
|
from lnbits.extensions.shop import shop_ext, shop_renderer
|
|
|
|
from lnbits.extensions.shop.models import CreateChatMessage
|
|
|
|
from lnbits.extensions.shop.notifier import Notifier
|
2022-01-27 12:24:38 +00:00
|
|
|
|
2022-08-18 10:49:04 +01:00
|
|
|
from .crud import (
|
2022-09-30 10:16:34 +01:00
|
|
|
create_chat_message,
|
2022-12-20 08:21:48 +00:00
|
|
|
get_shop_market,
|
|
|
|
get_shop_market_stalls,
|
|
|
|
get_shop_order_details,
|
|
|
|
get_shop_order_invoiceid,
|
|
|
|
get_shop_products,
|
|
|
|
get_shop_stall,
|
|
|
|
get_shop_zone,
|
|
|
|
get_shop_zones,
|
|
|
|
update_shop_product_stock,
|
2022-08-18 10:49:04 +01:00
|
|
|
)
|
2022-01-27 12:24:38 +00:00
|
|
|
|
2022-01-28 16:22:54 +00:00
|
|
|
templates = Jinja2Templates(directory="templates")
|
2022-01-27 12:24:38 +00:00
|
|
|
|
2022-07-12 15:25:25 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
@shop_ext.get("/", response_class=HTMLResponse)
|
2022-01-28 16:22:54 +00:00
|
|
|
async def index(request: Request, user: User = Depends(check_user_exists)):
|
2022-12-20 08:21:48 +00:00
|
|
|
return shop_renderer().TemplateResponse(
|
|
|
|
"shop/index.html", {"request": request, "user": user.dict()}
|
2022-01-28 16:22:54 +00:00
|
|
|
)
|
2022-01-27 12:24:38 +00:00
|
|
|
|
2022-09-28 11:12:34 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
@shop_ext.get("/stalls/{stall_id}", response_class=HTMLResponse)
|
2022-12-22 12:23:22 +00:00
|
|
|
async def stall(request: Request, stall_id):
|
2022-12-20 08:21:48 +00:00
|
|
|
stall = await get_shop_stall(stall_id)
|
|
|
|
products = await get_shop_products(stall_id)
|
2022-08-18 10:49:04 +01:00
|
|
|
zones = []
|
|
|
|
for id in stall.shippingzones.split(","):
|
2022-12-20 08:21:48 +00:00
|
|
|
z = await get_shop_zone(id)
|
2022-08-18 10:49:04 +01:00
|
|
|
z = z.dict()
|
|
|
|
zones.append({"label": z["countries"], "cost": z["cost"], "value": z["id"]})
|
2022-08-18 16:20:15 +01:00
|
|
|
|
2022-07-19 10:13:06 +01:00
|
|
|
if not stall:
|
2022-01-28 16:22:54 +00:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Stall does not exist."
|
|
|
|
)
|
2022-09-12 17:01:41 +01:00
|
|
|
|
2022-08-16 12:07:56 +01:00
|
|
|
stall = stall.dict()
|
2022-12-21 17:44:37 +00:00
|
|
|
|
2022-08-18 10:49:04 +01:00
|
|
|
stall["zones"] = zones
|
2022-08-16 12:19:31 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
return shop_renderer().TemplateResponse(
|
|
|
|
"shop/stall.html",
|
2022-07-27 15:28:58 +01:00
|
|
|
{
|
|
|
|
"request": request,
|
2022-08-16 12:07:56 +01:00
|
|
|
"stall": stall,
|
2022-08-16 12:19:31 +01:00
|
|
|
"products": [product.dict() for product in products],
|
2022-07-27 15:28:58 +01:00
|
|
|
},
|
|
|
|
)
|
2022-08-16 12:07:56 +01:00
|
|
|
|
2022-08-16 12:19:31 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
@shop_ext.get("/market/{market_id}", response_class=HTMLResponse)
|
2022-12-22 12:23:22 +00:00
|
|
|
async def market(request: Request, market_id):
|
2022-12-20 08:21:48 +00:00
|
|
|
market = await get_shop_market(market_id)
|
2022-09-15 15:52:08 +01:00
|
|
|
|
2022-09-15 15:51:12 +01:00
|
|
|
if not market:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.NOT_FOUND, detail="Marketplace doesn't exist."
|
|
|
|
)
|
2022-09-15 15:52:08 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
stalls = await get_shop_market_stalls(market_id)
|
2022-09-15 15:51:12 +01:00
|
|
|
stalls_ids = [stall.id for stall in stalls]
|
2022-12-20 16:11:56 +00:00
|
|
|
products = [product.dict() for product in await get_shop_products(stalls_ids)]
|
2022-08-16 12:07:56 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
return shop_renderer().TemplateResponse(
|
|
|
|
"shop/market.html",
|
2022-09-15 15:51:12 +01:00
|
|
|
{
|
|
|
|
"request": request,
|
|
|
|
"market": market,
|
|
|
|
"stalls": [stall.dict() for stall in stalls],
|
2022-09-15 15:52:08 +01:00
|
|
|
"products": products,
|
2022-09-15 15:51:12 +01:00
|
|
|
},
|
|
|
|
)
|
2022-09-28 11:12:34 +01:00
|
|
|
|
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
@shop_ext.get("/order", response_class=HTMLResponse)
|
2022-12-22 12:23:22 +00:00
|
|
|
async def order_chat(
|
2022-11-25 14:24:17 +00:00
|
|
|
request: Request,
|
|
|
|
merch: str = Query(...),
|
|
|
|
invoice_id: str = Query(...),
|
|
|
|
keys: str = Query(None),
|
2022-10-19 22:08:48 +01:00
|
|
|
):
|
2022-12-20 08:21:48 +00:00
|
|
|
stall = await get_shop_stall(merch)
|
|
|
|
order = await get_shop_order_invoiceid(invoice_id)
|
|
|
|
_order = await get_shop_order_details(order.id)
|
|
|
|
products = await get_shop_products(stall.id)
|
2022-09-28 11:12:34 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
return shop_renderer().TemplateResponse(
|
|
|
|
"shop/order.html",
|
2022-09-28 17:02:15 +01:00
|
|
|
{
|
|
|
|
"request": request,
|
|
|
|
"stall": {
|
|
|
|
"id": stall.id,
|
|
|
|
"name": stall.name,
|
|
|
|
"publickey": stall.publickey,
|
|
|
|
"wallet": stall.wallet,
|
|
|
|
},
|
2022-11-25 14:24:17 +00:00
|
|
|
"publickey": keys.split(",")[0] if keys else None,
|
|
|
|
"privatekey": keys.split(",")[1] if keys else None,
|
2022-10-19 22:08:48 +01:00
|
|
|
"order_id": order.invoiceid,
|
2022-09-28 17:02:15 +01:00
|
|
|
"order": [details.dict() for details in _order],
|
2022-10-19 22:08:48 +01:00
|
|
|
"products": [product.dict() for product in products],
|
2022-09-28 17:02:15 +01:00
|
|
|
},
|
|
|
|
)
|
2022-09-28 11:12:34 +01:00
|
|
|
|
|
|
|
|
2022-09-28 17:02:15 +01:00
|
|
|
##################WEBSOCKET ROUTES########################
|
2022-09-28 11:12:34 +01:00
|
|
|
|
2022-09-28 17:02:15 +01:00
|
|
|
# Initialize Notifier:
|
|
|
|
notifier = Notifier()
|
|
|
|
|
2022-09-28 17:03:09 +01:00
|
|
|
|
2022-12-20 08:21:48 +00:00
|
|
|
@shop_ext.websocket("/ws/{room_name}")
|
2022-09-28 17:02:15 +01:00
|
|
|
async def websocket_endpoint(
|
|
|
|
websocket: WebSocket, room_name: str, background_tasks: BackgroundTasks
|
|
|
|
):
|
|
|
|
await notifier.connect(websocket, room_name)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
data = await websocket.receive_text()
|
|
|
|
d = json.loads(data)
|
|
|
|
d["room_name"] = room_name
|
|
|
|
|
|
|
|
room_members = (
|
|
|
|
notifier.get_members(room_name)
|
|
|
|
if notifier.get_members(room_name) is not None
|
|
|
|
else []
|
|
|
|
)
|
|
|
|
|
|
|
|
if websocket not in room_members:
|
|
|
|
print("Sender not in room member: Reconnecting...")
|
|
|
|
await notifier.connect(websocket, room_name)
|
2022-09-30 10:16:34 +01:00
|
|
|
await notifier._notify(data, room_name)
|
2022-09-28 17:02:15 +01:00
|
|
|
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
notifier.remove(websocket, room_name)
|