mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-09 20:12:34 +02:00
Update views_api.py
This commit is contained in:
@@ -5,7 +5,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
|||||||
from lnbits.helpers import Status
|
from lnbits.helpers import Status
|
||||||
|
|
||||||
from lnbits.extensions.diagonalley import diagonalley_ext
|
from lnbits.extensions.diagonalley import diagonalley_ext
|
||||||
from .crud import create_diagonalleys_product,get_diagonalleys_product,get_diagonalleys_products,delete_diagonalleys_product,create_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order
|
from .crud import create_diagonalleys_product,get_diagonalleys_product,get_diagonalleys_products,delete_diagonalleys_product,create_diagonalleys_indexer,update_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order
|
||||||
from lnbits.core.services import create_invoice
|
from lnbits.core.services import create_invoice
|
||||||
from base64 import urlsafe_b64encode
|
from base64 import urlsafe_b64encode
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -25,6 +25,7 @@ def api_diagonalley_products():
|
|||||||
|
|
||||||
|
|
||||||
@diagonalley_ext.route("/api/v1/diagonalley/products", methods=["POST"])
|
@diagonalley_ext.route("/api/v1/diagonalley/products", methods=["POST"])
|
||||||
|
@diagonalley_ext.route("/api/v1/diagonalley/products<product_id>", methods=["PUT"])
|
||||||
@api_check_wallet_key(key_type="invoice")
|
@api_check_wallet_key(key_type="invoice")
|
||||||
@api_validate_post_request(schema={
|
@api_validate_post_request(schema={
|
||||||
"product": {"type": "string", "empty": False, "required": True},
|
"product": {"type": "string", "empty": False, "required": True},
|
||||||
@@ -34,10 +35,23 @@ def api_diagonalley_products():
|
|||||||
"price": {"type": "integer", "min": 0, "required": True},
|
"price": {"type": "integer", "min": 0, "required": True},
|
||||||
"quantity": {"type": "integer", "min": 0, "required": True}
|
"quantity": {"type": "integer", "min": 0, "required": True}
|
||||||
})
|
})
|
||||||
def api_diagonalley_products_create():
|
def api_diagonalley_product_create(product_id=None):
|
||||||
product = create_diagonalleys_product(wallet_id=g.wallet.id, **g.data)
|
|
||||||
|
if product_id:
|
||||||
|
product = get_diagonalleys_indexer(product_id)
|
||||||
|
|
||||||
|
if not product:
|
||||||
|
return jsonify({"message": "Withdraw product does not exist."}), Status.NOT_FOUND
|
||||||
|
|
||||||
|
if product.wallet != g.wallet.id:
|
||||||
|
return jsonify({"message": "Not your withdraw product."}), Status.FORBIDDEN
|
||||||
|
|
||||||
|
product = update_diagonalleys_product(product_id, **g.data)
|
||||||
|
else:
|
||||||
|
product = create_diagonalleys_product(wallet_id=g.wallet.id, **g.data)
|
||||||
|
|
||||||
|
return jsonify(product._asdict()), Status.OK if product_id else Status.CREATED
|
||||||
|
|
||||||
return jsonify(product._asdict()), Status.CREATED
|
|
||||||
|
|
||||||
|
|
||||||
@diagonalley_ext.route("/api/v1/diagonalley/products/<product_id>", methods=["DELETE"])
|
@diagonalley_ext.route("/api/v1/diagonalley/products/<product_id>", methods=["DELETE"])
|
||||||
@@ -71,6 +85,7 @@ def api_diagonalley_indexers():
|
|||||||
|
|
||||||
|
|
||||||
@diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["POST"])
|
@diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["POST"])
|
||||||
|
@diagonalley_ext.route("/api/v1/diagonalley/indexers<indexer_id>", methods=["PUT"])
|
||||||
@api_check_wallet_key(key_type="invoice")
|
@api_check_wallet_key(key_type="invoice")
|
||||||
@api_validate_post_request(schema={
|
@api_validate_post_request(schema={
|
||||||
"shopname": {"type": "string", "empty": False, "required": True},
|
"shopname": {"type": "string", "empty": False, "required": True},
|
||||||
@@ -81,10 +96,22 @@ def api_diagonalley_indexers():
|
|||||||
"zone1cost": {"type": "integer", "min": 0, "required": True},
|
"zone1cost": {"type": "integer", "min": 0, "required": True},
|
||||||
"zone2cost": {"type": "integer", "min": 0, "required": True}
|
"zone2cost": {"type": "integer", "min": 0, "required": True}
|
||||||
})
|
})
|
||||||
def api_diagonalley_indexer_create():
|
def api_diagonalley_indexer_create(indexer_id=None):
|
||||||
indexer = create_diagonalleys_indexer(wallet_id=g.wallet.id, **g.data)
|
|
||||||
|
|
||||||
return jsonify(indexer._asdict()), Status.CREATED
|
if indexer_id:
|
||||||
|
indexer = get_diagonalleys_indexer(indexer_id)
|
||||||
|
|
||||||
|
if not indexer:
|
||||||
|
return jsonify({"message": "Withdraw indexer does not exist."}), Status.NOT_FOUND
|
||||||
|
|
||||||
|
if indexer.wallet != g.wallet.id:
|
||||||
|
return jsonify({"message": "Not your withdraw indexer."}), Status.FORBIDDEN
|
||||||
|
|
||||||
|
indexer = update_diagonalleys_indexer(indexer_id, **g.data)
|
||||||
|
else:
|
||||||
|
indexer = create_diagonalleys_indexer(wallet_id=g.wallet.id, **g.data)
|
||||||
|
|
||||||
|
return jsonify(indexer._asdict()), Status.OK if indexer_id else Status.CREATED
|
||||||
|
|
||||||
|
|
||||||
@diagonalley_ext.route("/api/v1/diagonalley/indexers/<indexer_id>", methods=["DELETE"])
|
@diagonalley_ext.route("/api/v1/diagonalley/indexers/<indexer_id>", methods=["DELETE"])
|
||||||
@@ -184,7 +211,7 @@ def api_diagonalleys_stall_checkshipped(checking_id):
|
|||||||
|
|
||||||
###Place order
|
###Place order
|
||||||
|
|
||||||
@diagonalley_ext.route("/api/v1/diagonalley/stall/order/<wallet_id>", methods=["POST"])
|
@diagonalley_ext.route("/api/v1/diagonalley/stall/order/<indexer_id>", methods=["POST"])
|
||||||
@api_validate_post_request(schema={
|
@api_validate_post_request(schema={
|
||||||
"id": {"type": "string", "empty": False, "required": True},
|
"id": {"type": "string", "empty": False, "required": True},
|
||||||
"email": {"type": "string", "empty": False, "required": True},
|
"email": {"type": "string", "empty": False, "required": True},
|
||||||
@@ -192,10 +219,16 @@ def api_diagonalleys_stall_checkshipped(checking_id):
|
|||||||
"quantity": {"type": "integer", "empty": False, "required": True},
|
"quantity": {"type": "integer", "empty": False, "required": True},
|
||||||
"shippingzone": {"type": "integer", "empty": False, "required": True},
|
"shippingzone": {"type": "integer", "empty": False, "required": True},
|
||||||
})
|
})
|
||||||
def api_diagonalley_stall_order(wallet_id):
|
def api_diagonalley_stall_order(indexer_id):
|
||||||
product = get_diagonalleys_product(g.data["id"])
|
product = get_diagonalleys_product(g.data["id"])
|
||||||
|
shipping = get_diagonalleys_indexer(indexer_id)
|
||||||
|
|
||||||
checking_id, payment_request = create_invoice(wallet_id=wallet_id, amount=(g.data["quantity"] * product.price), memo=g.data["id"])
|
if g.data["shippingzone"] == 1:
|
||||||
|
shippingcost = shipping.zone1cost
|
||||||
|
else:
|
||||||
|
shippingcost = shipping.zone2cost
|
||||||
|
|
||||||
|
checking_id, payment_request = create_invoice(wallet_id=product.wallet, amount=shippingcost + (g.data["quantity"] * product.price), memo=g.data["id"])
|
||||||
selling_id = urlsafe_b64encode(uuid4().bytes_le).decode('utf-8')
|
selling_id = urlsafe_b64encode(uuid4().bytes_le).decode('utf-8')
|
||||||
with open_ext_db("diagonalley") as db:
|
with open_ext_db("diagonalley") as db:
|
||||||
db.execute(
|
db.execute(
|
||||||
@@ -203,7 +236,7 @@ def api_diagonalley_stall_order(wallet_id):
|
|||||||
INSERT INTO orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped)
|
INSERT INTO orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
""",
|
""",
|
||||||
(selling_id ,g.data["id"] ,wallet_id, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False),
|
(selling_id ,g.data["id"] , product.wallet, product.product, g.data["quantity"], g.data["shippingzone"], g.data["address"], g.data["email"], checking_id, False, False),
|
||||||
)
|
)
|
||||||
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK
|
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user