diff --git a/handlers.py b/handlers.py index 0d6135b..bc39743 100644 --- a/handlers.py +++ b/handlers.py @@ -354,49 +354,54 @@ def handle_add_form(msg="", action_type="index"): ) return _respond( f"
Add a site via URL or Reticulum destination hash
" + f"Add a site to your index
" f'" - f"" f"{msg}
" f'back' + f'' ) 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() tags = body.get("tags", [""])[0].strip() - reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "") - if reticulum_dest and url: - return handle_add_form("Please provide either a URL or a Reticulum destination hash, not both.") - - if not url and not reticulum_dest: - return handle_add_form("URL or Reticulum destination hash is required.") - - if reticulum_dest and (len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest)): - return handle_add_form("Invalid reticulum destination hash. Must be 32 hex characters.") - - if url and not url.startswith(("http://", "https://")): - return handle_add_form("URL must start with http:// or https://") - - if reticulum_dest and not url: + if input_type == "url": + if not url: + return handle_add_form("URL is required.") + url = clean_url(url) + if not url.startswith(("http://", "https://")): + return handle_add_form("URL must start with http:// or https://") + else: + if not reticulum_dest: + return handle_add_form("Reticulum destination hash is required.") + if len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest): + return handle_add_form("Invalid reticulum destination hash. Must be 32 hex characters.") url = f"reticulum:{reticulum_dest}" try: - title = index_url(url, note, reticulum_dest) + title = index_url(url, note, reticulum_dest if reticulum_dest else "") if tags: db = get_db() try: @@ -407,8 +412,7 @@ def handle_add_submit(body): finally: return_db(db) - display_url = url if url.startswith("http") else reticulum_dest - return handle_add_form(f'Indexed: {esc(display_url)}') + return handle_add_form(f'Indexed: {esc(url)}') except ValueError as e: return handle_add_form(f"Error: {esc(str(e))}") @@ -427,7 +431,6 @@ def handle_add_submit(body): f'' f'' f'' - f'' f'{esc(msg)}
' if msg else "" page = _paginate(query or {}) offset = (page - 1) * BROWSE_PER_PAGE db = get_db() @@ -520,6 +521,7 @@ def handle_pages(query=None): return_db(db) return _respond( f"{esc(row['title'])}
"
f"{esc(row['url'])}
{msg}
" @@ -552,15 +561,25 @@ def handle_edit_form(page_id, msg=""): def handle_edit_submit(page_id, body): + title = body.get("title", [""])[0].strip() + summary = body.get("summary", [""])[0].strip() note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() + db = get_db() try: - db.execute("UPDATE pages SET note = ? WHERE id = ?", (note, page_id)) + db.execute( + "UPDATE pages SET title = ?, summary = ?, note = ? WHERE id = ?", + (title, summary, note, page_id) + ) + _set_page_tags(page_id, tags, db) + db.commit() + finally: return_db(db) + return _redirect("/pages")