From 8db457bd423b000d123e91f0c031b02bea0e8d69 Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Thu, 27 Jan 2022 15:26:55 +0000 Subject: [PATCH] zones --- lnbits/extensions/diagonalley/crud.py | 63 +++++++++------------- lnbits/extensions/diagonalley/models.py | 41 +++++++++----- lnbits/extensions/diagonalley/views_api.py | 26 +++++---- 3 files changed, 64 insertions(+), 66 deletions(-) diff --git a/lnbits/extensions/diagonalley/crud.py b/lnbits/extensions/diagonalley/crud.py index c6ce82220..7dc02cd41 100644 --- a/lnbits/extensions/diagonalley/crud.py +++ b/lnbits/extensions/diagonalley/crud.py @@ -1,17 +1,17 @@ +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, createProduct, createZones regex = re.compile( r"^(?:http|ftp)s?://" # http:// or https:// @@ -28,35 +28,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) @@ -109,17 +101,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 +115,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) diff --git a/lnbits/extensions/diagonalley/models.py b/lnbits/extensions/diagonalley/models.py index 0f2a1d785..bd667a2f7 100644 --- a/lnbits/extensions/diagonalley/models.py +++ b/lnbits/extensions/diagonalley/models.py @@ -1,12 +1,15 @@ -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): @@ -25,23 +28,35 @@ class createStalls(BaseModel): relays: str = Query(None) shippingzones: str = Query(None) -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 Orders(BaseModel): id: str = Query(None) diff --git a/lnbits/extensions/diagonalley/views_api.py b/lnbits/extensions/diagonalley/views_api.py index 9df2c1d8e..1ad839364 100644 --- a/lnbits/extensions/diagonalley/views_api.py +++ b/lnbits/extensions/diagonalley/views_api.py @@ -39,7 +39,7 @@ from .crud import ( update_diagonalley_stall, 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 @@ -79,7 +79,7 @@ async def api_diagonalley_products( @diagonalley_ext.post("/api/v1/products") @diagonalley_ext.put("/api/v1/products/{product_id}") async def api_diagonalley_product_create( - data: Products, + data: createProduct, product_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) ): @@ -93,9 +93,9 @@ async def api_diagonalley_product_create( if product.wallet != wallet.wallet.id: 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.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)]) - @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) ): @@ -138,20 +137,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: @@ -161,7 +160,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)