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
detail.py · 6.1 KB · 126 lines
| 1 | """Detail generator, served at /detail as window.dbbasicDetail. |
| 2 | |
| 3 | Sibling to /list and /form (same convention: a public static script, no |
| 4 | server state). `59-detail-related-spec.md`'s detail mode is explicitly NOT |
| 5 | a second field-renderer -- it reuses /form's schema fetch, field order, |
| 6 | relation display-field resolution, and type-aware value formatting |
| 7 | (`window.dbbasicForm.readOnly`, in form.py) in forced-read-only mode. This |
| 8 | file is thin on purpose: it fetches the one record (the record read is the |
| 9 | only permission gate, the same posture /list and /form already have) and |
| 10 | hands it to that shared renderer. A page that mounts a `detail` block |
| 11 | loads both `<script src="/form">` and `<script src="/detail">` (see |
| 12 | packages/app-views/objects/site/view_render.py). |
| 13 | |
| 14 | Owner-aware edit/delete (Stage 6 extension of 59): when a `detail` block |
| 15 | declares `editable`/`deletable` AND the viewer owns the record |
| 16 | (`record[owner_field] === viewer_id`, owner_field defaulting to owner_id), |
| 17 | the read-only view gains Edit and Delete affordances -- Edit swaps the same |
| 18 | mount into /form's EXISTING edit pipeline (window.dbbasicForm with a |
| 19 | record, which PUTs and calls onSaved) and Delete issues the ordinary record |
| 20 | DELETE then redirects. This is what lets the hand-written per-collection |
| 21 | `*_view.py` detail pages (each re-implementing fetch + render + owner-edit + |
| 22 | delete) collapse into one view record + this one renderer: read-only for |
| 23 | everyone, owner-editable for the owner, no bespoke page code. Non-owners |
| 24 | and anonymous visitors see exactly the read-only view they saw before -- |
| 25 | the affordances are never shown, and the record write would be |
| 26 | permission-denied regardless (this only gates the UI, not the gate). |
| 27 | |
| 28 | Mounts via window.dbbasicDetail.mount(el, {collection, record_id, |
| 29 | editable?, deletable?, delete_redirect?, owner_field?, viewer_id?}). |
| 30 | """ |
| 31 | |
| 32 | _JS = r""" |
| 33 | (function () { |
| 34 | const qs = (m) => typeof m === "string" ? document.querySelector(m) : m; |
| 35 | |
| 36 | async function renderView(mount, collection, record, opts) { |
| 37 | mount.innerHTML = '<div class="detailview"></div><div class="detailtools"></div>'; |
| 38 | const viewEl = mount.querySelector(".detailview"); |
| 39 | const toolsEl = mount.querySelector(".detailtools"); |
| 40 | await window.dbbasicForm.readOnly(collection, {mount: viewEl, record: record}); |
| 41 | |
| 42 | // Owner-only affordances. `viewer_id` comes from the page (VIEWER_ID, |
| 43 | // embedded server-side by view_render); a field the viewer can't read |
| 44 | // is already absent from `record`, so a hidden owner_field simply means |
| 45 | // "not owner" here, failing closed. |
| 46 | const ownerField = opts.owner_field || "owner_id"; |
| 47 | const isOwner = !!opts.viewer_id && !!record[ownerField] && record[ownerField] === opts.viewer_id; |
| 48 | if (!isOwner || (!opts.editable && !opts.deletable)) return; |
| 49 | |
| 50 | const btns = []; |
| 51 | if (opts.editable) btns.push('<button class="btn" data-act="edit">Edit</button>'); |
| 52 | if (opts.deletable) btns.push('<button class="btn danger" data-act="delete">Delete</button>'); |
| 53 | toolsEl.innerHTML = btns.join(""); |
| 54 | |
| 55 | const editBtn = toolsEl.querySelector('[data-act="edit"]'); |
| 56 | if (editBtn) editBtn.addEventListener("click", () => startEdit(mount, viewEl, toolsEl, collection, record, opts)); |
| 57 | const delBtn = toolsEl.querySelector('[data-act="delete"]'); |
| 58 | if (delBtn) delBtn.addEventListener("click", () => doDelete(collection, record, opts)); |
| 59 | } |
| 60 | |
| 61 | function startEdit(mount, viewEl, toolsEl, collection, record, opts) { |
| 62 | // Guard against view_render's subscribe-triggered re-mount clobbering an |
| 63 | // in-progress edit (see mount() below). |
| 64 | mount._dbbasicEditing = true; |
| 65 | toolsEl.innerHTML = '<button class="btn" data-act="cancel">Cancel</button>'; |
| 66 | toolsEl.querySelector('[data-act="cancel"]').addEventListener("click", () => { |
| 67 | mount._dbbasicEditing = false; |
| 68 | renderView(mount, collection, record, opts); |
| 69 | }); |
| 70 | // Reuse /form's edit pipeline unchanged: a record present => PUT, and |
| 71 | // onSaved hands back the stored row so we re-render read-only in place. |
| 72 | window.dbbasicForm(collection, { |
| 73 | mount: viewEl, |
| 74 | record: record, |
| 75 | onSaved: (updated) => { |
| 76 | mount._dbbasicEditing = false; |
| 77 | renderView(mount, collection, updated || record, opts); |
| 78 | }, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | async function doDelete(collection, record, opts) { |
| 83 | const label = String(collection).replace(/_/g, " ").replace(/s$/, "") || "record"; |
| 84 | if (!window.confirm("Delete this " + label + "?")) return; |
| 85 | const res = await fetch("/collections/" + encodeURIComponent(collection) + "/records/" + encodeURIComponent(record.id), |
| 86 | {method: "DELETE", credentials: "same-origin", headers: {accept: "application/json"}}); |
| 87 | if (res.ok) window.location = opts.delete_redirect || ("/" + collection); |
| 88 | } |
| 89 | |
| 90 | window.dbbasicDetail = { |
| 91 | mount: async function (mount, opts) { |
| 92 | mount = qs(mount); |
| 93 | opts = opts || {}; |
| 94 | const collection = opts.collection; |
| 95 | const recordId = opts.record_id; |
| 96 | if (!collection || !recordId) { |
| 97 | mount.innerHTML = '<div class="viewblock-error">detail needs a collection and record_id</div>'; |
| 98 | return; |
| 99 | } |
| 100 | if (!window.dbbasicForm || !window.dbbasicForm.readOnly) { |
| 101 | mount.innerHTML = '<div class="viewblock-error">detail generator unavailable</div>'; |
| 102 | return; |
| 103 | } |
| 104 | // A subscribe-driven reload must not wipe out an edit the owner is in |
| 105 | // the middle of; the flag lives on the mount element so it survives |
| 106 | // across these independent mount() calls. |
| 107 | if (mount._dbbasicEditing) return; |
| 108 | const res = await fetch("/collections/" + encodeURIComponent(collection) + "/records/" + encodeURIComponent(recordId), |
| 109 | {credentials: "same-origin", headers: {accept: "application/json"}}); |
| 110 | if (!res.ok) { |
| 111 | mount.innerHTML = res.status === 404 |
| 112 | ? '<div class="state">Not found.</div>' |
| 113 | : '<div class="state denied">Not available.</div>'; |
| 114 | return; |
| 115 | } |
| 116 | const body = await res.json(); |
| 117 | const record = body.record || body; |
| 118 | await renderView(mount, collection, record, opts); |
| 119 | }, |
| 120 | }; |
| 121 | })(); |
| 122 | """ |
| 123 | |
| 124 | |
| 125 | def GET(request): |
| 126 | return {"content_type": "application/javascript; charset=utf-8", "body": _JS} |