From 62b593b6b2135a4b2be15de239456592aff42875 Mon Sep 17 00:00:00 2001 From: lza_menace Date: Mon, 13 Nov 2023 13:18:01 -0800 Subject: [PATCH] trying out htmx for more pieces --- lwsadmin/lws/__init__.py | 1 + lwsadmin/lws/factory.py | 6 +- lwsadmin/lws/filters.py | 12 +++- lwsadmin/lws/helpers.py | 3 +- lwsadmin/lws/models.py | 21 ++++++- lwsadmin/lws/routes/htmx.py | 14 ++++- lwsadmin/lws/routes/meta.py | 8 +-- lwsadmin/lws/routes/wallet.py | 55 +++++++++------- lwsadmin/lws/templates/htmx/label_wallet.html | 13 ++++ lwsadmin/lws/templates/htmx/show_wallets.html | 63 ++++++++++--------- lwsadmin/lws/templates/index.html | 21 +++---- lwsadmin/lws/templates/wallet/show.html | 4 +- lwsadmin/pyproject.toml | 2 +- 13 files changed, 142 insertions(+), 81 deletions(-) create mode 100644 lwsadmin/lws/templates/htmx/label_wallet.html diff --git a/lwsadmin/lws/__init__.py b/lwsadmin/lws/__init__.py index e69de29..44b7576 100644 --- a/lwsadmin/lws/__init__.py +++ b/lwsadmin/lws/__init__.py @@ -0,0 +1 @@ +from lws.factory import create_app \ No newline at end of file diff --git a/lwsadmin/lws/factory.py b/lwsadmin/lws/factory.py index 15a8a6c..b93790d 100644 --- a/lwsadmin/lws/factory.py +++ b/lwsadmin/lws/factory.py @@ -30,7 +30,11 @@ def create_app(): @app.errorhandler(Unauthorized) async def redirect_to_login(*_): - return redirect(f"/login?next={request.path}") + if request.path == "/": + return redirect(f"/login?next={request.path}") + else: + return f"

you need to authenticate first

