chore: use standard library's HTTP status codes

This commit is contained in:
Eneko Illarramendi
2020-05-03 15:57:05 +02:00
parent 3a1167c892
commit 083f7a0a8d
18 changed files with 145 additions and 163 deletions
+2 -2
View File
@@ -1,8 +1,8 @@
from flask import g, abort, render_template
from http import HTTPStatus
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.extensions.amilk import amilk_ext
from lnbits.helpers import Status
from .crud import get_amilk
@@ -16,6 +16,6 @@ def index():
@amilk_ext.route("/<amilk_id>")
def wall(amilk_id):
amilk = get_amilk(amilk_id) or abort(Status.NOT_FOUND, "AMilk does not exist.")
amilk = get_amilk(amilk_id) or abort(HTTPStatus.NOT_FOUND, "AMilk does not exist.")
return render_template("amilk/wall.html", amilk=amilk)
+12 -12
View File
@@ -1,8 +1,8 @@
from flask import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import Status
from lnbits.extensions.amilk import amilk_ext
from .crud import create_amilk, get_amilk, get_amilks, delete_amilk
@@ -23,7 +23,7 @@ def api_amilks():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([amilk._asdict() for amilk in get_amilks(wallet_ids)]), Status.OK
return jsonify([amilk._asdict() for amilk in get_amilks(wallet_ids)]), HTTPStatus.OK
@amilk_ext.route("/api/v1/amilk/milk/<amilk_id>", methods=["GET"])
@@ -34,10 +34,10 @@ def api_amilkit(amilk_id):
try:
withdraw_res = handle_lnurl(milk.lnurl, response_class=LnurlWithdrawResponse)
except LnurlException:
abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
print(withdraw_res.max_sats)
try:
checking_id, payment_request = create_invoice(wallet_id=milk.wallet, amount=withdraw_res.max_sats, memo=memo)
#print(payment_request)
@@ -48,10 +48,10 @@ def api_amilkit(amilk_id):
withdraw_res.callback.base,
params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": payment_request}},
)
if not r.ok:
abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
abort(HTTPStatus.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
for i in range(10):
invoice_status = WALLET.get_invoice_status(checking_id)
@@ -59,10 +59,10 @@ def api_amilkit(amilk_id):
if not invoice_status.paid:
continue
else:
return jsonify({"paid": False}), Status.OK
return jsonify({"paid": False}), HTTPStatus.OK
break
return jsonify({"paid": True}), Status.OK
return jsonify({"paid": True}), HTTPStatus.OK
@amilk_ext.route("/api/v1/amilk", methods=["POST"])
@@ -75,7 +75,7 @@ def api_amilkit(amilk_id):
def api_amilk_create():
amilk = create_amilk(wallet_id=g.wallet.id, lnurl=g.data["lnurl"], atime=g.data["atime"], amount=g.data["amount"])
return jsonify(amilk._asdict()), Status.CREATED
return jsonify(amilk._asdict()), HTTPStatus.CREATED
@amilk_ext.route("/api/v1/amilk/<amilk_id>", methods=["DELETE"])
@@ -84,11 +84,11 @@ def api_amilk_delete(amilk_id):
amilk = get_amilk(amilk_id)
if not amilk:
return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
if amilk.wallet != g.wallet.id:
return jsonify({"message": "Not your amilk."}), Status.FORBIDDEN
return jsonify({"message": "Not your amilk."}), HTTPStatus.FORBIDDEN
delete_amilk(amilk_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
+3 -2
View File
@@ -1,8 +1,9 @@
from flask import g, abort, render_template, jsonify
import json
from flask import g, abort, render_template, jsonify
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.extensions.diagonalley import diagonalley_ext
from lnbits.helpers import Status
from lnbits.db import open_ext_db
+27 -30
View File
@@ -1,8 +1,8 @@
from flask import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import Status
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,update_diagonalleys_indexer,get_diagonalleys_indexer,get_diagonalleys_indexers,delete_diagonalleys_indexer,create_diagonalleys_order,get_diagonalleys_order,get_diagonalleys_orders,delete_diagonalleys_order
@@ -21,7 +21,7 @@ def api_diagonalley_products():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([product._asdict() for product in get_diagonalleys_products(wallet_ids)]), Status.OK
return jsonify([product._asdict() for product in get_diagonalleys_products(wallet_ids)]), HTTPStatus.OK
@diagonalley_ext.route("/api/v1/diagonalley/products", methods=["POST"])
@@ -41,16 +41,16 @@ def api_diagonalley_product_create(product_id=None):
product = get_diagonalleys_indexer(product_id)
if not product:
return jsonify({"message": "Withdraw product does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Withdraw product does not exist."}), HTTPStatus.NOT_FOUND
if product.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw product."}), Status.FORBIDDEN
return jsonify({"message": "Not your withdraw product."}), HTTPStatus.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()), HTTPStatus.OK if product_id else HTTPStatus.CREATED
@@ -60,14 +60,14 @@ def api_diagonalley_products_delete(product_id):
product = get_diagonalleys_product(product_id)
if not product:
return jsonify({"message": "Product does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Product does not exist."}), HTTPStatus.NOT_FOUND
if product.wallet != g.wallet.id:
return jsonify({"message": "Not your Diagon Alley."}), Status.FORBIDDEN
return jsonify({"message": "Not your Diagon Alley."}), HTTPStatus.FORBIDDEN
delete_diagonalleys_product(product_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
@@ -81,7 +81,7 @@ def api_diagonalley_indexers():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([indexer._asdict() for indexer in get_diagonalleys_indexers(wallet_ids)]), Status.OK
return jsonify([indexer._asdict() for indexer in get_diagonalleys_indexers(wallet_ids)]), HTTPStatus.OK
@diagonalley_ext.route("/api/v1/diagonalley/indexers", methods=["POST"])
@@ -102,16 +102,16 @@ def api_diagonalley_indexer_create(indexer_id=None):
indexer = get_diagonalleys_indexer(indexer_id)
if not indexer:
return jsonify({"message": "Withdraw indexer does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Withdraw indexer does not exist."}), HTTPStatus.NOT_FOUND
if indexer.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw indexer."}), Status.FORBIDDEN
return jsonify({"message": "Not your withdraw indexer."}), HTTPStatus.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
return jsonify(indexer._asdict()), HTTPStatus.OK if indexer_id else HTTPStatus.CREATED
@diagonalley_ext.route("/api/v1/diagonalley/indexers/<indexer_id>", methods=["DELETE"])
@@ -120,14 +120,14 @@ def api_diagonalley_indexer_delete(indexer_id):
indexer = get_diagonalleys_indexer(indexer_id)
if not indexer:
return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND
if indexer.wallet != g.wallet.id:
return jsonify({"message": "Not your Indexer."}), Status.FORBIDDEN
return jsonify({"message": "Not your Indexer."}), HTTPStatus.FORBIDDEN
delete_diagonalleys_indexer(indexer_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
###Orders
@@ -140,7 +140,7 @@ def api_diagonalley_orders():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([order._asdict() for order in get_diagonalleys_orders(wallet_ids)]), Status.OK
return jsonify([order._asdict() for order in get_diagonalleys_orders(wallet_ids)]), HTTPStatus.OK
@diagonalley_ext.route("/api/v1/diagonalley/orders", methods=["POST"])
@@ -154,7 +154,7 @@ def api_diagonalley_orders():
})
def api_diagonalley_order_create():
order = create_diagonalleys_order(wallet_id=g.wallet.id, **g.data)
return jsonify(order._asdict()), Status.CREATED
return jsonify(order._asdict()), HTTPStatus.CREATED
@diagonalley_ext.route("/api/v1/diagonalley/orders/<order_id>", methods=["DELETE"])
@@ -163,14 +163,14 @@ def api_diagonalley_order_delete(order_id):
order = get_diagonalleys_order(order_id)
if not order:
return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND
if order.wallet != g.wallet.id:
return jsonify({"message": "Not your Indexer."}), Status.FORBIDDEN
return jsonify({"message": "Not your Indexer."}), HTTPStatus.FORBIDDEN
delete_diagonalleys_indexer(order_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
@diagonalley_ext.route("/api/v1/diagonalley/orders/paid/<order_id>", methods=["GET"])
@@ -178,7 +178,7 @@ def api_diagonalley_order_delete(order_id):
def api_diagonalleys_order_paid(order_id):
with open_ext_db("diagonalley") as db:
db.execute("UPDATE orders SET paid = ? WHERE id = ?", (True, order_id,))
return "", Status.OK
return "", HTTPStatus.OK
@diagonalley_ext.route("/api/v1/diagonalley/orders/shipped/<order_id>", methods=["GET"])
@@ -188,7 +188,7 @@ def api_diagonalleys_order_shipped(order_id):
db.execute("UPDATE orders SET shipped = ? WHERE id = ?", (True, order_id,))
order = db.fetchone("SELECT * FROM orders WHERE id = ?", (order_id,))
return jsonify([order._asdict() for order in get_diagonalleys_orders(order["wallet"])]), Status.OK
return jsonify([order._asdict() for order in get_diagonalleys_orders(order["wallet"])]), HTTPStatus.OK
###List products based on indexer id
@@ -199,13 +199,13 @@ def api_diagonalleys_stall_products(indexer_id):
rows = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,))
print(rows[1])
if not rows:
return jsonify({"message": "Indexer does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Indexer does not exist."}), HTTPStatus.NOT_FOUND
products = db.fetchone("SELECT * FROM products WHERE wallet = ?", (rows[1],))
if not products:
return jsonify({"message": "No products"}), Status.NOT_FOUND
return jsonify({"message": "No products"}), HTTPStatus.NOT_FOUND
return jsonify([products._asdict() for products in get_diagonalleys_products(rows[1])]), Status.OK
return jsonify([products._asdict() for products in get_diagonalleys_products(rows[1])]), HTTPStatus.OK
###Check a product has been shipped
@@ -214,7 +214,7 @@ def api_diagonalleys_stall_checkshipped(checking_id):
with open_ext_db("diagonalley") as db:
rows = db.fetchone("SELECT * FROM orders WHERE invoiceid = ?", (checking_id,))
return jsonify({"shipped": rows["shipped"]}), Status.OK
return jsonify({"shipped": rows["shipped"]}), HTTPStatus.OK
###Place order
@@ -245,7 +245,4 @@ def api_diagonalley_stall_order(indexer_id):
""",
(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}), HTTPStatus.OK
+2 -2
View File
@@ -6,8 +6,8 @@
# import requests
from flask import jsonify
from http import HTTPStatus
from lnbits.helpers import Status
from lnbits.extensions.example import example_ext
@@ -34,4 +34,4 @@ def api_example():
}
]
return jsonify(tools), Status.OK
return jsonify(tools), HTTPStatus.OK
+2 -2
View File
@@ -1,7 +1,7 @@
from flask import g, abort, render_template
from http import HTTPStatus
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.helpers import Status
from lnbits.extensions.paywall import paywall_ext
from .crud import get_paywall
@@ -16,6 +16,6 @@ def index():
@paywall_ext.route("/<paywall_id>")
def display(paywall_id):
paywall = get_paywall(paywall_id) or abort(Status.NOT_FOUND, "Paywall does not exist.")
paywall = get_paywall(paywall_id) or abort(HTTPStatus.NOT_FOUND, "Paywall does not exist.")
return render_template("paywall/display.html", paywall=paywall)
+12 -12
View File
@@ -1,9 +1,9 @@
from flask import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.services import create_invoice
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import Status
from lnbits.settings import WALLET
from lnbits.extensions.paywall import paywall_ext
@@ -18,7 +18,7 @@ def api_paywalls():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([paywall._asdict() for paywall in get_paywalls(wallet_ids)]), Status.OK
return jsonify([paywall._asdict() for paywall in get_paywalls(wallet_ids)]), HTTPStatus.OK
@paywall_ext.route("/api/v1/paywalls", methods=["POST"])
@@ -33,7 +33,7 @@ def api_paywalls():
def api_paywall_create():
paywall = create_paywall(wallet_id=g.wallet.id, **g.data)
return jsonify(paywall._asdict()), Status.CREATED
return jsonify(paywall._asdict()), HTTPStatus.CREATED
@paywall_ext.route("/api/v1/paywalls/<paywall_id>", methods=["DELETE"])
@@ -42,14 +42,14 @@ def api_paywall_delete(paywall_id):
paywall = get_paywall(paywall_id)
if not paywall:
return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
if paywall.wallet != g.wallet.id:
return jsonify({"message": "Not your paywall."}), Status.FORBIDDEN
return jsonify({"message": "Not your paywall."}), HTTPStatus.FORBIDDEN
delete_paywall(paywall_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/invoice", methods=["GET"])
@@ -61,9 +61,9 @@ def api_paywall_get_invoice(paywall_id):
wallet_id=paywall.wallet, amount=paywall.amount, memo=f"#paywall {paywall.memo}"
)
except Exception as e:
return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.OK
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
@paywall_ext.route("/api/v1/paywalls/<paywall_id>/check_invoice", methods=["POST"])
@@ -72,18 +72,18 @@ def api_paywal_check_invoice(paywall_id):
paywall = get_paywall(paywall_id)
if not paywall:
return jsonify({"message": "Paywall does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
try:
is_paid = not WALLET.get_invoice_status(g.data["checking_id"]).pending
except Exception:
return jsonify({"paid": False}), Status.OK
return jsonify({"paid": False}), HTTPStatus.OK
if is_paid:
wallet = get_wallet(paywall.wallet)
payment = wallet.get_payment(g.data["checking_id"])
payment.set_pending(False)
return jsonify({"paid": True, "url": paywall.url}), Status.OK
return jsonify({"paid": True, "url": paywall.url}), HTTPStatus.OK
return jsonify({"paid": False}), Status.OK
return jsonify({"paid": False}), HTTPStatus.OK
+2 -2
View File
@@ -1,7 +1,7 @@
from flask import g, abort, render_template
from http import HTTPStatus
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.helpers import Status
from lnbits.extensions.tpos import tpos_ext
from .crud import get_tpos
@@ -16,6 +16,6 @@ def index():
@tpos_ext.route("/<tpos_id>")
def tpos(tpos_id):
tpos = get_tpos(tpos_id) or abort(Status.NOT_FOUND, "TPoS does not exist.")
tpos = get_tpos(tpos_id) or abort(HTTPStatus.NOT_FOUND, "TPoS does not exist.")
return render_template("tpos/tpos.html", tpos=tpos)
+13 -13
View File
@@ -1,9 +1,9 @@
from flask import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.services import create_invoice
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import Status
from lnbits.settings import WALLET
from lnbits.extensions.tpos import tpos_ext
@@ -18,7 +18,7 @@ def api_tposs():
if "all_wallets" in request.args:
wallet_ids = get_user(g.wallet.user).wallet_ids
return jsonify([tpos._asdict() for tpos in get_tposs(wallet_ids)]), Status.OK
return jsonify([tpos._asdict() for tpos in get_tposs(wallet_ids)]), HTTPStatus.OK
@tpos_ext.route("/api/v1/tposs", methods=["POST"])
@@ -32,7 +32,7 @@ def api_tposs():
def api_tpos_create():
tpos = create_tpos(wallet_id=g.wallet.id, **g.data)
return jsonify(tpos._asdict()), Status.CREATED
return jsonify(tpos._asdict()), HTTPStatus.CREATED
@tpos_ext.route("/api/v1/tposs/<tpos_id>", methods=["DELETE"])
@@ -41,14 +41,14 @@ def api_tpos_delete(tpos_id):
tpos = get_tpos(tpos_id)
if not tpos:
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND
if tpos.wallet != g.wallet.id:
return jsonify({"message": "Not your TPoS."}), Status.FORBIDDEN
return jsonify({"message": "Not your TPoS."}), HTTPStatus.FORBIDDEN
delete_tpos(tpos_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
@tpos_ext.route("/api/v1/tposs/<tpos_id>/invoices/", methods=["POST"])
@@ -57,16 +57,16 @@ def api_tpos_create_invoice(tpos_id):
tpos = get_tpos(tpos_id)
if not tpos:
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND
try:
checking_id, payment_request = create_invoice(
wallet_id=tpos.wallet, amount=g.data["amount"], memo=f"#tpos {tpos.name}"
)
except Exception as e:
return jsonify({"message": str(e)}), Status.INTERNAL_SERVER_ERROR
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.CREATED
@tpos_ext.route("/api/v1/tposs/<tpos_id>/invoices/<checking_id>", methods=["GET"])
@@ -74,18 +74,18 @@ def api_tpos_check_invoice(tpos_id, checking_id):
tpos = get_tpos(tpos_id)
if not tpos:
return jsonify({"message": "TPoS does not exist."}), Status.NOT_FOUND
return jsonify({"message": "TPoS does not exist."}), HTTPStatus.NOT_FOUND
try:
is_paid = not WALLET.get_invoice_status(checking_id).pending
except Exception:
return jsonify({"paid": False}), Status.OK
return jsonify({"paid": False}), HTTPStatus.OK
if is_paid:
wallet = get_wallet(tpos.wallet)
payment = wallet.get_payment(checking_id)
payment.set_pending(False)
return jsonify({"paid": True}), Status.OK
return jsonify({"paid": True}), HTTPStatus.OK
return jsonify({"paid": False}), Status.OK
return jsonify({"paid": False}), HTTPStatus.OK
-1
View File
@@ -2,7 +2,6 @@ from flask import g, abort, render_template, jsonify
import json
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.extensions.usermanager import usermanager_ext
from lnbits.helpers import Status
from lnbits.db import open_ext_db
+12 -13
View File
@@ -1,8 +1,8 @@
from flask import g, jsonify, request
from http import HTTPStatus
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import Status
from lnbits.extensions.usermanager import usermanager_ext
from .crud import create_usermanager_user, get_usermanager_user, get_usermanager_users, get_usermanager_wallet_transactions, get_usermanager_wallet_balances, delete_usermanager_user, create_usermanager_wallet, get_usermanager_wallet, get_usermanager_wallets, delete_usermanager_wallet
@@ -19,7 +19,7 @@ from lnbits.db import open_ext_db
@api_check_wallet_key(key_type="invoice")
def api_usermanager_users():
user_id = g.wallet.user
return jsonify([user._asdict() for user in get_usermanager_users(user_id)]), Status.OK
return jsonify([user._asdict() for user in get_usermanager_users(user_id)]), HTTPStatus.OK
@usermanager_ext.route("/api/v1/users", methods=["POST"])
@@ -31,7 +31,7 @@ def api_usermanager_users():
})
def api_usermanager_users_create():
user = create_usermanager_user(g.data["user_name"], g.data["wallet_name"], g.data["admin_id"])
return jsonify(user._asdict()), Status.CREATED
return jsonify(user._asdict()), HTTPStatus.CREATED
@usermanager_ext.route("/api/v1/users/<user_id>", methods=["DELETE"])
@@ -40,9 +40,9 @@ def api_usermanager_users_delete(user_id):
print("cunt")
user = get_usermanager_user(user_id)
if not user:
return jsonify({"message": "User does not exist."}), Status.NOT_FOUND
return jsonify({"message": "User does not exist."}), HTTPStatus.NOT_FOUND
delete_usermanager_user(user_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
###Wallets
@@ -51,7 +51,7 @@ def api_usermanager_users_delete(user_id):
@api_check_wallet_key(key_type="invoice")
def api_usermanager_wallets():
user_id = g.wallet.user
return jsonify([wallet._asdict() for wallet in get_usermanager_wallets(user_id)]), Status.OK
return jsonify([wallet._asdict() for wallet in get_usermanager_wallets(user_id)]), HTTPStatus.OK
@usermanager_ext.route("/api/v1/wallets", methods=["POST"])
@@ -60,23 +60,23 @@ def api_usermanager_wallets():
"user_id": {"type": "string", "empty": False, "required": True},
"wallet_name": {"type": "string", "empty": False, "required": True},
"admin_id": {"type": "string", "empty": False, "required": True}
})
def api_usermanager_wallets_create():
user = create_usermanager_wallet(g.data["user_id"], g.data["wallet_name"], g.data["admin_id"])
return jsonify(user._asdict()), Status.CREATED
return jsonify(user._asdict()), HTTPStatus.CREATED
@usermanager_ext.route("/api/v1/wallets<wallet_id>", methods=["GET"])
@api_check_wallet_key(key_type="invoice")
def api_usermanager_wallet_transactions(wallet_id):
return jsonify(get_usermanager_wallet_transactions(wallet_id)), Status.OK
return jsonify(get_usermanager_wallet_transactions(wallet_id)), HTTPStatus.OK
@usermanager_ext.route("/api/v1/wallets/<user_id>", methods=["GET"])
@api_check_wallet_key(key_type="invoice")
def api_usermanager_wallet_balances(user_id):
return jsonify(get_usermanager_wallet_balances(user_id)), Status.OK
return jsonify(get_usermanager_wallet_balances(user_id)), HTTPStatus.OK
@usermanager_ext.route("/api/v1/wallets/<wallet_id>", methods=["DELETE"])
@@ -85,9 +85,8 @@ def api_usermanager_wallets_delete(wallet_id):
wallet = get_usermanager_wallet(wallet_id)
print(wallet.id)
if not wallet:
return jsonify({"message": "Wallet does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Wallet does not exist."}), HTTPStatus.NOT_FOUND
delete_usermanager_wallet(wallet_id, wallet.user)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
+3 -3
View File
@@ -1,7 +1,7 @@
from flask import g, abort, render_template
from http import HTTPStatus
from lnbits.decorators import check_user_exists, validate_uuids
from lnbits.helpers import Status
from lnbits.extensions.withdraw import withdraw_ext
from .crud import get_withdraw_link
@@ -16,13 +16,13 @@ def index():
@withdraw_ext.route("/<link_id>")
def display(link_id):
link = get_withdraw_link(link_id) or abort(Status.NOT_FOUND, "Withdraw link does not exist.")
link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
return render_template("withdraw/display.html", link=link)
@withdraw_ext.route("/print/<link_id>")
def print_qr(link_id):
link = get_withdraw_link(link_id) or abort(Status.NOT_FOUND, "Withdraw link does not exist.")
link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
return render_template("withdraw/print_qr.html", link=link)
+25 -24
View File
@@ -1,11 +1,12 @@
from datetime import datetime
from flask import g, jsonify, request
from http import HTTPStatus
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
from lnbits.core.crud import get_user
from lnbits.core.services import pay_invoice
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import urlsafe_short_hash, Status
from lnbits.helpers import urlsafe_short_hash
from lnbits.extensions.withdraw import withdraw_ext
from .crud import (
@@ -29,12 +30,12 @@ def api_links():
try:
return (
jsonify([{**link._asdict(), **{"lnurl": link.lnurl}} for link in get_withdraw_links(wallet_ids)]),
Status.OK,
HTTPStatus.OK,
)
except LnurlInvalidUrl:
return (
jsonify({"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."}),
Status.UPGRADE_REQUIRED,
HTTPStatus.UPGRADE_REQUIRED,
)
@@ -44,12 +45,12 @@ def api_link_retrieve(link_id):
link = get_withdraw_link(link_id)
if not link:
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
if link.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), Status.OK
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK
@withdraw_ext.route("/api/v1/links", methods=["POST"])
@@ -67,25 +68,25 @@ def api_link_retrieve(link_id):
)
def api_link_create_or_update(link_id=None):
if g.data["max_withdrawable"] < g.data["min_withdrawable"]:
return jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), Status.BAD_REQUEST
return jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), HTTPStatus.BAD_REQUEST
if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat:
return jsonify({"message": "Insufficient balance."}), Status.FORBIDDEN
return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN
if link_id:
link = get_withdraw_link(link_id)
if not link:
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
if link.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
link = update_withdraw_link(link_id, **g.data)
else:
link = create_withdraw_link(wallet_id=g.wallet.id, **g.data)
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), Status.OK if link_id else Status.CREATED
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK if link_id else HTTPStatus.CREATED
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["DELETE"])
@@ -94,14 +95,14 @@ def api_link_delete(link_id):
link = get_withdraw_link(link_id)
if not link:
return jsonify({"message": "Withdraw link does not exist."}), Status.NOT_FOUND
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
if link.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw link."}), Status.FORBIDDEN
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
delete_withdraw_link(link_id)
return "", Status.NO_CONTENT
return "", HTTPStatus.NO_CONTENT
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>", methods=["GET"])
@@ -109,11 +110,11 @@ def api_lnurl_response(unique_hash):
link = get_withdraw_link_by_hash(unique_hash)
if not link:
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), Status.OK
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
link = update_withdraw_link(link.id, k1=urlsafe_short_hash())
return jsonify(link.lnurl_response.dict()), Status.OK
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK
@withdraw_ext.route("/api/v1/lnurl/cb/<unique_hash>", methods=["GET"])
@@ -124,16 +125,16 @@ def api_lnurl_callback(unique_hash):
now = int(datetime.now().timestamp())
if not link:
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), Status.OK
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
if link.is_spent:
return jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), Status.OK
return jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), HTTPStatus.OK
if link.k1 != k1:
return jsonify({"status": "ERROR", "reason": "Bad request."}), Status.OK
return jsonify({"status": "ERROR", "reason": "Bad request."}), HTTPStatus.OK
if now < link.open_time:
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), Status.OK
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), HTTPStatus.OK
try:
pay_invoice(wallet_id=link.wallet, bolt11=payment_request, max_sat=link.max_withdrawable)
@@ -149,10 +150,10 @@ def api_lnurl_callback(unique_hash):
update_withdraw_link(link.id, **changes)
except ValueError as e:
return jsonify({"status": "ERROR", "reason": str(e)}), Status.OK
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
except PermissionError:
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}), Status.OK
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}), HTTPStatus.OK
except Exception as e:
return jsonify({"status": "ERROR", "reason": str(e)}), Status.OK
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
return jsonify({"status": "OK"}), Status.OK
return jsonify({"status": "OK"}), HTTPStatus.OK