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
calendar.py · 3.9 KB · 100 lines
| 1 | """Calendar page over the events collection. |
| 2 | |
| 3 | The collection is named events (matching a private predecessor-system audit, |
| 4 | not part of this repo); this page lives at /calendar because /events is the |
| 5 | server's built-in event-delivery API. The month-grid display is the shared |
| 6 | /list generator in calendar mode (events.views.list_mode == "calendar", spec |
| 7 | 60) -- this page keeps only its custom quick-add form and hands the display to |
| 8 | window.dbbasicList, which reads the schema and renders the month grid. |
| 9 | """ |
| 10 | |
| 11 | # Page-unique layout only (the quick-add form); the calendar grid + everything |
| 12 | # else come from the shared /style sheet. |
| 13 | _STYLE = """ |
| 14 | form.capture { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-md); |
| 15 | padding: var(--pad); display: grid; gap: var(--gap); margin-bottom: var(--gap); |
| 16 | grid-template-columns: 2fr 1fr 1fr; } |
| 17 | form.capture .btn { justify-self: start; } |
| 18 | form.capture .error { grid-column: 1 / -1; } |
| 19 | """ |
| 20 | |
| 21 | _SCRIPT = """ |
| 22 | // The month-grid display is the shared generator in calendar mode; this page |
| 23 | // only owns the quick-add form and asks the list to reload after a save (the |
| 24 | // realtime subscription would refresh it anyway, this just makes it instant). |
| 25 | const cal = window.dbbasicList("events", {mount: "#events-mount"}); |
| 26 | |
| 27 | document.getElementById("capture-form").addEventListener("submit", async (event) => { |
| 28 | event.preventDefault(); |
| 29 | const fields = event.target.elements; |
| 30 | const starts = fields["starts"].value; |
| 31 | const record = {id: crypto.randomUUID(), title: fields["title"].value.trim(), |
| 32 | starts_at: starts ? starts + ":00" : "", |
| 33 | location: fields["location"].value.trim(), |
| 34 | purpose: fields["purpose"].value, owner_id: OWNER_ID}; |
| 35 | const res = await fetch("/collections/events/records", { |
| 36 | method: "POST", credentials: "same-origin", |
| 37 | headers: {"content-type": "application/json", accept: "application/json"}, |
| 38 | body: JSON.stringify(record), |
| 39 | }); |
| 40 | const body = await res.json(); |
| 41 | document.getElementById("form-error").textContent = res.ok ? "" : (body.error || "Save failed"); |
| 42 | if (res.ok) { event.target.reset(); if (cal && cal.reload) cal.reload(); } |
| 43 | }); |
| 44 | """ |
| 45 | |
| 46 | |
| 47 | def GET(request): |
| 48 | identity = request.get("_identity", {}) |
| 49 | user_id = identity.get("user_id") |
| 50 | _logger.info("site_calendar served", user_id=user_id or "anonymous") |
| 51 | |
| 52 | if not user_id: |
| 53 | body = '<p class="hint"><a href="/login?next=/calendar">Sign in</a> to see your events.</p>' |
| 54 | script = "" |
| 55 | else: |
| 56 | body = """ |
| 57 | <form class="capture" id="capture-form"> |
| 58 | <input name="title" placeholder="Event title" required maxlength="255"> |
| 59 | <input name="starts" type="datetime-local" required> |
| 60 | <input name="location" placeholder="Location" maxlength="255"> |
| 61 | <select name="purpose"> |
| 62 | <option value="meeting">meeting</option> |
| 63 | <option value="workshop">workshop</option> |
| 64 | <option value="recreation">recreation</option> |
| 65 | <option value="others">others</option> |
| 66 | </select> |
| 67 | <button type="submit" class="btn primary">Add Event</button> |
| 68 | <div class="error" id="form-error"></div> |
| 69 | </form> |
| 70 | <div id="events-mount"><div class="state">loading…</div></div> |
| 71 | """ |
| 72 | script = ( |
| 73 | '<script src="/list"></script>' |
| 74 | f"<script>const OWNER_ID = {user_id!r};{_SCRIPT}</script>" |
| 75 | ) |
| 76 | |
| 77 | who = ( |
| 78 | f"signed in as <strong>{user_id}</strong>" |
| 79 | if user_id |
| 80 | else '<a href="/login?next=/calendar">sign in</a>' |
| 81 | ) |
| 82 | html = f"""<!doctype html> |
| 83 | <html lang="en"> |
| 84 | <head> |
| 85 | <meta charset="utf-8"> |
| 86 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 87 | <title>Calendar</title> |
| 88 | <link rel="stylesheet" href="/style"> |
| 89 | <style>{_STYLE}</style> |
| 90 | </head> |
| 91 | <body> |
| 92 | <div class="wrap"> |
| 93 | <header class="app"><h1>Calendar</h1><div class="who">{who}</div></header> |
| 94 | {body} |
| 95 | </div> |
| 96 | {script} |
| 97 | <script src="/nav"></script> |
| 98 | </body> |
| 99 | </html>""" |
| 100 | return {"content_type": "text/html; charset=utf-8", "body": html} |