login" + return app bcrypt = Bcrypt(create_app()) \ No newline at end of file diff --git a/lwsadmin/lws/filters.py b/lwsadmin/lws/filters.py index bd70a3a..a840e36 100644 --- a/lwsadmin/lws/filters.py +++ b/lwsadmin/lws/filters.py @@ -1,6 +1,8 @@ from monero.numbers import from_atomic from quart import Blueprint +from lws.models import Wallet, get_random_words + bp = Blueprint('filters', 'filters') @@ -12,4 +14,12 @@ def atomic(amt): @bp.app_template_filter('shorten') def shorten(s): - return f"{s[:6]}...{s[-6:]}" \ No newline at end of file + return f"{s[:6]}...{s[-6:]}" + +@bp.app_template_filter('find_label') +def find_label(s): + w = Wallet.select().where(Wallet.address == s).first() + if w: + return w.label + else: + return get_random_words() \ No newline at end of file diff --git a/lwsadmin/lws/helpers.py b/lwsadmin/lws/helpers.py index 1dff371..65d917c 100644 --- a/lwsadmin/lws/helpers.py +++ b/lwsadmin/lws/helpers.py @@ -126,9 +126,8 @@ class LWS: print(f"Failed to add wallet {address}: {e}") return {} - def modify_wallet(self, address: str, active: bool) -> dict: + def modify_wallet(self, address: str, status: str) -> dict: endpoint = f"{config.LWS_ADMIN_URL}/modify_account_status" - status = "active" if active else "inactive" data = { "auth": self.admin_key, "params": { diff --git a/lwsadmin/lws/models.py b/lwsadmin/lws/models.py index bb1c0cf..5a30918 100644 --- a/lwsadmin/lws/models.py +++ b/lwsadmin/lws/models.py @@ -1,9 +1,16 @@ +from random import choice from datetime import datetime from peewee import * +from monero.wordlists import English -db = SqliteDatabase('data/lws.db') +db = SqliteDatabase("data/lws.db") + + +def get_random_words(): + e = English().word_list + return f"{choice(e)}-{choice(e)}-{choice(e)}" class User(Model): @@ -17,4 +24,14 @@ class User(Model): database = db -db.create_tables([User]) +class Wallet(Model): + date = DateTimeField(default=datetime.utcnow) + address = CharField() + view_key = CharField() + label = CharField(default=get_random_words, null=False) + + class Meta: + database = db + + +db.create_tables([User, Wallet]) diff --git a/lwsadmin/lws/routes/htmx.py b/lwsadmin/lws/routes/htmx.py index ed75391..2a98196 100644 --- a/lwsadmin/lws/routes/htmx.py +++ b/lwsadmin/lws/routes/htmx.py @@ -1,4 +1,4 @@ -from quart import Blueprint, render_template +from quart import Blueprint, render_template, request from monero.seed import Seed from quart_auth import login_required @@ -10,7 +10,6 @@ bp = Blueprint('htmx', 'htmx', url_prefix="/htmx") @bp.route("/create_wallet") -@login_required async def create_wallet(): seed = Seed() return await render_template( @@ -24,10 +23,19 @@ async def create_wallet(): ) @bp.route("/import_wallet") -@login_required async def import_wallet(): return await render_template("htmx/import_wallet.html") +@bp.route("/label_wallet") +async def label_wallet(): + address = request.args.get("address") + label = request.args.get("label") + return await render_template( + "htmx/label_wallet.html", + address=address, + label=label + ) + @bp.route("/show_wallets") @login_required async def show_wallets(): diff --git a/lwsadmin/lws/routes/meta.py b/lwsadmin/lws/routes/meta.py index 54c6f81..3339a28 100644 --- a/lwsadmin/lws/routes/meta.py +++ b/lwsadmin/lws/routes/meta.py @@ -14,15 +14,9 @@ bp = Blueprint("meta", "meta") async def index(): admin = User.select().first() lws.init(admin.view_key) - accounts = lws.list_accounts() - if 'hidden' in accounts: - del accounts["hidden"] - requests = lws.list_requests() return await render_template( "index.html", - config=config, - accounts=accounts, - requests=requests + config=config ) diff --git a/lwsadmin/lws/routes/wallet.py b/lwsadmin/lws/routes/wallet.py index aad9cf5..216d2ac 100644 --- a/lwsadmin/lws/routes/wallet.py +++ b/lwsadmin/lws/routes/wallet.py @@ -1,11 +1,12 @@ import monero.address -from quart import Blueprint, render_template, request, flash, redirect +from quart import Blueprint, render_template, request, flash, redirect, url_for from quart_auth import login_required, current_user from lws.helpers import lws +from lws.models import Wallet, get_random_words -bp = Blueprint('wallet', 'wallet') +bp = Blueprint("wallet", "wallet") @bp.route("/wallet/add", methods=["GET", "POST"]) @@ -13,6 +14,7 @@ bp = Blueprint('wallet', 'wallet') async def add(): form = await request.form if form: + label = form.get("label") address = form.get("address", "") view_key = form.get("view_key", "") restore_height = form.get("restore_height", None) @@ -35,16 +37,23 @@ async def add(): lws.add_wallet(address, view_key) if restore_height != "-1": lws.rescan(address, int(restore_height)) + w = Wallet( + address=address, + view_key=view_key, + label=label if label else get_random_words() + ) + w.save() await flash("wallet added") return redirect(f"/") - return await render_template("wallet/add.html") + else: + return "" @bp.route("/wallet/rescan") @login_required async def rescan(): - address = request.args.get('address') - height = request.args.get('height') + address = request.args.get("address") + height = request.args.get("height") if not address or not height: await flash("you need to provide both address and height") return redirect("/") @@ -55,33 +64,35 @@ async def rescan(): return redirect(f"/") -@bp.route("/wallet/
/disable") +@bp.route("/wallet/
/") @login_required -async def disable(address): - lws.modify_wallet(address, False) - await flash(f"{address} disabled in LWS") - return redirect(f"/") - +async def modify(address, status): + lws.modify_wallet(address, status) + await flash(f"{address} {status} in LWS") + return redirect(url_for("htmx.show_wallets")) -@bp.route("/wallet/
/enable") -@login_required -async def enable(address): - lws.modify_wallet(address, True) - await flash(f"{address} enabled in LWS") - return redirect(f"/") -@bp.route("/wallet/
/accept") +@bp.route("/wallet/
/approve") @login_required async def accept(address): lws.accept_request(address) await flash(f"{address} accepted") - return redirect(f"/") + return redirect(url_for("htmx.show_wallets")) -@bp.route("/wallet/
/reject") +@bp.route("/wallet/
/deny") @login_required async def reject(address): lws.reject_request(address) - await flash(f"{address} rejected") - return redirect(f"/") + await flash(f"{address} denied") + return redirect(url_for("htmx.show_wallets")) + +@bp.route("/wallet/
/label/