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 url

" - f"

Add a site via URL or Reticulum destination hash

" + f"

Add a site to your index

" f'
' f'{_csrf_field()}' - f'

' - f'
' - f'or
' - f'

' + f'' + f'' + f'' + f'' + f'

' + f'
' + f'

' f'

' f'

' f'' f"
" - f"

or manage subscriptions

" 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'
' f'

' f'
' @@ -443,15 +446,11 @@ def handle_add_manual_submit(body): url = clean_url(body.get("url", [""])[0].strip()) note = body.get("note", [""])[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_desc = body.get("manual_description", [""])[0].strip() - if not url and not reticulum_dest: - return handle_add_form("URL or Reticulum destination hash is required.") - - if not url and reticulum_dest: - url = f"reticulum:{reticulum_dest}" + if not url: + return handle_add_form("URL is required.") if not manual_title or not manual_desc: 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") 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, " - "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary, reticulum_dest=excluded.reticulum_dest", - (url, manual_title, manual_desc, note, now, manual_desc[:200], reticulum_dest), + "note=excluded.note, last_modified=excluded.last_modified, summary=excluded.summary", + (url, manual_title, manual_desc, note, now, manual_desc[:200]), ) # Get the page ID @@ -493,6 +492,8 @@ def handle_add_manual_submit(body): def handle_pages(query=None): + msg = query.get("msg", [""])[0] if query else "" + msg_html = 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"

indexed pages ({total})

" + f"{msg_html}" f"" f'{_page_nav(page, total, "/pages", BROWSE_PER_PAGE)}' f'

export | import

' @@ -530,20 +532,27 @@ def handle_pages(query=None): def handle_edit_form(page_id, msg=""): db = get_db() try: - row = db.execute("SELECT id, url, title, note FROM pages WHERE id = ?", (page_id,)).fetchone() + row = db.execute("SELECT id, url, title, body, note, summary FROM pages WHERE id = ?", (page_id,)).fetchone() if not row: return _error(404) tags = ", ".join(_get_page_tags(page_id, db)) finally: return_db(db) + return _respond( f"

edit page

" f"

{esc(row['title'])}
" f"{esc(row['url'])}

" f'
' f'{_csrf_field()}' - f'

' - f'

' + f'
' + f'

' + f'
' + f'

' + f'
' + f'

' + f'
' + f'

' f'' f"
" f"

{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")