refactor: remove databases from code an use schemas instead

All databases are now saved together in the same LNBITS_DATA_FOLDER.
Extensions have to define a schema.yml file for creating the necessary database.
This commit is contained in:
Eneko Illarramendi
2020-02-10 19:40:01 +01:00
parent e3fd6b4ff1
commit 0d33bc2933
14 changed files with 153 additions and 125 deletions
View File
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS withdraws (
key INTEGER PRIMARY KEY AUTOINCREMENT,
usr TEXT,
wal TEXT,
walnme TEXT,
adm INTEGER,
uni TEXT,
tit TEXT,
maxamt INTEGER,
minamt INTEGER,
spent INTEGER,
inc INTEGER,
tme INTEGER,
uniq INTEGER DEFAULT 0,
withdrawals TEXT,
tmestmp INTEGER,
rand TEXT
);
@@ -34,9 +34,9 @@
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
{% if user_ext[0][3] %}
{% if "withdraw" in user_ext %}
<li>
<a href="{{ url_for('withdraw.index') }}?usr={{ user_ext[0][0]}}"
<a href="{{ url_for('withdraw.index') }}?usr={{ user }}"
><i class="fa fa-plus"></i> LNURLw</a>
</li>
{% endif %}
+14 -13
View File
@@ -21,9 +21,8 @@ def index():
# Get all the data
with open_db() as db:
user_wallets = db.fetchall("SELECT * FROM wallets WHERE user = ?", (usr,))
with open_ext_db() as ext_db:
user_ext = ext_db.fetchall("SELECT * FROM overview WHERE user = ?", (usr,))
user_ext = db.fetchall("SELECT * FROM extensions WHERE user = ?", (usr,))
user_ext = [v[0] for v in user_ext]
with open_ext_db("withdraw") as withdraw_ext_db:
user_fau = withdraw_ext_db.fetchall("SELECT * FROM withdraws WHERE usr = ?", (usr,))
@@ -92,10 +91,18 @@ def create():
dt = datetime.now()
seconds = dt.timestamp()
with open_db() as db:
user_ext = db.fetchall("SELECT * FROM extensions WHERE user = ?", (usr,))
user_ext = [v[0] for v in user_ext]
# Add to DB
with open_ext_db("withdraw") as db:
db.execute(
"INSERT OR IGNORE INTO withdraws (usr, wal, walnme, adm, uni, tit, maxamt, minamt, spent, inc, tme, uniq, withdrawals, tmestmp, rand) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
with open_ext_db("withdraw") as withdraw_ext_db:
withdraw_ext_db.execute(
"""
INSERT OR IGNORE INTO withdraws
(usr, wal, walnme, adm, uni, tit, maxamt, minamt, spent, inc, tme, uniq, withdrawals, tmestmp, rand)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
usr,
wall[1],
@@ -115,14 +122,8 @@ def create():
),
)
# Get updated records
with open_ext_db() as ext_db:
user_ext = ext_db.fetchall("SELECT * FROM overview WHERE user = ?", (usr,))
if not user_ext:
return jsonify({"ERROR": "NO WALLET USER"}), 401
with open_ext_db("withdraw") as withdraw_ext_db:
user_fau = withdraw_ext_db.fetchall("SELECT * FROM withdraws WHERE usr = ?", (usr,))
if not user_fau:
return jsonify({"ERROR": "NO WALLET USER"}), 401
+2 -7
View File
@@ -28,7 +28,6 @@ def api_lnurlencode(urlstr, parstr):
else:
rand = randar[0]
url = url_for("withdraw.api_lnurlfetch", _external=True, urlstr=urlstr, parstr=parstr, rand=rand)
return jsonify({"status": "TRUE", "lnurl": lnurl_encode(url.replace("http", "https"))}), 200
@@ -42,7 +41,6 @@ def api_lnurlfetch(parstr, urlstr, rand):
return jsonify({"status": "FALSE", "ERROR": "NO WALL ID"}), 200
if not urlstr:
return jsonify({"status": "FALSE", "ERROR": "NO URL"}), 200
with open_ext_db("withdraw") as withdraw_ext_db:
@@ -58,7 +56,6 @@ def api_lnurlfetch(parstr, urlstr, rand):
default_description="LNbits LNURL withdraw",
)
return res.json(), 200
@@ -79,11 +76,9 @@ def api_lnurlwithdraw(rand):
user_fau = withdraw_ext_db.fetchall("SELECT * FROM withdraws WHERE withdrawals = ?", (k1,))
if not user_fau:
return jsonify({"status": "ERROR", "reason": "NO AUTH"}), 400
if user_fau[0][10] < 1:
return jsonify({"status": "ERROR", "reason": "withdraw SPENT"}), 400
# Check withdraw time
@@ -110,8 +105,8 @@ def api_lnurlwithdraw(rand):
header = {"Content-Type": "application/json", "Grpc-Metadata-macaroon": str(user_fau[0][4])}
data = {"payment_request": pr}
#this works locally but not being served over host, bug, needs fixing
#r = requests.post(url="https://lnbits.com/api/v1/channels/transactions", headers=header, data=json.dumps(data))
# this works locally but not being served over host, bug, needs fixing
# r = requests.post(url="https://lnbits.com/api/v1/channels/transactions", headers=header, data=json.dumps(data))
r = requests.post(url=url_for("api_transactions", _external=True), headers=header, data=json.dumps(data))
r_json = r.json()