commit
e26e342dd7
6 changed files with 261 additions and 242 deletions
|
|
@ -1,16 +1,37 @@
|
|||
from quart import Blueprint
|
||||
import asyncio
|
||||
|
||||
from fastapi import APIRouter
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from lnbits.db import Database
|
||||
from lnbits.helpers import template_renderer
|
||||
from lnbits.tasks import catch_everything_and_restart
|
||||
|
||||
db = Database("ext_diagonalley")
|
||||
|
||||
diagonalley_ext: Blueprint = Blueprint(
|
||||
"diagonalley", __name__, static_folder="static", template_folder="templates"
|
||||
diagonalley_static_files = [
|
||||
{
|
||||
"path": "/diagonalley/static",
|
||||
"app": StaticFiles(directory="lnbits/extensions/diagonalley/static"),
|
||||
"name": "diagonalley_static",
|
||||
}
|
||||
]
|
||||
|
||||
diagonalley_ext: APIRouter = APIRouter(
|
||||
prefix="/diagonalley", tags=["diagonalley"]
|
||||
# "diagonalley", __name__, static_folder="static", template_folder="templates"
|
||||
)
|
||||
|
||||
from .views_api import * # noqa
|
||||
def diagonalley_renderer():
|
||||
return template_renderer(["lnbits/extensions/diagonalley/templates"])
|
||||
|
||||
|
||||
from .tasks import wait_for_paid_invoices
|
||||
from .views import * # noqa
|
||||
from .views_api import * # noqa
|
||||
|
||||
from .tasks import register_listeners
|
||||
from lnbits.tasks import record_async
|
||||
|
||||
diagonalley_ext.record(record_async(register_listeners))
|
||||
def diagonalley_start():
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,26 @@
|
|||
import re
|
||||
from base64 import urlsafe_b64encode
|
||||
from uuid import uuid4
|
||||
from typing import List, Optional, Union
|
||||
from uuid import uuid4
|
||||
|
||||
from lnbits.settings import WALLET
|
||||
import httpx
|
||||
|
||||
# from lnbits.db import open_ext_db
|
||||
from lnbits.db import SQLITE
|
||||
from . import db
|
||||
from .models import Products, Orders, Stalls, Zones
|
||||
|
||||
import httpx
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
import re
|
||||
from lnbits.settings import WALLET
|
||||
|
||||
from . import db
|
||||
from .models import (
|
||||
Orders,
|
||||
Products,
|
||||
Stalls,
|
||||
Zones,
|
||||
createOrder,
|
||||
createProduct,
|
||||
createStalls,
|
||||
createZones,
|
||||
)
|
||||
|
||||
regex = re.compile(
|
||||
r"^(?:http|ftp)s?://" # http:// or https://
|
||||
|
|
@ -28,35 +37,27 @@ regex = re.compile(
|
|||
|
||||
|
||||
async def create_diagonalley_product(
|
||||
*,
|
||||
stall_id: str,
|
||||
product: str,
|
||||
categories: str,
|
||||
description: str,
|
||||
image: Optional[str] = None,
|
||||
price: int,
|
||||
quantity: int,
|
||||
shippingzones: str,
|
||||
data: createProduct
|
||||
) -> Products:
|
||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
method = db.execute if db.type == SQLITE else db.fetchone
|
||||
# returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
# method = db.execute if db.type == SQLITE else db.fetchone
|
||||
product_id = urlsafe_short_hash()
|
||||
# with open_ext_db("diagonalley") as db:
|
||||
result = await (method)(
|
||||
# result = await (method)(
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO diagonalley.products (id, stall, product, categories, description, image, price, quantity, shippingzones)
|
||||
INSERT INTO diagonalley.products (id, stall, product, categories, description, image, price, quantity)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
{returning}
|
||||
""",
|
||||
(
|
||||
product_id,
|
||||
stall_id,
|
||||
product,
|
||||
categories,
|
||||
description,
|
||||
image,
|
||||
price,
|
||||
quantity,
|
||||
data.stall,
|
||||
data.product,
|
||||
data.categories,
|
||||
data.description,
|
||||
data.image,
|
||||
data.price,
|
||||
data.quantity,
|
||||
),
|
||||
)
|
||||
product = await get_diagonalley_product(product_id)
|
||||
|
|
@ -83,7 +84,7 @@ async def get_diagonalley_product(product_id: str) -> Optional[Products]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
|
||||
)
|
||||
return Products.from_row(row) if row else None
|
||||
return Products(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Products]:
|
||||
|
|
@ -98,7 +99,7 @@ async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Pr
|
|||
""",
|
||||
(*wallet_ids,),
|
||||
)
|
||||
return [Products.from_row(row) for row in rows]
|
||||
return [Products(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_product(product_id: str) -> None:
|
||||
|
|
@ -109,17 +110,11 @@ async def delete_diagonalley_product(product_id: str) -> None:
|
|||
|
||||
|
||||
async def create_diagonalley_zone(
|
||||
*,
|
||||
wallet: Optional[str] = None,
|
||||
cost: Optional[int] = 0,
|
||||
countries: Optional[str] = None,
|
||||
wallet,
|
||||
data: createZones
|
||||
) -> Zones:
|
||||
|
||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
method = db.execute if db.type == SQLITE else db.fetchone
|
||||
|
||||
zone_id = urlsafe_short_hash()
|
||||
result = await (method)(
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO diagonalley.zones (
|
||||
id,
|
||||
|
|
@ -129,9 +124,8 @@ async def create_diagonalley_zone(
|
|||
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
{returning}
|
||||
""",
|
||||
(zone_id, wallet, cost, countries),
|
||||
(zone_id, wallet, data.cost, data.countries),
|
||||
)
|
||||
|
||||
zone = await get_diagonalley_zone(zone_id)
|
||||
|
|
@ -146,12 +140,12 @@ async def update_diagonalley_zone(zone_id: str, **kwargs) -> Optional[Zones]:
|
|||
(*kwargs.values(), zone_id),
|
||||
)
|
||||
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
|
||||
return Zones.from_row(row) if row else None
|
||||
return Zones(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_zone(zone_id: str) -> Optional[Zones]:
|
||||
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
|
||||
return Zones.from_row(row) if row else None
|
||||
return Zones(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_zones(wallet_ids: Union[str, List[str]]) -> List[Zones]:
|
||||
|
|
@ -189,7 +183,7 @@ async def get_diagonalley_zones(wallet_ids: Union[str, List[str]]) -> List[Zones
|
|||
rows = await db.fetchall(
|
||||
f"SELECT * FROM diagonalley.zones WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
return [Zones.from_row(row) for row in rows]
|
||||
return [Zones(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_zone(zone_id: str) -> None:
|
||||
|
|
@ -200,20 +194,10 @@ async def delete_diagonalley_zone(zone_id: str) -> None:
|
|||
|
||||
|
||||
async def create_diagonalley_stall(
|
||||
*,
|
||||
wallet: str,
|
||||
name: str,
|
||||
publickey: str,
|
||||
privatekey: str,
|
||||
relays: str,
|
||||
shippingzones: str,
|
||||
data: createStalls
|
||||
) -> Stalls:
|
||||
|
||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
method = db.execute if db.type == SQLITE else db.fetchone
|
||||
|
||||
stall_id = urlsafe_short_hash()
|
||||
result = await (method)(
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO diagonalley.stalls (
|
||||
id,
|
||||
|
|
@ -225,9 +209,15 @@ async def create_diagonalley_stall(
|
|||
shippingzones
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
{returning}
|
||||
""",
|
||||
(stall_id, wallet, name, publickey, privatekey, relays, shippingzones),
|
||||
(
|
||||
stall_id,
|
||||
data.wallet,
|
||||
data.name,
|
||||
data.publickey,
|
||||
data.privatekey,
|
||||
data.relays,
|
||||
data.shippingzones),
|
||||
)
|
||||
|
||||
stall = await get_diagonalley_stall(stall_id)
|
||||
|
|
@ -244,7 +234,7 @@ async def update_diagonalley_stall(stall_id: str, **kwargs) -> Optional[Stalls]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
|
||||
)
|
||||
return Stalls.from_row(row) if row else None
|
||||
return Stalls(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]:
|
||||
|
|
@ -277,7 +267,7 @@ async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
|
||||
)
|
||||
return Stalls.from_row(row) if row else None
|
||||
return Stalls(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_stalls(wallet_ids: Union[str, List[str]]) -> List[Stalls]:
|
||||
|
|
@ -314,7 +304,7 @@ async def get_diagonalley_stalls(wallet_ids: Union[str, List[str]]) -> List[Stal
|
|||
rows = await db.fetchall(
|
||||
f"SELECT * FROM diagonalley.stalls WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
return [Stalls.from_row(row) for row in rows]
|
||||
return [Stalls(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_stall(stall_id: str) -> None:
|
||||
|
|
@ -325,47 +315,34 @@ async def delete_diagonalley_stall(stall_id: str) -> None:
|
|||
|
||||
|
||||
async def create_diagonalley_order(
|
||||
*,
|
||||
productid: str,
|
||||
wallet: str,
|
||||
product: str,
|
||||
quantity: int,
|
||||
shippingzone: str,
|
||||
address: str,
|
||||
email: str,
|
||||
invoiceid: str,
|
||||
paid: bool,
|
||||
shipped: bool,
|
||||
data: createOrder
|
||||
) -> Orders:
|
||||
returning = "" if db.type == SQLITE else "RETURNING ID"
|
||||
method = db.execute if db.type == SQLITE else db.fetchone
|
||||
|
||||
order_id = urlsafe_short_hash()
|
||||
result = await (method)(
|
||||
await db.execute(
|
||||
f"""
|
||||
INSERT INTO diagonalley.orders (id, productid, wallet, product,
|
||||
quantity, shippingzone, address, email, invoiceid, paid, shipped)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
{returning}
|
||||
""",
|
||||
(
|
||||
order_id,
|
||||
productid,
|
||||
wallet,
|
||||
product,
|
||||
quantity,
|
||||
shippingzone,
|
||||
address,
|
||||
email,
|
||||
invoiceid,
|
||||
data.productid,
|
||||
data.wallet,
|
||||
data.product,
|
||||
data.quantity,
|
||||
data.shippingzone,
|
||||
data.address,
|
||||
data.email,
|
||||
data.invoiceid,
|
||||
False,
|
||||
False,
|
||||
),
|
||||
)
|
||||
if db.type == SQLITE:
|
||||
order_id = result._result_proxy.lastrowid
|
||||
else:
|
||||
order_id = result[0]
|
||||
# if db.type == SQLITE:
|
||||
# order_id = result._result_proxy.lastrowid
|
||||
# else:
|
||||
# order_id = result[0]
|
||||
|
||||
link = await get_diagonalley_order(order_id)
|
||||
assert link, "Newly created link couldn't be retrieved"
|
||||
|
|
@ -376,7 +353,7 @@ async def get_diagonalley_order(order_id: str) -> Optional[Orders]:
|
|||
row = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
|
||||
)
|
||||
return Orders.from_row(row) if row else None
|
||||
return Orders(**row) if row else None
|
||||
|
||||
|
||||
async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
|
||||
|
|
@ -388,7 +365,7 @@ async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orde
|
|||
f"SELECT * FROM diagonalley.orders WHERE wallet IN ({q})", (*wallet_ids,)
|
||||
)
|
||||
#
|
||||
return [Orders.from_row(row) for row in rows]
|
||||
return [Orders(**row) for row in rows]
|
||||
|
||||
|
||||
async def delete_diagonalley_order(order_id: str) -> None:
|
||||
|
|
|
|||
|
|
@ -1,57 +1,83 @@
|
|||
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult
|
||||
from starlette.requests import Request
|
||||
import json
|
||||
from lib2to3.pytree import Base
|
||||
from sqlite3 import Row
|
||||
from typing import Dict, Optional
|
||||
from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse
|
||||
|
||||
from fastapi.param_functions import Query
|
||||
from typing import Optional, Dict
|
||||
from lnbits.lnurl import encode as lnurl_encode # type: ignore
|
||||
from lnurl.types import LnurlPayMetadata # type: ignore
|
||||
from pydantic import BaseModel
|
||||
import json
|
||||
from sqlite3 import Row
|
||||
from starlette.requests import Request
|
||||
|
||||
from lnbits.lnurl import encode as lnurl_encode # type: ignore
|
||||
|
||||
|
||||
class Stalls(BaseModel):
|
||||
id: str = Query(None)
|
||||
wallet: str = Query(None)
|
||||
name: str = Query(None)
|
||||
publickey: str = Query(None)
|
||||
privatekey: str = Query(None)
|
||||
relays: str = Query(None)
|
||||
id: str
|
||||
wallet: str
|
||||
name: str
|
||||
publickey: str
|
||||
privatekey: str
|
||||
relays: str
|
||||
shippingzones: str
|
||||
|
||||
class createStalls(BaseModel):
|
||||
wallet: str = Query(None)
|
||||
name: str = Query(None)
|
||||
publickey: str = Query(None)
|
||||
privatekey: str = Query(None)
|
||||
relays: str = Query(None)
|
||||
shippingzones: str = Query(None)
|
||||
wallet: str = Query(...)
|
||||
name: str = Query(...)
|
||||
publickey: str = Query(...)
|
||||
privatekey: str = Query(...)
|
||||
relays: str = Query(...)
|
||||
shippingzones: str = Query(...)
|
||||
|
||||
class Products(BaseModel):
|
||||
id: str = Query(None)
|
||||
class createProduct(BaseModel):
|
||||
stall: str = Query(None)
|
||||
product: str = Query(None)
|
||||
categories: str = Query(None)
|
||||
description: str = Query(None)
|
||||
image: str = Query(None)
|
||||
price: int = Query(0)
|
||||
quantity: int = Query(0)
|
||||
price: int = Query(0, ge=0)
|
||||
quantity: int = Query(0, ge=0)
|
||||
|
||||
class Products(BaseModel):
|
||||
id: str
|
||||
stall: str
|
||||
product: str
|
||||
categories: str
|
||||
description: str
|
||||
image: str
|
||||
price: int
|
||||
quantity: int
|
||||
|
||||
class Zones(BaseModel):
|
||||
id: str = Query(None)
|
||||
wallet: str = Query(None)
|
||||
class createZones(BaseModel):
|
||||
cost: str = Query(None)
|
||||
countries: str = Query(None)
|
||||
|
||||
class Zones(BaseModel):
|
||||
id: str
|
||||
wallet: str
|
||||
cost: str
|
||||
countries: str
|
||||
|
||||
|
||||
class createOrder(BaseModel):
|
||||
productid: str = Query(...)
|
||||
stall: str = Query(...)
|
||||
product: str = Query(...)
|
||||
quantity: int = Query(..., ge=1)
|
||||
shippingzone: int = Query(...)
|
||||
address: str = Query(...)
|
||||
email: str = Query(...)
|
||||
invoiceid: str = Query(...)
|
||||
|
||||
class Orders(BaseModel):
|
||||
id: str = Query(None)
|
||||
productid: str = Query(None)
|
||||
stall: str = Query(None)
|
||||
product: str = Query(None)
|
||||
quantity: int = Query(0)
|
||||
shippingzone: int = Query(0)
|
||||
address: str = Query(None)
|
||||
email: str = Query(None)
|
||||
invoiceid: str = Query(None)
|
||||
id: str
|
||||
productid: str
|
||||
stall: str
|
||||
product: str
|
||||
quantity: int
|
||||
shippingzone: int
|
||||
address: str
|
||||
email: str
|
||||
invoiceid: str
|
||||
paid: bool
|
||||
shipped: bool
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@ import asyncio
|
|||
from lnbits.core.models import Payment
|
||||
from lnbits.tasks import register_invoice_listener
|
||||
|
||||
from .crud import get_ticket, set_ticket_paid
|
||||
|
||||
|
||||
async def wait_for_paid_invoices():
|
||||
invoice_queue = asyncio.Queue()
|
||||
|
|
@ -16,6 +14,7 @@ async def wait_for_paid_invoices():
|
|||
|
||||
|
||||
async def on_invoice_paid(payment: Payment) -> None:
|
||||
"""
|
||||
if "lnticket" != payment.extra.get("tag"):
|
||||
# not a lnticket invoice
|
||||
return
|
||||
|
|
@ -26,4 +25,5 @@ async def on_invoice_paid(payment: Payment) -> None:
|
|||
return
|
||||
|
||||
await payment.set_pending(False)
|
||||
await set_ticket_paid(payment.payment_hash)
|
||||
await set_ticket_paid(payment.payment_hash)
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -1,44 +1,35 @@
|
|||
|
||||
from typing import List
|
||||
|
||||
from fastapi import Request, WebSocket, WebSocketDisconnect
|
||||
from fastapi.params import Depends
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from starlette.responses import HTMLResponse # type: ignore
|
||||
|
||||
from http import HTTPStatus
|
||||
import json
|
||||
from lnbits.decorators import check_user_exists, validate_uuids
|
||||
from lnbits.extensions.diagonalley import diagonalley_ext
|
||||
|
||||
from .crud import (
|
||||
create_diagonalley_product,
|
||||
get_diagonalley_product,
|
||||
get_diagonalley_products,
|
||||
delete_diagonalley_product,
|
||||
create_diagonalley_order,
|
||||
get_diagonalley_order,
|
||||
get_diagonalley_orders,
|
||||
update_diagonalley_product,
|
||||
)
|
||||
from fastapi import Request
|
||||
from fastapi.params import Depends
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import HTMLResponse
|
||||
|
||||
from lnbits.core.models import User
|
||||
from lnbits.decorators import check_user_exists # type: ignore
|
||||
from lnbits.extensions.diagonalley import diagonalley_ext, diagonalley_renderer
|
||||
|
||||
from .crud import get_diagonalley_products
|
||||
|
||||
templates = Jinja2Templates(directory="templates")
|
||||
|
||||
@diagonalley_ext.get("/", response_class=HTMLResponse)
|
||||
@validate_uuids(["usr"], required=True)
|
||||
@check_user_exists(request: Request)
|
||||
async def index():
|
||||
return await render_template("diagonalley/index.html", user=g.user)
|
||||
async def index(request: Request, user: User = Depends(check_user_exists)):
|
||||
return diagonalley_renderer().TemplateResponse(
|
||||
"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):
|
||||
product = await get_diagonalley_products(stall_id)
|
||||
|
||||
if not product:
|
||||
abort(HTTPStatus.NOT_FOUND, "Stall does not exist.")
|
||||
|
||||
return await render_template(
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND, detail="Stall does not exist."
|
||||
)
|
||||
return diagonalley_renderer().TemplateResponse(
|
||||
"diagonalley/stall.html",
|
||||
stall=json.dumps(
|
||||
[product._asdict() for product in await get_diagonalley_products(stall_id)]
|
||||
),
|
||||
{"stall": [product.dict() for product in await get_diagonalley_products(stall_id)]}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
from base64 import urlsafe_b64encode
|
||||
from http import HTTPStatus
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.param_functions import Query
|
||||
|
|
@ -6,41 +8,47 @@ from fastapi.params import Depends
|
|||
from starlette.exceptions import HTTPException
|
||||
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
from lnbits.core.services import create_invoice
|
||||
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
|
||||
|
||||
from . import diagonalley_ext
|
||||
from . import db, diagonalley_ext
|
||||
from .crud import (
|
||||
create_diagonalley_product,
|
||||
get_diagonalley_product,
|
||||
get_diagonalley_products,
|
||||
delete_diagonalley_product,
|
||||
create_diagonalley_zone,
|
||||
update_diagonalley_zone,
|
||||
get_diagonalley_zone,
|
||||
get_diagonalley_zones,
|
||||
delete_diagonalley_zone,
|
||||
create_diagonalley_stall,
|
||||
update_diagonalley_stall,
|
||||
get_diagonalley_stall,
|
||||
get_diagonalley_stalls,
|
||||
delete_diagonalley_stall,
|
||||
create_diagonalley_order,
|
||||
create_diagonalley_product,
|
||||
create_diagonalley_stall,
|
||||
create_diagonalley_zone,
|
||||
delete_diagonalley_order,
|
||||
delete_diagonalley_product,
|
||||
delete_diagonalley_stall,
|
||||
delete_diagonalley_zone,
|
||||
get_diagonalley_order,
|
||||
get_diagonalley_orders,
|
||||
get_diagonalley_product,
|
||||
get_diagonalley_products,
|
||||
get_diagonalley_stall,
|
||||
get_diagonalley_stalls,
|
||||
get_diagonalley_zone,
|
||||
get_diagonalley_zones,
|
||||
update_diagonalley_product,
|
||||
delete_diagonalley_order,
|
||||
update_diagonalley_stall,
|
||||
update_diagonalley_zone,
|
||||
)
|
||||
from .models import (
|
||||
Orders,
|
||||
Products,
|
||||
Stalls,
|
||||
Zones,
|
||||
createOrder,
|
||||
createProduct,
|
||||
createStalls,
|
||||
createZones,
|
||||
)
|
||||
from lnbits.core.services import create_invoice
|
||||
from base64 import urlsafe_b64encode
|
||||
from uuid import uuid4
|
||||
|
||||
# from lnbits.db import open_ext_db
|
||||
|
||||
from . import db
|
||||
from .models import Products, Orders, Stalls
|
||||
|
||||
### Products
|
||||
|
||||
"""
|
||||
@copilot_ext.get("/api/v1/copilot/{copilot_id}")
|
||||
async def api_copilot_retrieve(
|
||||
req: Request,
|
||||
|
|
@ -55,26 +63,27 @@ async def api_copilot_retrieve(
|
|||
if not copilot.lnurl_toggle:
|
||||
return copilot.dict()
|
||||
return {**copilot.dict(), **{"lnurl": copilot.lnurl(req)}}
|
||||
|
||||
"""
|
||||
|
||||
@diagonalley_ext.get("/api/v1/products")
|
||||
async def api_diagonalley_products(
|
||||
req: Request,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
all_stalls: bool = Query(False)
|
||||
):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_stalls" in request.args:
|
||||
if all_stalls:
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
return ([product._asdict() 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.put("/api/v1/products/{product_id}")
|
||||
async def api_diagonalley_product_create(
|
||||
data: Products,
|
||||
product_id=: str = Query(None),
|
||||
product_id,
|
||||
data: createProduct,
|
||||
wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
|
||||
|
|
@ -82,19 +91,19 @@ async def api_diagonalley_product_create(
|
|||
product = await get_diagonalley_product(product_id)
|
||||
|
||||
if not product:
|
||||
return ({"message": "Withdraw product does not exist."}))
|
||||
return ({"message": "Withdraw product does not exist."})
|
||||
|
||||
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)
|
||||
product = await update_diagonalley_product(product_id, **data.dict())
|
||||
else:
|
||||
product = await create_diagonalley_product(wallet_id=wallet.wallet.id, data)
|
||||
product = await create_diagonalley_product(data=data)
|
||||
|
||||
return ({**product._asdict()}))
|
||||
return product.dict()
|
||||
|
||||
|
||||
@diagonalley_ext.route("/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)):
|
||||
product = await get_diagonalley_product(product_id)
|
||||
|
||||
|
|
@ -105,7 +114,6 @@ async def api_diagonalley_products_delete(product_id, wallet: WalletTypeInfo = D
|
|||
return ({"message": "Not your Diagon Alley."})
|
||||
|
||||
await delete_diagonalley_product(product_id)
|
||||
|
||||
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
|
|
@ -113,19 +121,18 @@ async def api_diagonalley_products_delete(product_id, wallet: WalletTypeInfo = D
|
|||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/zones")
|
||||
async def api_diagonalley_zones(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_zones(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
if all_wallets:
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
return ([zone._asdict() for zone in await get_diagonalley_zones(wallet_ids)]))
|
||||
|
||||
return ([zone.dict() for zone in await get_diagonalley_zones(wallet_ids)])
|
||||
|
||||
@diagonalley_ext.post("/api/v1/zones")
|
||||
@diagonalley_ext.put("/api/v1/zones/{zone_id}")
|
||||
async def api_diagonalley_zone_create(
|
||||
data: Zones,
|
||||
data: createZones,
|
||||
zone_id: str = Query(None),
|
||||
wallet: WalletTypeInfo = Depends(get_key_type)
|
||||
):
|
||||
|
|
@ -133,20 +140,20 @@ async def api_diagonalley_zone_create(
|
|||
zone = await get_diagonalley_zone(zone_id)
|
||||
|
||||
if not zone:
|
||||
return ({"message": "Zone does not exist."}))
|
||||
return ({"message": "Zone does not exist."})
|
||||
|
||||
if zone.wallet != walley.wallet.id:
|
||||
return ({"message": "Not your record."}))
|
||||
if zone.wallet != wallet.wallet.id:
|
||||
return ({"message": "Not your record."})
|
||||
|
||||
zone = await update_diagonalley_zone(zone_id, data)
|
||||
zone = await update_diagonalley_zone(zone_id, **data.dict())
|
||||
else:
|
||||
zone = await create_diagonalley_zone(wallet=wallet.wallet.id, data)
|
||||
zone = await create_diagonalley_zone(wallet=wallet.wallet.id, data=data)
|
||||
|
||||
return ({**zone._asdict()}))
|
||||
return zone.dict()
|
||||
|
||||
|
||||
@diagonalley_ext.delete("/api/v1/zones/{zone_id}")
|
||||
async def api_diagonalley_zone_delete(zone_id: str = Query(None), 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)
|
||||
|
||||
if not zone:
|
||||
|
|
@ -156,7 +163,6 @@ async def api_diagonalley_zone_delete(zone_id: str = Query(None), wallet: Walle
|
|||
return ({"message": "Not your zone."})
|
||||
|
||||
await delete_diagonalley_zone(zone_id)
|
||||
|
||||
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
|
|
@ -164,13 +170,13 @@ async def api_diagonalley_zone_delete(zone_id: str = Query(None), wallet: Walle
|
|||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/stalls")
|
||||
async def api_diagonalley_stalls(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_stalls(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
if all_wallets:
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
return ([stall._asdict() 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")
|
||||
|
|
@ -181,16 +187,16 @@ async def api_diagonalley_stall_create(data: createStalls, stall_id: str = Query
|
|||
stall = await get_diagonalley_stall(stall_id)
|
||||
|
||||
if not stall:
|
||||
return ({"message": "Withdraw stall does not exist."}))
|
||||
return ({"message": "Withdraw stall does not exist."})
|
||||
|
||||
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)
|
||||
stall = await update_diagonalley_stall(stall_id, **data.dict())
|
||||
else:
|
||||
stall = await create_diagonalley_stall(wallet_id=wallet.wallet.id, data)
|
||||
stall = await create_diagonalley_stall(data=data)
|
||||
|
||||
return ({**stall._asdict()}))
|
||||
return stall.dict()
|
||||
|
||||
|
||||
@diagonalley_ext.delete("/api/v1/stalls/{stall_id}")
|
||||
|
|
@ -204,7 +210,6 @@ async def api_diagonalley_stall_delete(stall_id: str = Query(None), wallet: Wall
|
|||
return ({"message": "Not your Stall."})
|
||||
|
||||
await delete_diagonalley_stall(stall_id)
|
||||
|
||||
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
||||
|
||||
|
||||
|
|
@ -212,23 +217,22 @@ async def api_diagonalley_stall_delete(stall_id: str = Query(None), wallet: Wall
|
|||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/orders")
|
||||
async def api_diagonalley_orders(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_orders(wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
||||
wallet_ids = [wallet.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
if all_wallets:
|
||||
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
||||
|
||||
try:
|
||||
return ([order._asdict() for order in await get_diagonalley_orders(wallet_ids)])
|
||||
return ([order.dict() for order in await get_diagonalley_orders(wallet_ids)])
|
||||
except:
|
||||
return ({"message": "We could not retrieve the orders."}))
|
||||
return ({"message": "We could not retrieve the orders."})
|
||||
|
||||
|
||||
@diagonalley_ext.post("/api/v1/orders")
|
||||
|
||||
async def api_diagonalley_order_create(data: createOrders, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
order = await create_diagonalley_order(wallet_id=wallet.wallet.id, data)
|
||||
return ({**order._asdict()})
|
||||
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)
|
||||
return order.dict()
|
||||
|
||||
|
||||
@diagonalley_ext.delete("/api/v1/orders/{order_id}")
|
||||
|
|
@ -271,7 +275,7 @@ async def api_diagonalley_order_shipped(order_id: str = Query(None), wallet: Wal
|
|||
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
|
||||
)
|
||||
|
||||
return ([order._asdict() 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
|
||||
|
|
@ -293,14 +297,14 @@ async def api_diagonalley_stall_products(stall_id: str = Query(None), wallet: Wa
|
|||
if not products:
|
||||
return ({"message": "No products"})
|
||||
|
||||
return ([products._asdict() 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
|
||||
|
||||
|
||||
@diagonalley_ext.get("/api/v1/stall/checkshipped/{checking_id}")
|
||||
async def api_diagonalley_stall_checkshipped(checking_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
async def api_diagonalley_stall_checkshipped(checking_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
rows = await db.fetchone(
|
||||
"SELECT * FROM diagonalley.orders WHERE invoiceid = ?", (checking_id,)
|
||||
)
|
||||
|
|
@ -311,19 +315,19 @@ async def api_diagonalley_stall_checkshipped(checking_id: str = Query(None), wal
|
|||
|
||||
|
||||
@diagonalley_ext.post("/api/v1/stall/order/{stall_id}")
|
||||
async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
product = await get_diagonalley_product(data.id)
|
||||
async def api_diagonalley_stall_order(stall_id, data: createOrder, wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
product = await get_diagonalley_product(data.productid)
|
||||
shipping = await get_diagonalley_stall(stall_id)
|
||||
|
||||
if data.shippingzone == 1:
|
||||
shippingcost = shipping.zone1cost
|
||||
shippingcost = shipping.zone1cost #missing in model
|
||||
else:
|
||||
shippingcost = shipping.zone2cost
|
||||
shippingcost = shipping.zone2cost #missing in model
|
||||
|
||||
checking_id, payment_request = await create_invoice(
|
||||
wallet_id=product.wallet,
|
||||
amount=shippingcost + (data.quantity * product.price),
|
||||
memo=data.id,
|
||||
memo=shipping.wallet,
|
||||
)
|
||||
selling_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
|
||||
await db.execute(
|
||||
|
|
@ -333,8 +337,8 @@ async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo
|
|||
""",
|
||||
(
|
||||
selling_id,
|
||||
data.id,
|
||||
product.wallet,
|
||||
data.productid,
|
||||
product.wallet, #doesn't exist in model
|
||||
product.product,
|
||||
data.quantity,
|
||||
data.shippingzone,
|
||||
|
|
@ -345,4 +349,4 @@ async def api_diagonalley_stall_order(data:createOrders, wallet: WalletTypeInfo
|
|||
False,
|
||||
),
|
||||
)
|
||||
return ({"checking_id": checking_id, "payment_request": payment_request}))
|
||||
return ({"checking_id": checking_id, "payment_request": payment_request})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue