Browse Source

refactor: clean database calls in `wallet` view

fee_issues
Eneko Illarramendi 5 years ago
parent
commit
52f31da4f8
  1. 151
      LNbits/__init__.py

151
LNbits/__init__.py

@ -160,94 +160,58 @@ def lnurlwallet():
@app.route("/wallet") @app.route("/wallet")
def wallet(): def wallet():
theid = request.args.get("usr") theid = request.args.get("usr")
thewal = request.args.get("wal") thewal = request.args.get("wal")
theamt = request.args.get("amt")
thenme = request.args.get("nme") thenme = request.args.get("nme")
if not thewal: if not thewal:
return render_template("index.html") 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: with Database() as db:
cur.close() user_exists = len(db.fetchall("SELECT * FROM accounts WHERE userhash = ?", (theid,))) > 0
# Yes, check the user has a wallet # user exists
con = db_connect() # -----------
cur = con.cursor()
print(thewal)
cur.execute("select * from wallets WHERE user = '" + str(theid) + "'")
rowss = cur.fetchall()
if len(rowss) > 0: if user_exists:
cur.close() user_wallets = db.fetchall("SELECT * FROM wallets WHERE user = ?", (theid,))
# Checks if the current wallet exists # user has wallets
con = db_connect() # ----------------
cur = con.cursor()
print(thewal)
cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'")
rowsss = cur.fetchall()
if len(rowsss) > 0: if len(user_wallets) > 0:
cur.close() wallet = db.fetchall("SELECT * FROM wallets WHERE hash = ?", (thewal,))
walb = rowsss[0][1].split(",")[-1]
if len(wallet) > 0:
walb = wallet[0][1].split(",")[-1]
return render_template( return render_template(
"wallet.html", "wallet.html",
thearr=rowss, thearr=user_wallets,
len=len(rowss), len=len(user_wallets),
walnme=rowsss[0][3], walnme=wallet[0][3],
user=theid, user=theid,
walbal=walb, walbal=walb,
theid=theid, theid=theid,
thewal=thewal, thewal=thewal,
transactions=rowsss[0][2], transactions=wallet[0][2],
adminkey=rowsss[0][5], adminkey=wallet[0][5],
inkey=rowsss[0][6], inkey=wallet[0][6],
) )
else:
cur.close()
con = db_connect()
cur = con.cursor()
adminkey = encrypt(thewal) adminkey = encrypt(thewal)
inkey = encrypt(adminkey) inkey = encrypt(adminkey)
cur.execute( db.execute(
"INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) "
+ thewal "VALUES (?, 0, 0, ?, ?, ?, ?)",
+ "',',0','0','" (thewal, thenme, theid, adminkey, inkey),
+ thenme
+ "','"
+ theid
+ "','"
+ adminkey
+ "','"
+ inkey
+ "')"
) )
con.commit() rows = db.fetchall("SELECT * FROM wallets WHERE user = ?", (theid,))
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( return render_template(
"wallet.html", "wallet.html",
thearr=rowss, thearr=rows,
len=len(rowss), len=len(rows),
walnme=thenme, walnme=thenme,
walbal="0", walbal="0",
theid=theid, theid=theid,
@ -255,30 +219,18 @@ def wallet():
adminkey=adminkey, adminkey=adminkey,
inkey=inkey, inkey=inkey,
) )
else:
cur.close()
con = db_connect() # user has no wallets
cur = con.cursor() # -------------------
adminkey = encrypt(theid) adminkey = encrypt(theid)
inkey = encrypt(adminkey) inkey = encrypt(adminkey)
cur.execute( db.execute(
"INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) "
+ thewal "VALUES (?, 0, 0, ?, ?, ?, ?)",
+ "',',0','0','" (thewal, thenme, theid, adminkey, inkey),
+ thenme
+ "','"
+ theid
+ "','"
+ adminkey
+ "','"
+ inkey
+ "')"
) )
con.commit()
cur.close()
return render_template( return render_template(
"wallet.html", "wallet.html",
@ -291,44 +243,19 @@ def wallet():
inkey=inkey, inkey=inkey,
) )
else: # user does not exist: create an account
cur.close() # --------------------------------------
con = db_connect()
cur = con.cursor()
cur.execute("INSERT INTO accounts (userhash) VALUES ('" + theid + "')")
con.commit()
cur.close()
con = db_connect() db.execute("INSERT INTO accounts (userhash) VALUES (?)", (theid,))
cur = con.cursor()
adminkey = encrypt(theid) adminkey = encrypt(theid)
inkey = encrypt(adminkey) inkey = encrypt(adminkey)
cur.execute( db.execute(
"INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) VALUES ('" "INSERT INTO wallets (hash, balance, transactions, name, user, adminkey, inkey) "
+ thewal "VALUES (?, 0, 0, ?, ?, ?, ?)",
+ "',',0','0','" (thewal, thenme, theid, adminkey, inkey),
+ 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( return render_template(
"wallet.html", "wallet.html",

Loading…
Cancel
Save