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
profile_edit.py · 6.0 KB · 147 lines
1"""Owner edit page for the signed-in user's own profile: /profile/edit ->
2site_profile_edit. Documented route (not seeded), same pattern as every
3other site route in this package -- see objects/site/profile.py's docstring.
4
5schemas/profiles.json declares one profile per user with id == the owning
6account's own id -- deliberately NOT a server-generated record id. The
7shared /form generator (window.dbbasicForm, packages/app-theme/objects/site/
8form.py) always assigns a fresh crypto.randomUUID() to a new record on
9create -- it has no hook to pin the id to a caller-chosen value, and this
10package does not modify app-theme. So the FIRST save (creating the profile)
11is a small hand-rolled form built directly in this page's own script, POSTed
12with an explicit id == the signed-in user's id; every save AFTER that uses
13window.dbbasicForm normally in edit mode (a PUT against the existing
14record's id, which dbbasicForm never touches). This is the same kind of
15small client-side glue app-forum's forum_topic.py uses to lock topic_id on
16its reply form -- reaching for a few lines of page-local JS where the
17generic generator's contract does not fit, rather than special-casing the
18shared generator for one page.
19"""
20
21_SCRIPT = """
22const esc = (s) => String(s ?? "").replace(/[&<>"']/g,
23 (c) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
24const el = (id) => document.getElementById(id);
25
26const CREATE_FIELDS = [
27 {name: "display_name", label: "Display Name", control: "text"},
28 {name: "bio", label: "Bio", control: "textarea"},
29 {name: "skills", label: "Skills", control: "text",
30 help: "Free text, e.g. \\"Python, woodworking, tax prep\\""},
31 {name: "experience", label: "Experience", control: "textarea"},
32 {name: "location", label: "Location", control: "text"},
33 {name: "education", label: "Education", control: "text"},
34 {name: "website", label: "Website", control: "text"},
35 {name: "social_links", label: "Social Links", control: "textarea",
36 help: "One URL per line"},
37 {name: "is_active", label: "Active", control: "checkbox"},
38];
39
40function createFormHtml() {
41 const rows = CREATE_FIELDS.map((f) => {
42 if (f.control === "checkbox") {
43 return '<div class="field"><label class="switch"><input type="checkbox" name="'
44 + f.name + '" checked> ' + esc(f.label) + '</label></div>';
45 }
46 const ctrl = f.control === "textarea"
47 ? '<textarea name="' + f.name + '" rows="3"></textarea>'
48 : '<input type="text" name="' + f.name + '">';
49 return '<div class="field"><label>' + esc(f.label) + '</label>' + ctrl
50 + (f.help ? '<div class="help">' + esc(f.help) + '</div>' : "") + '</div>';
51 }).join("");
52 return '<form class="genform stack" id="createform">' + rows
53 + '<div class="formactions"><button type="submit" class="btn primary">Create Profile</button>'
54 + '<span class="error" id="create-error"></span></div></form>';
55}
56
57async function initCreateForm() {
58 el("formmount").innerHTML = createFormHtml();
59 document.getElementById("createform").addEventListener("submit", async (e) => {
60 e.preventDefault();
61 const rec = {id: VIEWER_ID};
62 for (const f of CREATE_FIELDS) {
63 const input = e.target.elements[f.name];
64 rec[f.name] = f.control === "checkbox" ? (input.checked ? "true" : "false") : input.value;
65 }
66 const res = await fetch("/collections/profiles/records", {
67 method: "POST", credentials: "same-origin",
68 headers: {"content-type": "application/json", accept: "application/json"},
69 body: JSON.stringify(rec),
70 });
71 const body = await res.json();
72 if (!res.ok) { el("create-error").textContent = body.error || "Save failed"; return; }
73 initEditForm(body.record || rec);
74 });
75}
76
77async function initEditForm(record) {
78 el("pagehead-sub").textContent = "Editing your profile";
79 el("viewlink").style.display = "inline";
80 await window.dbbasicForm("profiles", {
81 mount: "#formmount", record: record, owner: VIEWER_ID,
82 onSaved: (saved) => { el("save-note").textContent = "Saved."; setTimeout(() => { el("save-note").textContent = ""; }, 2000); },
83 });
84}
85
86async function load() {
87 const res = await fetch(`/collections/profiles/records/${encodeURIComponent(VIEWER_ID)}`,
88 {credentials: "same-origin", headers: {accept: "application/json"}});
89 if (res.ok) {
90 const body = await res.json();
91 initEditForm(body.record || body);
92 } else {
93 el("pagehead-sub").textContent = "You don't have a profile yet -- create one below.";
94 initCreateForm();
95 }
96}
97load();
98"""
99
100
101def GET(request):
102 identity = request.get("_identity", {})
103 user_id = identity.get("user_id")
104 _logger.info("site_profile_edit served", user_id=user_id or "anonymous")
105
106 if not user_id:
107 html = """<!doctype html>
108<html lang="en">
109<head>
110<meta charset="utf-8">
111<meta name="viewport" content="width=device-width, initial-scale=1">
112<title>Edit Profile</title>
113<link rel="stylesheet" href="/style">
114</head>
115<body>
116<div class="wrap narrow">
117<header class="app"><a href="/">DBBASIC</a></header>
118<p class="hint"><a href="/login?next=/profile/edit">Sign in</a> to edit your profile.</p>
119</div>
120</body>
121</html>"""
122 return {"content_type": "text/html; charset=utf-8", "body": html}
123
124 html = f"""<!doctype html>
125<html lang="en">
126<head>
127<meta charset="utf-8">
128<meta name="viewport" content="width=device-width, initial-scale=1">
129<title>Edit Profile</title>
130<link rel="stylesheet" href="/style">
131</head>
132<body>
133<div class="wrap narrow">
134<header class="app"><a href="/">DBBASIC</a><div class="who">signed in as <strong>{user_id}</strong></div></header>
135<div class="pagehead"><h1>Your Profile</h1></div>
136<p id="pagehead-sub" class="hint">loading&hellip;</p>
137<p><a id="viewlink" style="display:none" href="/u/{user_id}">View your public profile &rarr;</a></p>
138<div id="formmount"></div>
139<span id="save-note" class="hint"></span>
140</div>
141<script>const VIEWER_ID = {user_id!r};</script>
142<script src="/form"></script>
143<script>{_SCRIPT}</script>
144<script src="/nav"></script>
145</body>
146</html>"""
147 return {"content_type": "text/html; charset=utf-8", "body": html}