trying out htmx for more pieces

htmx
lza_menace 6 months ago
parent 1622c26d75
commit 62b593b6b2

@ -0,0 +1 @@
from lws.factory import create_app

@ -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"<p>you need to authenticate first</p><a href=\"/login\">login</a>"
return app
bcrypt = Bcrypt(create_app())

@ -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:]}"
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()

@ -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": {

@ -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])

@ -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():

@ -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
)

@ -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/<address>/disable")
@bp.route("/wallet/<address>/<status>")
@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/<address>/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/<address>/accept")
@bp.route("/wallet/<address>/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/<address>/reject")
@bp.route("/wallet/<address>/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/<address>/label/<label>")
@login_required
async def label(address, label):
w = Wallet.select().where(Wallet.address == address).first()
if w and label:
w.label = label
w.save()
return redirect(url_for("htmx.show_wallets"))

@ -0,0 +1,13 @@
<form action="#" onsubmit="updateLabel(event)">
<input type="text" name="label" onkeyup="updateLabel(event)" value="{{ label }}" id="newLabel">
</form>
<script>
function updateLabel(e){
e.preventDefault();
if (e.keyCode === 13) {
htmx.ajax('GET', `/wallet/{{ address }}/label/${e.target.value}`, '#show_wallets');
} else if (e.keyCode === 27) {
htmx.ajax('GET', '{{ url_for("htmx.show_wallets") }}', '#show_wallets');
}
}
</script>

@ -1,6 +1,7 @@
<table class="striped">
<thead>
<tr>
<th>Label</th>
<th>Status</th>
<th>Action</th>
<th>Address</th>
@ -8,49 +9,55 @@
</tr>
</thead>
<tbody>
{% for status in accounts %}
{% for account in accounts[status] %}
{% for status in requests %}
{% for request in requests[status] %}
<tr>
<td>?</td>
<td>
<span class="tag text-white {% if status == 'active' %}bg-success{% else %}bg-error{% endif %}">
{{ status | upper }}
<span class="tag text-grey">
PENDING
</span>
</td>
<td>
{% if status == 'active' %}
<a href="/wallet/{{ account['address'] }}/disable" class="">disable</a>
{% else %}
<a href="/wallet/{{ account['address'] }}/enable" class="">enable</a>
{% endif %}
</td>
<td>{{ account['address'] | shorten }}</td>
<td>
<form method="get" action="{{ url_for('wallet.rescan') }}">
{{ account['scan_height'] }}
<input type="integer" name="height" style="width: 5em; display: inline;" />
<input type="hidden" name="address" value="{{ account['address'] }}" />
<button type="submit">Rescan</button>
</form>
<!-- <a hx-get="/htmx/import_wallet" hx-target="#walletForm" class="button primary outline"></a> -->
<a href="/wallet/{{ request['address'] }}/approve" class="button primary outline">Approve</a>
<a href="/wallet/{{ request['address'] }}/deny" class="button secondary outline">Deny</a>
</td>
<td>{{ request['address'] | shorten }}</td>
<td>{{ request['start_height'] }}</td>
</tr>
{% endfor %}
{% endfor %}
{% for status in requests %}
{% for request in requests[status] %}
{% for status in accounts %}
{% for account in accounts[status] %}
<tr>
<td>
<span class="tag text-white bg-dark">
PENDING
<div hx-get="/htmx/label_wallet" hx-target="this" hx-swap="outerHTML" hx-vals='{"address": "{{ account['address'] }}", "label": "{{ account['address'] | find_label }}"}'>{{ account['address'] | find_label }}</div>
</td>
<td>
<span class="tag {% if status == 'active' %}text-primary{% else %}text-error{% endif %}">
{{ status | upper }}
</span>
</td>
<td>
<a href="/wallet/{{ request['address'] }}/accept" class="">approve</a> |
<a href="/wallet/{{ request['address'] }}/reject" class="">reject</a>
{% if status == 'active' %}
<!-- <a href="/wallet/{{ account['address'] }}/inactive" class="button secondary outline">Disable</a> -->
<button class="button primary outline" tx-target="#show_wallets" hx-get="/wallet/{{ account['address'] }}/inactive">Disable</button>
{% else %}
<a href="/wallet/{{ account['address'] }}/active" class="button primary outline">Enable</a>
{% endif %}
</td>
<td>{{ account['address'] | shorten }}</td>
<td>
{{ account['scan_height'] }}
<!--
<form method="get" action="{{ url_for('wallet.rescan') }}">
<input type="integer" name="height" style="width: 5em; display: inline;" />
<input type="hidden" name="address" value="{{ account['address'] }}" />
<button type="submit">Rescan</button>
</form>
-->
</td>
<td>{{ request['address'] | shorten }}</td>
<td>{{ request['start_height'] }}</td>
</tr>
{% endfor %}
{% endfor %}

@ -5,21 +5,18 @@
<h1>Monero Lightwallet Server</h1>
<p>LWS Admin: {{ config.LWS_ADMIN_URL }}</p>
<p>LWS RPC: {{ config.LWS_URL }}</p>
<h3>Accounts</h3>
<div>
<a hx-get="/htmx/import_wallet" hx-target="#walletEvent" class="button primary outline">
Import Wallet
</a>
<a hx-get="/htmx/create_wallet" hx-target="#walletEvent" class="button primary">
Create Wallet
</a>
</div>
<div class="" style="margin-top: 2em">
<p id="walletEvent"></p>
<a hx-get="/htmx/import_wallet" hx-target="#walletForm" class="button primary outline">Import Wallet</a>
<a hx-get="/htmx/create_wallet" hx-target="#walletForm" class="button primary">Create Wallet</a>
<p id="walletForm" style="margin: 2em 0 2em 0"></p>
</div>
<div hx-trigger="every 15s" hx-get="/htmx/show_wallets" hx-target="#show_wallets"></div>
<div hx-trigger="load" hx-get="/htmx/show_wallets" id="show_wallets"></div>
<div>
<h3>Accounts</h3>
<a hx-get="/htmx/show_wallets" hx-target="#show_wallets" class="button outline" hx-indicator="#refreshLoader">Refresh</a>
<p class="indicator" id="refreshLoader">...</p>
<div hx-trigger="load" hx-get="/htmx/show_wallets" id="show_wallets"></div>
</div>
{% endblock %}

@ -21,9 +21,9 @@
<a href="{{ url_for('wallet.rescan', id=wallet.id) }}" class="button dark outline">rescan</a>
{% if wallet.is_active() %}
<a href="{{ url_for('wallet.disable', id=wallet.id) }}" class="button error">disable</a>
<a href="{{ url_for('wallet.modify', id=wallet.id, status) }}" class="button error">disable</a>
{% else %}
<a href="{{ url_for('wallet.enable', id=wallet.id) }}" class="button primary">enable</a>
<a href="{{ url_for('wallet.modify', id=wallet.id, status) }}" class="button primary">enable</a>
{% endif %}
{% else %}
<p>not connected to lws</p>

@ -22,4 +22,4 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
start = "lws.app:app.run"
start = "lws:create_app().run()"

Loading…
Cancel
Save