This commit is contained in:
Tiago vasconcelos 2022-01-27 15:26:55 +00:00
parent 903e0d5b0b
commit 8db457bd42
3 changed files with 64 additions and 66 deletions

View file

@ -1,17 +1,17 @@
import re
from base64 import urlsafe_b64encode from base64 import urlsafe_b64encode
from uuid import uuid4
from typing import List, Optional, Union 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 open_ext_db
from lnbits.db import SQLITE from lnbits.db import SQLITE
from . import db
from .models import Products, Orders, Stalls, Zones
import httpx
from lnbits.helpers import urlsafe_short_hash from lnbits.helpers import urlsafe_short_hash
import re from lnbits.settings import WALLET
from . import db
from .models import Orders, Products, Stalls, Zones, createProduct, createZones
regex = re.compile( regex = re.compile(
r"^(?:http|ftp)s?://" # http:// or https:// r"^(?:http|ftp)s?://" # http:// or https://
@ -28,35 +28,27 @@ regex = re.compile(
async def create_diagonalley_product( async def create_diagonalley_product(
*, data: createProduct
stall_id: str,
product: str,
categories: str,
description: str,
image: Optional[str] = None,
price: int,
quantity: int,
shippingzones: str,
) -> Products: ) -> Products:
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
product_id = urlsafe_short_hash() product_id = urlsafe_short_hash()
# with open_ext_db("diagonalley") as db: # with open_ext_db("diagonalley") as db:
result = await (method)( # result = await (method)(
await db.execute(
f""" 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 (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
{returning}
""", """,
( (
product_id, product_id,
stall_id, data.stall,
product, data.product,
categories, data.categories,
description, data.description,
image, data.image,
price, data.price,
quantity, data.quantity,
), ),
) )
product = await get_diagonalley_product(product_id) product = await get_diagonalley_product(product_id)
@ -109,17 +101,11 @@ async def delete_diagonalley_product(product_id: str) -> None:
async def create_diagonalley_zone( async def create_diagonalley_zone(
*, wallet,
wallet: Optional[str] = None, data: createZones
cost: Optional[int] = 0,
countries: Optional[str] = None,
) -> Zones: ) -> Zones:
returning = "" if db.type == SQLITE else "RETURNING ID"
method = db.execute if db.type == SQLITE else db.fetchone
zone_id = urlsafe_short_hash() zone_id = urlsafe_short_hash()
result = await (method)( await db.execute(
f""" f"""
INSERT INTO diagonalley.zones ( INSERT INTO diagonalley.zones (
id, id,
@ -129,9 +115,8 @@ async def create_diagonalley_zone(
) )
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
{returning}
""", """,
(zone_id, wallet, cost, countries), (zone_id, wallet, data.cost, data.countries),
) )
zone = await get_diagonalley_zone(zone_id) zone = await get_diagonalley_zone(zone_id)

View file

@ -1,12 +1,15 @@
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode, ParseResult import json
from starlette.requests import Request 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 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 lnurl.types import LnurlPayMetadata # type: ignore
from pydantic import BaseModel from pydantic import BaseModel
import json from starlette.requests import Request
from sqlite3 import Row
from lnbits.lnurl import encode as lnurl_encode # type: ignore
class Stalls(BaseModel): class Stalls(BaseModel):
@ -25,23 +28,35 @@ class createStalls(BaseModel):
relays: str = Query(None) relays: str = Query(None)
shippingzones: str = Query(None) shippingzones: str = Query(None)
class Products(BaseModel): class createProduct(BaseModel):
id: str = Query(None)
stall: str = Query(None) stall: str = Query(None)
product: str = Query(None) product: str = Query(None)
categories: str = Query(None) categories: str = Query(None)
description: str = Query(None) description: str = Query(None)
image: str = Query(None) image: str = Query(None)
price: int = Query(0) price: int = Query(0, ge=0)
quantity: int = Query(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): class createZones(BaseModel):
id: str = Query(None)
wallet: str = Query(None)
cost: str = Query(None) cost: str = Query(None)
countries: str = Query(None) countries: str = Query(None)
class Zones(BaseModel):
id: str
wallet: str
cost: str
countries: str
class Orders(BaseModel): class Orders(BaseModel):
id: str = Query(None) id: str = Query(None)

View file

@ -39,7 +39,7 @@ from .crud import (
update_diagonalley_stall, update_diagonalley_stall,
update_diagonalley_zone, update_diagonalley_zone,
) )
from .models import Orders, Products, Stalls from .models import Orders, Products, Stalls, Zones, createProduct, createZones
# from lnbits.db import open_ext_db # from lnbits.db import open_ext_db
@ -79,7 +79,7 @@ async def api_diagonalley_products(
@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(
data: Products, data: createProduct,
product_id: str = Query(None), product_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type) wallet: WalletTypeInfo = Depends(get_key_type)
): ):
@ -93,9 +93,9 @@ async def api_diagonalley_product_create(
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) product = await update_diagonalley_product(product_id, **data.dict())
else: else:
product = await create_diagonalley_product(wallet_id=wallet.wallet.id, data) product = await create_diagonalley_product(data=data)
return product.dict() return product.dict()
@ -126,11 +126,10 @@ async def api_diagonalley_zones(wallet: WalletTypeInfo = Depends(get_key_type),
return ([zone.dict() 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.post("/api/v1/zones")
@diagonalley_ext.put("/api/v1/zones/{zone_id}") @diagonalley_ext.put("/api/v1/zones/{zone_id}")
async def api_diagonalley_zone_create( async def api_diagonalley_zone_create(
data: Zones, data: createZones,
zone_id: str = Query(None), zone_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type) wallet: WalletTypeInfo = Depends(get_key_type)
): ):
@ -138,20 +137,20 @@ async def api_diagonalley_zone_create(
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.wallet != walley.wallet.id: if zone.wallet != wallet.wallet.id:
return ({"message": "Not your record."})) return ({"message": "Not your record."})
zone = await update_diagonalley_zone(zone_id, data) zone = await update_diagonalley_zone(zone_id, **data.dict())
else: 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}") @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) zone = await get_diagonalley_zone(zone_id)
if not zone: if not zone:
@ -161,7 +160,6 @@ async def api_diagonalley_zone_delete(zone_id: str = Query(None), wallet: Walle
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)