From 52f31da4f873ee08b0ec6a532c7b80d6d21950a9 Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Fri, 13 Dec 2019 18:41:22 +0100 Subject: [PATCH] refactor: clean database calls in `wallet` view --- LNbits/__init__.py | 189 ++++++++++++++------------------------------- 1 file changed, 58 insertions(+), 131 deletions(-) diff --git a/LNbits/__init__.py b/LNbits/__init__.py index e5c268f07..83f5e9785 100644 --- a/LNbits/__init__.py +++ b/LNbits/__init__.py @@ -160,129 +160,58 @@ def lnurlwallet(): @app.route("/wallet") def wallet(): - theid = request.args.get("usr") thewal = request.args.get("wal") - theamt = request.args.get("amt") thenme = request.args.get("nme") if not thewal: return render_template("index.html") - else: - # Checks if the user exists in "accounts" - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from accounts WHERE userhash = '" + str(theid) + "'") - rows = cur.fetchall() - if len(rows) > 0: - cur.close() + with Database() as db: + user_exists = len(db.fetchall("SELECT * FROM accounts WHERE userhash = ?", (theid,))) > 0 - # Yes, check the user has a wallet - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE user = '" + str(theid) + "'") - rowss = cur.fetchall() + # user exists + # ----------- - if len(rowss) > 0: - cur.close() + if user_exists: + user_wallets = db.fetchall("SELECT * FROM wallets WHERE user = ?", (theid,)) - # Checks if the current wallet exists - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'") - rowsss = cur.fetchall() + # user has wallets + # ---------------- - if len(rowsss) > 0: - cur.close() - walb = rowsss[0][1].split(",")[-1] + if len(user_wallets) > 0: + wallet = db.fetchall("SELECT * FROM wallets WHERE hash = ?", (thewal,)) + + if len(wallet) > 0: + walb = wallet[0][1].split(",")[-1] return render_template( "wallet.html", - thearr=rowss, - len=len(rowss), - walnme=rowsss[0][3], + thearr=user_wallets, + len=len(user_wallets), + walnme=wallet[0][3], user=theid, walbal=walb, theid=theid, thewal=thewal, - transactions=rowsss[0][2], - adminkey=rowsss[0][5], - inkey=rowsss[0][6], + transactions=wallet[0][2], + adminkey=wallet[0][5], + inkey=wallet[0][6], ) - else: - cur.close() - con = db_connect() - cur = con.cursor() - - adminkey = encrypt(thewal) - inkey = encrypt(adminkey) - - cur.execute( - "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" - + thewal - + "',',0','0','" - + thenme - + "','" - + theid - + "','" - + adminkey - + "','" - + inkey - + "')" - ) - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE user = '" + str(theid) + "'") - rowss = cur.fetchall() - cur.close() - - return render_template( - "wallet.html", - thearr=rowss, - len=len(rowss), - walnme=thenme, - walbal="0", - theid=theid, - thewal=thewal, - adminkey=adminkey, - inkey=inkey, - ) - else: - cur.close() - - con = db_connect() - cur = con.cursor() - - adminkey = encrypt(theid) + adminkey = encrypt(thewal) inkey = encrypt(adminkey) - cur.execute( - "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" - + thewal - + "',',0','0','" - + thenme - + "','" - + theid - + "','" - + adminkey - + "','" - + inkey - + "')" + db.execute( + "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) " + "VALUES (?, 0, 0, ?, ?, ?, ?)", + (thewal, thenme, theid, adminkey, inkey), ) - con.commit() - cur.close() + rows = db.fetchall("SELECT * FROM wallets WHERE user = ?", (theid,)) return render_template( "wallet.html", - len=len("1"), + thearr=rows, + len=len(rows), walnme=thenme, walbal="0", theid=theid, @@ -291,44 +220,17 @@ def wallet(): inkey=inkey, ) - else: - cur.close() - con = db_connect() - cur = con.cursor() - - cur.execute("INSERT INTO accounts (userhash) VALUES ('" + theid + "')") - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() + # user has no wallets + # ------------------- adminkey = encrypt(theid) inkey = encrypt(adminkey) - cur.execute( - "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" - + thewal - + "',',0','0','" - + thenme - + "','" - + theid - + "','" - + adminkey - + "','" - + inkey - + "')" + db.execute( + "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) " + "VALUES (?, 0, 0, ?, ?, ?, ?)", + (thewal, thenme, theid, adminkey, inkey), ) - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE user = '" + str(theid) + "'") - rows = cur.fetchall() - con.commit() - cur.close() return render_template( "wallet.html", @@ -341,6 +243,31 @@ def wallet(): inkey=inkey, ) + # user does not exist: create an account + # -------------------------------------- + + db.execute("INSERT INTO accounts (userhash) VALUES (?)", (theid,)) + + adminkey = encrypt(theid) + inkey = encrypt(adminkey) + + db.execute( + "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) " + "VALUES (?, 0, 0, ?, ?, ?, ?)", + (thewal, thenme, theid, adminkey, inkey), + ) + + return render_template( + "wallet.html", + len=len("1"), + walnme=thenme, + walbal="0", + theid=theid, + thewal=thewal, + adminkey=adminkey, + inkey=inkey, + ) + # API requests @app.route("/v1/invoices", methods=["GET", "POST"])