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
share.py · 4.1 KB · 85 lines
1"""Share widget, served at /share as window.dbbasicShare.
2
3What a collection's `capabilities.shareable` flag mounts under a detail page.
4It is owner-only: it asks /api/share for the record's grants and, if the
5viewer isn't the owner (403), renders nothing. For the owner it lists who has
6access and lets them grant another user read/write or revoke. All grant writes
7go through /api/share, which checks the requester owns the target record --
8the widget never writes record_shares directly.
9"""
10
11_JS = r"""
12(function () {
13 const esc = (s) => String(s == null ? "" : s).replace(/[&<>"']/g,
14 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
15 const qs = (m) => typeof m === "string" ? document.querySelector(m) : m;
16
17 async function api(method, path, body) {
18 const res = await fetch(path, {method, credentials: "same-origin",
19 headers: {"content-type": "application/json", accept: "application/json"},
20 body: body === undefined ? undefined : JSON.stringify(body)});
21 let data = null; try { data = await res.json(); } catch (e) {}
22 return [res.status, res.ok, data];
23 }
24
25 window.dbbasicShare = {
26 mount: async function (mountEl, opts) {
27 mountEl = qs(mountEl); opts = opts || {};
28 const pc = opts.parent_collection, pid = opts.parent_id;
29 const viewer = opts.viewer_id
30 || (typeof VIEWER_ID !== "undefined" ? VIEWER_ID : "") || "";
31 if (!mountEl) return;
32 // Sharing is owner-only: without a signed-in viewer, or if the access
33 // read comes back forbidden (not the owner), render nothing at all.
34 if (!pc || !pid || !viewer) { mountEl.innerHTML = ""; return; }
35
36 const base = "/api/share?collection=" + encodeURIComponent(pc) + "&record_id=" + encodeURIComponent(pid);
37
38 async function load() {
39 const [status, ok, data] = await api("GET", base);
40 if (status === 401 || status === 403) { mountEl.innerHTML = ""; return; }
41 if (!ok) { mountEl.innerHTML = ""; return; }
42 const shares = (data && data.shares) || [];
43 const rows = shares.map((s) =>
44 '<div class="sharerow"><div class="sharewho">' + esc(s.user_id) + '</div>'
45 + '<div class="sharemeta">' + esc(s.permission || "read") + '</div>'
46 + '<button class="rowbtn danger" data-revoke="' + esc(s.id) + '" title="Revoke">✕</button></div>'
47 ).join("") || '<div class="state">Not shared with anyone yet.</div>';
48 mountEl.innerHTML =
49 '<div class="share"><h3 class="threadhead">Sharing</h3>'
50 + '<div class="sharelist">' + rows + '</div>'
51 + '<form class="shareadd"><input name="user_id" placeholder="Share with (user id)" autocomplete="off" required>'
52 + '<select name="permission"><option value="read">can view</option><option value="write">can edit</option></select>'
53 + '<button type="submit" class="btn primary">Share</button>'
54 + '<span class="error" data-err></span></form></div>';
55 const form = mountEl.querySelector(".shareadd");
56 form.addEventListener("submit", async (e) => {
57 e.preventDefault();
58 const errEl = form.querySelector("[data-err]"); if (errEl) errEl.textContent = "";
59 const uid = String(form.elements["user_id"].value || "").trim();
60 if (!uid) return;
61 const [st, ok2, data2] = await api("POST", "/api/share",
62 {collection: pc, record_id: pid, user_id: uid, permission: form.elements["permission"].value});
63 if (!ok2) { if (errEl) errEl.textContent = (data2 && data2.error) || "Could not share"; return; }
64 load();
65 });
66 mountEl.querySelector(".sharelist").addEventListener("click", async (e) => {
67 const btn = e.target.closest("[data-revoke]"); if (!btn) return;
68 await api("DELETE", "/api/share/" + encodeURIComponent(btn.getAttribute("data-revoke")));
69 load();
70 });
71 }
72
73 (function sub() {
74 if (window.dbbasicSubscribe) window.dbbasicSubscribe("record_shares", load);
75 else setTimeout(sub, 400);
76 })();
77 load();
78 },
79 };
80})();
81"""
82
83
84def GET(request):
85 return {"content_type": "application/javascript; charset=utf-8", "body": _JS}