lnbits-legend/lnbits/extensions/diagonalley/crud.py

296 lines
8.9 KiB
Python
Raw Normal View History

2020-04-16 21:51:17 +01:00
from base64 import urlsafe_b64encode
from uuid import uuid4
from typing import List, Optional, Union
import httpx
2020-04-16 21:51:17 +01:00
from lnbits.db import open_ext_db
2020-04-17 18:51:24 +01:00
from lnbits.settings import WALLET
2020-04-16 21:51:17 +01:00
from .models import Products, Orders, Indexers
2020-04-17 18:51:24 +01:00
import re
2020-08-30 23:19:43 -03:00
2020-04-17 18:51:24 +01:00
regex = re.compile(
2020-08-30 23:19:43 -03:00
r"^(?:http|ftp)s?://" # http:// or https://
r"(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|"
r"localhost|"
r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
r"(?::\d+)?"
r"(?:/?|[/?]\S+)$",
re.IGNORECASE,
)
2020-04-16 21:51:17 +01:00
###Products
2020-08-30 23:19:43 -03:00
def create_diagonalleys_product(
*,
wallet_id: str,
product: str,
categories: str,
description: str,
image: str,
price: int,
quantity: int,
2020-08-30 23:19:43 -03:00
) -> Products:
2020-04-16 21:51:17 +01:00
with open_ext_db("diagonalley") as db:
2020-08-30 23:19:43 -03:00
product_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
2020-04-16 21:51:17 +01:00
db.execute(
"""
INSERT INTO products (id, wallet, product, categories, description, image, price, quantity)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
product_id,
wallet_id,
product,
categories,
description,
image,
price,
quantity,
),
2020-04-16 21:51:17 +01:00
)
return get_diagonalleys_product(product_id)
2020-04-17 18:51:24 +01:00
def update_diagonalleys_product(product_id: str, **kwargs) -> Optional[Indexers]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
with open_ext_db("diagonalley") as db:
db.execute(
f"UPDATE products SET {q} WHERE id = ?", (*kwargs.values(), product_id)
)
2020-04-17 18:51:24 +01:00
row = db.fetchone("SELECT * FROM products WHERE id = ?", (product_id,))
return get_diagonalleys_indexer(product_id)
2020-04-16 21:51:17 +01:00
def get_diagonalleys_product(product_id: str) -> Optional[Products]:
with open_ext_db("diagonalley") as db:
row = db.fetchone("SELECT * FROM products WHERE id = ?", (product_id,))
return Products(**row) if row else None
def get_diagonalleys_products(wallet_ids: Union[str, List[str]]) -> List[Products]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
with open_ext_db("diagonalley") as db:
q = ",".join(["?"] * len(wallet_ids))
rows = db.fetchall(
f"SELECT * FROM products WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-04-16 21:51:17 +01:00
return [Products(**row) for row in rows]
def delete_diagonalleys_product(product_id: str) -> None:
with open_ext_db("diagonalley") as db:
db.execute("DELETE FROM products WHERE id = ?", (product_id,))
###Indexers
2020-08-30 23:19:43 -03:00
def create_diagonalleys_indexer(
wallet_id: str,
shopname: str,
indexeraddress: str,
shippingzone1: str,
shippingzone2: str,
zone1cost: int,
zone2cost: int,
email: str,
) -> Indexers:
2020-04-16 21:51:17 +01:00
with open_ext_db("diagonalley") as db:
2020-08-30 23:19:43 -03:00
indexer_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
2020-04-16 21:51:17 +01:00
db.execute(
"""
2020-04-17 18:51:24 +01:00
INSERT INTO indexers (id, wallet, shopname, indexeraddress, online, rating, shippingzone1, shippingzone2, zone1cost, zone2cost, email)
2020-04-16 21:51:17 +01:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
2020-08-30 23:19:43 -03:00
(
indexer_id,
wallet_id,
shopname,
indexeraddress,
False,
0,
shippingzone1,
shippingzone2,
zone1cost,
zone2cost,
email,
),
2020-04-16 21:51:17 +01:00
)
2020-04-17 18:51:24 +01:00
return get_diagonalleys_indexer(indexer_id)
def update_diagonalleys_indexer(indexer_id: str, **kwargs) -> Optional[Indexers]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
with open_ext_db("diagonalley") as db:
db.execute(
f"UPDATE indexers SET {q} WHERE id = ?", (*kwargs.values(), indexer_id)
)
2020-04-17 18:51:24 +01:00
row = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,))
2020-04-16 21:51:17 +01:00
return get_diagonalleys_indexer(indexer_id)
def get_diagonalleys_indexer(indexer_id: str) -> Optional[Indexers]:
2020-04-17 18:51:24 +01:00
with open_ext_db("diagonalley") as db:
roww = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,))
try:
x = httpx.get(roww["indexeraddress"] + "/" + roww["ratingkey"])
2020-04-17 18:51:24 +01:00
if x.status_code == 200:
print(x)
print("poo")
with open_ext_db("diagonalley") as db:
2020-09-03 23:02:15 +02:00
db.execute(
"UPDATE indexers SET online = ? WHERE id = ?",
(
True,
indexer_id,
),
)
2020-04-17 18:51:24 +01:00
else:
with open_ext_db("diagonalley") as db:
2020-09-03 23:02:15 +02:00
db.execute(
"UPDATE indexers SET online = ? WHERE id = ?",
(
False,
indexer_id,
),
)
2020-04-17 18:51:24 +01:00
except:
2020-08-30 23:19:43 -03:00
print("An exception occurred")
2020-04-16 21:51:17 +01:00
with open_ext_db("diagonalley") as db:
row = db.fetchone("SELECT * FROM indexers WHERE id = ?", (indexer_id,))
return Indexers(**row) if row else None
def get_diagonalleys_indexers(wallet_ids: Union[str, List[str]]) -> List[Indexers]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
with open_ext_db("diagonalley") as db:
q = ",".join(["?"] * len(wallet_ids))
rows = db.fetchall(
f"SELECT * FROM indexers WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-04-16 21:51:17 +01:00
2020-04-17 18:51:24 +01:00
for r in rows:
try:
x = httpx.get(r["indexeraddress"] + "/" + r["ratingkey"])
2020-04-17 18:51:24 +01:00
if x.status_code == 200:
with open_ext_db("diagonalley") as db:
2020-09-03 23:02:15 +02:00
db.execute(
"UPDATE indexers SET online = ? WHERE id = ?",
(
True,
r["id"],
),
)
2020-04-17 18:51:24 +01:00
else:
with open_ext_db("diagonalley") as db:
2020-09-03 23:02:15 +02:00
db.execute(
"UPDATE indexers SET online = ? WHERE id = ?",
(
False,
r["id"],
),
)
2020-04-17 18:51:24 +01:00
except:
2020-08-30 23:19:43 -03:00
print("An exception occurred")
2020-04-17 18:51:24 +01:00
with open_ext_db("diagonalley") as db:
q = ",".join(["?"] * len(wallet_ids))
rows = db.fetchall(
f"SELECT * FROM indexers WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-04-16 21:51:17 +01:00
return [Indexers(**row) for row in rows]
def delete_diagonalleys_indexer(indexer_id: str) -> None:
with open_ext_db("diagonalley") as db:
db.execute("DELETE FROM indexers WHERE id = ?", (indexer_id,))
###Orders
2020-08-30 23:19:43 -03:00
def create_diagonalleys_order(
*,
productid: str,
wallet: str,
product: str,
quantity: int,
shippingzone: str,
address: str,
email: str,
invoiceid: str,
paid: bool,
shipped: bool,
) -> Indexers:
2020-04-16 21:51:17 +01:00
with open_ext_db("diagonalley") as db:
2020-08-30 23:19:43 -03:00
order_id = urlsafe_b64encode(uuid4().bytes_le).decode("utf-8")
2020-04-16 21:51:17 +01:00
db.execute(
"""
INSERT INTO orders (id, productid, wallet, product, quantity, shippingzone, address, email, invoiceid, paid, shipped)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
order_id,
productid,
wallet,
product,
quantity,
shippingzone,
address,
email,
invoiceid,
False,
False,
),
2020-04-16 21:51:17 +01:00
)
2020-08-30 23:19:43 -03:00
2020-04-16 21:51:17 +01:00
return get_diagonalleys_order(order_id)
def get_diagonalleys_order(order_id: str) -> Optional[Orders]:
with open_ext_db("diagonalley") as db:
row = db.fetchone("SELECT * FROM orders WHERE id = ?", (order_id,))
return Orders(**row) if row else None
def get_diagonalleys_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
with open_ext_db("diagonalley") as db:
q = ",".join(["?"] * len(wallet_ids))
rows = db.fetchall(
f"SELECT * FROM orders WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-04-17 18:51:24 +01:00
for r in rows:
PAID = (await WALLET.get_invoice_status(r["invoiceid"])).paid
2020-04-17 18:51:24 +01:00
if PAID:
with open_ext_db("diagonalley") as db:
2020-09-03 23:02:15 +02:00
db.execute(
"UPDATE orders SET paid = ? WHERE id = ?",
(
True,
r["id"],
),
)
rows = db.fetchall(
f"SELECT * FROM orders WHERE wallet IN ({q})", (*wallet_ids,)
)
2020-04-16 21:51:17 +01:00
return [Orders(**row) for row in rows]
def delete_diagonalleys_order(order_id: str) -> None:
with open_ext_db("diagonalley") as db:
db.execute("DELETE FROM orders WHERE id = ?", (order_id,))