Add radio toggle for URL vs Reticulum hash input in add page

This commit is contained in:
Test User 2026-03-30 22:54:29 +00:00
parent a3429409eb
commit 7795662154

View file

@ -354,49 +354,54 @@ def handle_add_form(msg="", action_type="index"):
) )
return _respond( return _respond(
f"<h1>add url</h1>" f"<h1>add url</h1>"
f"<p>Add a site via URL or Reticulum destination hash</p>" f"<p>Add a site to your index</p>"
f'<form method="post" action="/add">' f'<form method="post" action="/add">'
f'{_csrf_field()}' f'{_csrf_field()}'
f'<select name="action_type" onchange="this.form.action = this.value === \'subscribe\' ? \'/subscriptions/add\' : \'/add\'">' f'<input type="radio" name="input_type" value="url" id="type_url" checked onchange="toggleInput()">'
f'<option value="/add" selected>Add site (index)</option>' f'<label for="type_url">URL</label>'
f'<option value="/subscriptions/add">Subscribe to instance</option>' f'<input type="radio" name="input_type" value="hash" id="type_hash" onchange="toggleInput()">'
f'</select><br><br>' f'<label for="type_hash">Reticulum Hash</label>'
f'<input name="url" placeholder="https://example.com" size="50"><br>' f'<br><br>'
f'<small>or</small><br>' f'<input name="url" id="url_input" placeholder="https://example.com" size="50"><br>'
f'<input name="reticulum_dest" placeholder="reticulum destination hash (32 hex chars)" size="50"><br><br>' f'<input name="reticulum_dest" id="hash_input" placeholder="reticulum destination hash (32 hex chars)" size="50" style="display:none"><br><br>'
f'<input name="note" placeholder="why are you saving this? (optional)" size="50"><br><br>' f'<input name="note" placeholder="why are you saving this? (optional)" size="50"><br><br>'
f'<input name="tags" placeholder="tags (comma-separated, e.g. solarpunk, mesh)" size="50"><br><br>' f'<input name="tags" placeholder="tags (comma-separated, e.g. solarpunk, mesh)" size="50"><br><br>'
f'<button type="submit">index</button>' f'<button type="submit">index</button>'
f"</form>" f"</form>"
f"<p><small>or <a href=\"/subscriptions\">manage subscriptions</a></small></p>"
f"<p>{msg}</p>" f"<p>{msg}</p>"
f'<a href="/">back</a>' f'<a href="/">back</a>'
f'<script>'
f'function toggleInput() {{'
f' var isUrl = document.getElementById("type_url").checked;'
f' document.getElementById("url_input").style.display = isUrl ? "block" : "none";'
f' document.getElementById("hash_input").style.display = isUrl ? "none" : "block";'
f'}}'
f'</script>'
) )
def handle_add_submit(body): def handle_add_submit(body):
url = clean_url(body.get("url", [""])[0].strip()) input_type = body.get("input_type", ["url"])[0]
url = body.get("url", [""])[0].strip()
reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "")
note = body.get("note", [""])[0].strip() note = body.get("note", [""])[0].strip()
tags = body.get("tags", [""])[0].strip() tags = body.get("tags", [""])[0].strip()
reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "")
if reticulum_dest and url: if input_type == "url":
return handle_add_form("Please provide either a URL or a Reticulum destination hash, not both.") if not url:
return handle_add_form("URL is required.")
if not url and not reticulum_dest: url = clean_url(url)
return handle_add_form("URL or Reticulum destination hash is required.") if not url.startswith(("http://", "https://")):
return handle_add_form("URL must start with http:// or https://")
if reticulum_dest and (len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest)): else:
return handle_add_form("Invalid reticulum destination hash. Must be 32 hex characters.") if not reticulum_dest:
return handle_add_form("Reticulum destination hash is required.")
if url and not url.startswith(("http://", "https://")): if len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest):
return handle_add_form("URL must start with http:// or https://") return handle_add_form("Invalid reticulum destination hash. Must be 32 hex characters.")
if reticulum_dest and not url:
url = f"reticulum:{reticulum_dest}" url = f"reticulum:{reticulum_dest}"
try: try:
title = index_url(url, note, reticulum_dest) title = index_url(url, note, reticulum_dest if reticulum_dest else "")
if tags: if tags:
db = get_db() db = get_db()
try: try:
@ -407,8 +412,7 @@ def handle_add_submit(body):
finally: finally:
return_db(db) return_db(db)
display_url = url if url.startswith("http") else reticulum_dest return handle_add_form(f'Indexed: {esc(url)}')
return handle_add_form(f'Indexed: {esc(display_url)}')
except ValueError as e: except ValueError as e:
return handle_add_form(f"Error: {esc(str(e))}") return handle_add_form(f"Error: {esc(str(e))}")
@ -427,7 +431,6 @@ def handle_add_submit(body):
f'<input type="hidden" name="url" value="{esc(url)}">' f'<input type="hidden" name="url" value="{esc(url)}">'
f'<input type="hidden" name="note" value="{esc(note)}">' f'<input type="hidden" name="note" value="{esc(note)}">'
f'<input type="hidden" name="tags" value="{esc(tags)}">' f'<input type="hidden" name="tags" value="{esc(tags)}">'
f'<input type="hidden" name="reticulum_dest" value="{esc(reticulum_dest)}">'
f'<label>Title:</label><br>' f'<label>Title:</label><br>'
f'<input name="manual_title" size="50" placeholder="page title" required><br><br>' f'<input name="manual_title" size="50" placeholder="page title" required><br><br>'
f'<label>Description:</label><br>' f'<label>Description:</label><br>'
@ -443,15 +446,11 @@ def handle_add_manual_submit(body):
url = clean_url(body.get("url", [""])[0].strip()) url = clean_url(body.get("url", [""])[0].strip())
note = body.get("note", [""])[0].strip() note = body.get("note", [""])[0].strip()
tags = body.get("tags", [""])[0].strip() tags = body.get("tags", [""])[0].strip()
reticulum_dest = body.get("reticulum_dest", [""])[0].strip()
manual_title = body.get("manual_title", [""])[0].strip() manual_title = body.get("manual_title", [""])[0].strip()
manual_desc = body.get("manual_description", [""])[0].strip() manual_desc = body.get("manual_description", [""])[0].strip()
if not url and not reticulum_dest: if not url:
return handle_add_form("URL or Reticulum destination hash is required.") return handle_add_form("URL is required.")
if not url and reticulum_dest:
url = f"reticulum:{reticulum_dest}"
if not manual_title or not manual_desc: if not manual_title or not manual_desc:
return handle_add_form("Title and description are required for manual entry.") return handle_add_form("Title and description are required for manual entry.")
@ -461,10 +460,10 @@ def handle_add_manual_submit(body):
now = __import__("datetime").datetime.now().strftime("%Y-%m-%dT%H:%M:%S") now = __import__("datetime").datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
db.execute( db.execute(
"INSERT INTO pages (url, title, body, note, last_modified, summary, reticulum_dest) VALUES (?, ?, ?, ?, ?, ?, ?) " "INSERT INTO pages (url, title, body, note, last_modified, summary) VALUES (?, ?, ?, ?, ?, ?) "
"ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, " "ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, "
"note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary, reticulum_dest=excluded.reticulum_dest", "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary",
(url, manual_title, manual_desc, note, now, manual_desc[:200], reticulum_dest), (url, manual_title, manual_desc, note, now, manual_desc[:200]),
) )
# Get the page ID # Get the page ID