Browse Source

refactor: ad a Database class and clean `deletewallet` view

fee_issues
Eneko Illarramendi 5 years ago
parent
commit
69a979eb8f
  1. 64
      LNbits/__init__.py
  2. BIN
      LNbits/data/database.sqlite3
  3. 30
      LNbits/db.py

64
LNbits/__init__.py

@ -16,12 +16,10 @@ import time
import json
import bech32
from .db import Database, DEFAULT_PATH
from .helpers import encrypt
# DATABASE = 'database.db'
INVOICE_KEY = "YOUR-LNTXBOT-INVOICE-KEY" # In the lntxbot bot on telegram type "/api"
ADMIN_KEY = "YOUR-LNTXBOT-ADMIN-KEY"
API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL"
@ -29,9 +27,6 @@ API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL"
app = Flask(__name__)
DEFAULT_PATH = "database.sqlite3"
def db_connect(db_path=DEFAULT_PATH):
con = sqlite3.connect(db_path)
return con
@ -45,58 +40,21 @@ def home():
@app.route("/deletewallet")
def deletewallet():
thewal = request.args.get("wal")
con = db_connect()
cur = con.cursor()
print(thewal)
cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'")
rowss = cur.fetchall()
with Database() as db:
rowss = db.fetchall("SELECT * FROM wallets WHERE hash = ?", (thewal,))
if len(rowss) > 0:
cur.close()
print(rowss)
con = db_connect()
cur = con.cursor()
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.execute("UPDATE wallets SET user = '" + "del" + rowss[0][4] + "' WHERE hash = '" + rowss[0][0] + "'")
if len(rowsss) > 0:
return render_template("deletewallet.html", theid=rowsss[0][4], thewal=rowsss[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")
return render_template("index.html")
@app.route("/lnurlwallet")

BIN
database.sqlite3 → LNbits/data/database.sqlite3

Binary file not shown.

30
LNbits/db.py

@ -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()
Loading…
Cancel
Save