mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-05 11:18:36 +02:00
refactor: ad a Database class and clean deletewallet
view
This commit is contained in:
@@ -16,12 +16,10 @@ import time
|
|||||||
import json
|
import json
|
||||||
import bech32
|
import bech32
|
||||||
|
|
||||||
|
from .db import Database, DEFAULT_PATH
|
||||||
from .helpers import encrypt
|
from .helpers import encrypt
|
||||||
|
|
||||||
|
|
||||||
# DATABASE = 'database.db'
|
|
||||||
|
|
||||||
INVOICE_KEY = "YOUR-LNTXBOT-INVOICE-KEY" # In the lntxbot bot on telegram type "/api"
|
INVOICE_KEY = "YOUR-LNTXBOT-INVOICE-KEY" # In the lntxbot bot on telegram type "/api"
|
||||||
ADMIN_KEY = "YOUR-LNTXBOT-ADMIN-KEY"
|
ADMIN_KEY = "YOUR-LNTXBOT-ADMIN-KEY"
|
||||||
API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL"
|
API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL"
|
||||||
@@ -29,9 +27,6 @@ API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL"
|
|||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PATH = "database.sqlite3"
|
|
||||||
|
|
||||||
|
|
||||||
def db_connect(db_path=DEFAULT_PATH):
|
def db_connect(db_path=DEFAULT_PATH):
|
||||||
con = sqlite3.connect(db_path)
|
con = sqlite3.connect(db_path)
|
||||||
return con
|
return con
|
||||||
@@ -45,58 +40,21 @@ def home():
|
|||||||
|
|
||||||
@app.route("/deletewallet")
|
@app.route("/deletewallet")
|
||||||
def deletewallet():
|
def deletewallet():
|
||||||
|
|
||||||
thewal = request.args.get("wal")
|
thewal = request.args.get("wal")
|
||||||
|
|
||||||
con = db_connect()
|
with Database() as db:
|
||||||
cur = con.cursor()
|
rowss = db.fetchall("SELECT * FROM wallets WHERE hash = ?", (thewal,))
|
||||||
print(thewal)
|
|
||||||
cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'")
|
|
||||||
rowss = cur.fetchall()
|
|
||||||
|
|
||||||
if len(rowss) > 0:
|
if len(rowss) > 0:
|
||||||
|
db.execute("UPDATE wallets SET user = ? WHERE hash = ?", (f"del{rowss[0][4]}", rowss[0][0]))
|
||||||
|
db.execute("UPDATE wallets SET adminkey = ? WHERE hash = ?", (f"del{rowss[0][5]}", rowss[0][0]))
|
||||||
|
db.execute("UPDATE wallets SET inkey = ? WHERE hash = ?", (f"del{rowss[0][6]}", rowss[0][0]))
|
||||||
|
rowsss = db.fetchall("SELECT * FROM wallets WHERE user = ?", (rowss[0][4],))
|
||||||
|
|
||||||
cur.close()
|
if len(rowsss) > 0:
|
||||||
print(rowss)
|
return render_template("deletewallet.html", theid=rowsss[0][4], thewal=rowsss[0][0])
|
||||||
|
|
||||||
con = db_connect()
|
return render_template("index.html")
|
||||||
cur = con.cursor()
|
|
||||||
|
|
||||||
cur.execute("UPDATE wallets SET user = '" + "del" + rowss[0][4] + "' WHERE hash = '" + rowss[0][0] + "'")
|
|
||||||
|
|
||||||
con.commit()
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
con = db_connect()
|
|
||||||
cur = con.cursor()
|
|
||||||
|
|
||||||
cur.execute("UPDATE wallets SET adminkey = '" + "del" + rowss[0][5] + "' WHERE hash = '" + rowss[0][0] + "'")
|
|
||||||
|
|
||||||
con.commit()
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
con = db_connect()
|
|
||||||
cur = con.cursor()
|
|
||||||
|
|
||||||
cur.execute("UPDATE wallets SET inkey = '" + "del" + rowss[0][6] + "' WHERE hash = '" + rowss[0][0] + "'")
|
|
||||||
|
|
||||||
con.commit()
|
|
||||||
cur.close()
|
|
||||||
|
|
||||||
con = db_connect()
|
|
||||||
cur = con.cursor()
|
|
||||||
print(thewal)
|
|
||||||
cur.execute("select * from wallets WHERE user = '" + rowss[0][4] + "'")
|
|
||||||
rowsss = cur.fetchall()
|
|
||||||
|
|
||||||
if len(rowsss) > 0:
|
|
||||||
cur.close()
|
|
||||||
return render_template("deletewallet.html", theid=rowsss[0][4], thewal=rowsss[0][0])
|
|
||||||
else:
|
|
||||||
return render_template("index.html")
|
|
||||||
|
|
||||||
else:
|
|
||||||
return render_template("index.html")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/lnurlwallet")
|
@app.route("/lnurlwallet")
|
||||||
|
Binary file not shown.
30
LNbits/db.py
Normal file
30
LNbits/db.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import os
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
LNBITS_PATH = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
DEFAULT_PATH = os.path.join(LNBITS_PATH, "data", "database.sqlite3")
|
||||||
|
|
||||||
|
|
||||||
|
class Database:
|
||||||
|
def __init__(self, db_path: str = DEFAULT_PATH):
|
||||||
|
self.path = db_path
|
||||||
|
self.connection = sqlite3.connect(db_path)
|
||||||
|
self.cursor = self.connection.cursor()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.cursor.close()
|
||||||
|
self.connection.close()
|
||||||
|
|
||||||
|
def fetchall(self, query: str, values: tuple) -> list:
|
||||||
|
"""Given a query, return cursor.fetchall() rows."""
|
||||||
|
self.cursor.execute(query, values)
|
||||||
|
return self.cursor.fetchall()
|
||||||
|
|
||||||
|
def execute(self, query: str, values: tuple) -> None:
|
||||||
|
"""Given a query, cursor.execute() it."""
|
||||||
|
self.cursor.execute(query, values)
|
||||||
|
self.connection.commit()
|
Reference in New Issue
Block a user