lnbits-legend/lnbits/extensions/tpos/views.py

75 lines
2.3 KiB
Python
Raw Normal View History

2020-02-25 00:45:21 +00:00
import uuid
import json
import requests
2020-04-03 19:59:40 +01:00
from flask import jsonify, render_template, request, redirect, url_for, g
from lnbits.decorators import check_user_exists, validate_uuids
2020-02-25 00:45:21 +00:00
from lnbits.db import open_db, open_ext_db
from lnbits.extensions.tpos import tpos_ext
#add your endpoints here
@tpos_ext.route("/")
2020-04-03 19:59:40 +01:00
@validate_uuids(["usr"], required=True)
@check_user_exists()
2020-02-25 00:45:21 +00:00
def index():
"""Try to add descriptions for others."""
usr = request.args.get("usr")
nme = request.args.get("nme")
2020-02-26 00:40:42 +00:00
wal = request.args.get("wal")
cur = request.args.get("cur")
2020-02-25 00:45:21 +00:00
if usr:
if not len(usr) > 20:
return redirect(url_for("home"))
# Get all the data
with open_db() as db:
user_wallets = db.fetchall("SELECT * FROM wallets WHERE user = ?", (usr,))
2020-02-26 00:40:42 +00:00
user_wall = db.fetchall("SELECT * FROM wallets WHERE user = ? AND id = ?", (usr,wal))
2020-02-25 00:45:21 +00:00
user_ext = db.fetchall("SELECT extension FROM extensions WHERE user = ? AND active = 1", (usr,))
user_ext = [v[0] for v in user_ext]
if nme:
uni = uuid.uuid4().hex
with open_ext_db("tpos") as pos_ext_db:
pos_ext_db.execute(
"""
INSERT OR IGNORE INTO tpos
2020-02-26 00:40:42 +00:00
(nme, uni, usr, invkey, cur)
VALUES (?, ?, ?, ?, ?)
2020-02-25 00:45:21 +00:00
""",
(
nme,
uni,
usr,
2020-02-26 00:40:42 +00:00
user_wall[0][3],
cur,
2020-02-25 00:45:21 +00:00
),
)
with open_ext_db("tpos") as pos_ext_dbb:
user_fau = pos_ext_dbb.fetchall("SELECT * FROM tpos WHERE usr = ?", (usr,))
2020-02-26 00:40:42 +00:00
2020-02-25 00:45:21 +00:00
return render_template(
2020-04-03 20:00:16 +01:00
"tpos/index.html", user_wallets=user_wallets, user_ext=user_ext, usr=usr, user_fau=user_fau, user=g.user
2020-02-25 00:45:21 +00:00
)
@tpos_ext.route("/tpos")
def tpos():
"""Try to add descriptions for others."""
pos = request.args.get("pos")
with open_ext_db("tpos") as pos_ext_dbb:
user_fau = pos_ext_dbb.fetchall("SELECT * FROM tpos WHERE uni = ?", (pos,))
if not user_fau:
return jsonify({"status": "ERROR", "reason":"NO POS"}), 400
2020-02-26 00:40:42 +00:00
r = requests.get("https://api.opennode.co/v1/rates")
r_json = r.json()
2020-02-25 00:45:21 +00:00
return render_template(
2020-02-26 19:12:12 +00:00
"tpos/tpos.html", pos=pos, ratee=user_fau[0][5], exchange=int(r_json["data"]["BTC" + user_fau[0][5]][user_fau[0][5]])
2020-02-25 00:45:21 +00:00
)