askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
forum.py · 5.0 KB · 137 lines
| 1 | """Forum landing page — built from metadata, not markup. |
| 2 | |
| 3 | Two sections, each just a mount point for the shared list generator |
| 4 | (/list -> window.dbbasicList): the category board list, and a feed of |
| 5 | recent topics across every category. New Category / New Topic both open |
| 6 | the schema-driven form (/form -> window.dbbasicForm); category_id on the |
| 7 | topic form renders as a select over forum_categories automatically, |
| 8 | because it is declared as a relation field in schemas/forum_topics.json. |
| 9 | |
| 10 | The forum is public read (see permissions/rules.json): anonymous visitors |
| 11 | see both lists. Only signed-in users get the "+ New Category" / "+ New |
| 12 | Topic" buttons and the forms; dbbasicList's own owner match still decides |
| 13 | per-row edit/delete controls. |
| 14 | """ |
| 15 | |
| 16 | _SCRIPT = """ |
| 17 | const catPanel = document.getElementById("catformpanel"); |
| 18 | const topicPanel = document.getElementById("topicformpanel"); |
| 19 | |
| 20 | const categories = window.dbbasicList("forum_categories", { |
| 21 | mount: "#categories", owner: OWNER_ID, |
| 22 | title: (r) => r.name, subtitle: (r) => r.description, |
| 23 | created: (r) => r.created_at, onEdit: (r) => openCatForm(r), |
| 24 | }); |
| 25 | function openCatForm(record) { |
| 26 | document.getElementById("catformtitle").textContent = record ? "Edit Category" : "New Category"; |
| 27 | catPanel.style.display = "block"; |
| 28 | window.dbbasicForm("forum_categories", { |
| 29 | mount: "#catformmount", record: record, owner: OWNER_ID, |
| 30 | onSaved: () => { catPanel.style.display = "none"; categories.reload(); }, |
| 31 | onCancel: () => { catPanel.style.display = "none"; }, |
| 32 | }); |
| 33 | } |
| 34 | const addCat = document.getElementById("addcat"); |
| 35 | if (addCat) addCat.addEventListener("click", () => openCatForm(null)); |
| 36 | |
| 37 | const topics = window.dbbasicList("forum_topics", { |
| 38 | mount: "#topics", search: "#search", sort: "#sort", owner: OWNER_ID, |
| 39 | title: (r) => r.title, href: (r) => "/forum/topics/" + r.id, |
| 40 | subtitle: (r) => { |
| 41 | const bits = []; |
| 42 | if (r.is_pinned === "true") bits.push("pinned"); |
| 43 | if (r.is_solved === "true") bits.push("solved"); |
| 44 | bits.push((r.views || "0") + " views"); |
| 45 | return bits.join(" \\u00b7 "); |
| 46 | }, |
| 47 | created: (r) => r.created_at, onEdit: (r) => openTopicForm(r), |
| 48 | }); |
| 49 | function openTopicForm(record) { |
| 50 | document.getElementById("topicformtitle").textContent = record ? "Edit Topic" : "New Topic"; |
| 51 | topicPanel.style.display = "block"; |
| 52 | window.dbbasicForm("forum_topics", { |
| 53 | mount: "#topicformmount", record: record, owner: OWNER_ID, |
| 54 | onSaved: () => { topicPanel.style.display = "none"; topics.reload(); }, |
| 55 | onCancel: () => { topicPanel.style.display = "none"; }, |
| 56 | }); |
| 57 | } |
| 58 | const addTopic = document.getElementById("addtopic"); |
| 59 | if (addTopic) addTopic.addEventListener("click", () => openTopicForm(null)); |
| 60 | """ |
| 61 | |
| 62 | |
| 63 | def GET(request): |
| 64 | identity = request.get("_identity", {}) |
| 65 | user_id = identity.get("user_id") |
| 66 | _logger.info("site_forum served", user_id=user_id or "anonymous") |
| 67 | |
| 68 | add_cat_btn = ( |
| 69 | '<button class="btn" id="addcat">+ New Category</button>' if user_id else "" |
| 70 | ) |
| 71 | add_topic_btn = ( |
| 72 | '<button class="btn primary" id="addtopic">+ New Topic</button>' if user_id else "" |
| 73 | ) |
| 74 | cat_form_panel = ( |
| 75 | """ |
| 76 | <div id="catformpanel" style="display:none; margin-bottom:1rem"> |
| 77 | <h2 id="catformtitle" style="font-size:1rem; margin:0 0 0.5rem">New Category</h2> |
| 78 | <div id="catformmount"></div> |
| 79 | </div>""" |
| 80 | if user_id |
| 81 | else "" |
| 82 | ) |
| 83 | topic_form_panel = ( |
| 84 | """ |
| 85 | <div id="topicformpanel" style="display:none; margin-bottom:1rem"> |
| 86 | <h2 id="topicformtitle" style="font-size:1rem; margin:0 0 0.5rem">New Topic</h2> |
| 87 | <div id="topicformmount"></div> |
| 88 | </div>""" |
| 89 | if user_id |
| 90 | else "" |
| 91 | ) |
| 92 | body = f""" |
| 93 | <div class="breadcrumb"><a href="/">Home</a> / Forum</div> |
| 94 | <div class="pagehead"><h1>Forum</h1></div> |
| 95 | |
| 96 | <div class="pagehead"><h2 style="font-size:1.1rem; margin:0">Categories</h2>{add_cat_btn}</div> |
| 97 | {cat_form_panel} |
| 98 | <div id="categories"><div class="state">loading…</div></div> |
| 99 | |
| 100 | <div class="pagehead" style="margin-top:2rem"><h2 style="font-size:1.1rem; margin:0">Recent Topics</h2>{add_topic_btn}</div> |
| 101 | {topic_form_panel} |
| 102 | <div class="toolbar"> |
| 103 | <input class="search grow" id="search" placeholder="Search topics…" autocomplete="off"> |
| 104 | <select id="sort"><option value="newest">Newest</option><option value="oldest">Oldest</option></select> |
| 105 | </div> |
| 106 | <div id="topics"><div class="state">loading…</div></div> |
| 107 | """ |
| 108 | |
| 109 | script = ( |
| 110 | f"<script>const OWNER_ID = {user_id or ''!r};</script>" |
| 111 | '<script src="/list"></script><script src="/form"></script>' |
| 112 | f"<script>{_SCRIPT}</script>" |
| 113 | ) |
| 114 | |
| 115 | who = ( |
| 116 | f"signed in as <strong>{user_id}</strong>" |
| 117 | if user_id |
| 118 | else '<a href="/login?next=/forum">sign in</a>' |
| 119 | ) |
| 120 | html = f"""<!doctype html> |
| 121 | <html lang="en"> |
| 122 | <head> |
| 123 | <meta charset="utf-8"> |
| 124 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 125 | <title>Forum</title> |
| 126 | <link rel="stylesheet" href="/style"> |
| 127 | </head> |
| 128 | <body> |
| 129 | <div class="wrap"> |
| 130 | <header class="app"><h1><a href="/">DBBASIC</a></h1><div class="who">{who}</div></header> |
| 131 | {body} |
| 132 | </div> |
| 133 | {script} |
| 134 | <script src="/nav"></script> |
| 135 | </body> |
| 136 | </html>""" |
| 137 | return {"content_type": "text/html; charset=utf-8", "body": html} |