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
materialize_run_button.py · 3.0 KB · 78 lines
| 1 | """Run-now button for materialize_definitions' detail page. |
| 2 | |
| 3 | Implements plan/vocabulary/61-materialize-spec.md's Surfaces section: |
| 4 | "a system object, materialize_run, exposed as a slot button |
| 5 | (window.dbbasicSlots, the same injection point 10-flow uses for its |
| 6 | action buttons) on the materialize_definitions detail page: after_fields |
| 7 | (ctx) renders a 'Run now' button." Mirrors packages/app-theme/objects/ |
| 8 | site/slots.py's own serving convention (GET returns a JS body); the |
| 9 | generated form page (packages/app-theme/objects/site/form.py) calls |
| 10 | window.dbbasicSlots.render(collection, "after_fields", ctx) when present |
| 11 | and no-ops when absent, so this registration is purely additive. |
| 12 | |
| 13 | No other package in this repo registers a dbbasicSlots renderer yet, so |
| 14 | whatever mechanism ultimately gets this script's <script> tag onto the |
| 15 | materialize_definitions detail page (the page shell, out of this |
| 16 | package's scope) is unexercised precedent -- the button's own logic |
| 17 | (register, render, POST, show the result) is complete and correct against |
| 18 | the documented window.dbbasicSlots contract regardless. |
| 19 | """ |
| 20 | |
| 21 | |
| 22 | _JS = r""" |
| 23 | (function () { |
| 24 | if (!window.dbbasicSlots) return; |
| 25 | |
| 26 | window.dbbasicSlots.register("materialize_definitions", "after_fields", function (ctx) { |
| 27 | var record = (ctx && ctx.record) || {}; |
| 28 | var id = record.id; |
| 29 | if (!id) return ""; |
| 30 | |
| 31 | var btnId = "materialize-run-now-" + id; |
| 32 | |
| 33 | setTimeout(function () { |
| 34 | var btn = document.getElementById(btnId); |
| 35 | if (!btn || btn._materializeBound) return; |
| 36 | btn._materializeBound = true; |
| 37 | btn.addEventListener("click", function () { |
| 38 | btn.disabled = true; |
| 39 | var out = document.getElementById(btnId + "-result"); |
| 40 | if (out) out.textContent = "Running..."; |
| 41 | |
| 42 | fetch("/objects/system_materialize_run", { |
| 43 | method: "POST", |
| 44 | headers: {"Content-Type": "application/json"}, |
| 45 | body: JSON.stringify({definition_id: id}) |
| 46 | }).then(function (res) { |
| 47 | return res.json(); |
| 48 | }).then(function (data) { |
| 49 | btn.disabled = false; |
| 50 | if (!out) return; |
| 51 | if (data && data.status === "ok") { |
| 52 | var errorCount = (data.errors && data.errors.length) || 0; |
| 53 | out.textContent = "Checked " + data.checked + ", generated " + data.generated + |
| 54 | ", skipped " + data.skipped_already_generated + |
| 55 | (errorCount ? ", " + errorCount + " error(s)" : ""); |
| 56 | } else { |
| 57 | out.textContent = "Error: " + ((data && data.error) || "run failed"); |
| 58 | } |
| 59 | }).catch(function (err) { |
| 60 | btn.disabled = false; |
| 61 | if (out) out.textContent = "Error: " + err; |
| 62 | }); |
| 63 | }); |
| 64 | }, 0); |
| 65 | |
| 66 | return ( |
| 67 | '<div class="materialize-run-now">' + |
| 68 | '<button type="button" id="' + btnId + '">Run now</button>' + |
| 69 | '<span id="' + btnId + '-result" class="materialize-run-now-result"></span>' + |
| 70 | '</div>' |
| 71 | ); |
| 72 | }); |
| 73 | })(); |
| 74 | """ |
| 75 | |
| 76 | |
| 77 | def GET(request): |
| 78 | return {"content_type": "application/javascript; charset=utf-8", "body": _JS} |