Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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
3Sibling to /list and /form (same convention: a public static script, no
4server state). `59-detail-related-spec.md`'s detail mode is explicitly NOT
5a second field-renderer -- it reuses /form's schema fetch, field order,
6relation display-field resolution, and type-aware value formatting
7(`window.dbbasicForm.readOnly`, in form.py) in forced-read-only mode. This
8file is thin on purpose: it fetches the one record (the record read is the
9only permission gate, the same posture /list and /form already have) and
10hands it to that shared renderer. A page that mounts a `detail` block
11loads both `<script src="/form">` and `<script src="/detail">` (see
12packages/app-views/objects/site/view_render.py).
13
14Owner-aware edit/delete (Stage 6 extension of 59): when a `detail` block
15declares `editable`/`deletable` AND the viewer owns the record
16(`record[owner_field] === viewer_id`, owner_field defaulting to owner_id),
17the read-only view gains Edit and Delete affordances -- Edit swaps the same
18mount into /form's EXISTING edit pipeline (window.dbbasicForm with a
19record, which PUTs and calls onSaved) and Delete issues the ordinary record
20DELETE then redirects. This is what lets the hand-written per-collection
21`*_view.py` detail pages (each re-implementing fetch + render + owner-edit +
22delete) collapse into one view record + this one renderer: read-only for
23everyone, owner-editable for the owner, no bespoke page code. Non-owners
24and anonymous visitors see exactly the read-only view they saw before --
25the affordances are never shown, and the record write would be
26permission-denied regardless (this only gates the UI, not the gate).
27
28Mounts via window.dbbasicDetail.mount(el, {collection, record_id,
29editable?, 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
125def GET(request):
126 return {"content_type": "application/javascript; charset=utf-8", "body": _JS}