fastapi-panel 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_panel/__init__.py +77 -0
- fastapi_panel/__main__.py +97 -0
- fastapi_panel/auth.py +189 -0
- fastapi_panel/db.py +41 -0
- fastapi_panel/filters.py +215 -0
- fastapi_panel/forms.py +733 -0
- fastapi_panel/imports.py +264 -0
- fastapi_panel/introspect.py +111 -0
- fastapi_panel/log.py +52 -0
- fastapi_panel/options.py +717 -0
- fastapi_panel/py.typed +0 -0
- fastapi_panel/security.py +139 -0
- fastapi_panel/site.py +602 -0
- fastapi_panel/static/panel.css +420 -0
- fastapi_panel/static/panel.js +404 -0
- fastapi_panel/static/theme.js +5 -0
- fastapi_panel/storage.py +38 -0
- fastapi_panel/templates/fastapi_panel/base.html +76 -0
- fastapi_panel/templates/fastapi_panel/delete.html +25 -0
- fastapi_panel/templates/fastapi_panel/error.html +10 -0
- fastapi_panel/templates/fastapi_panel/form.html +113 -0
- fastapi_panel/templates/fastapi_panel/history.html +23 -0
- fastapi_panel/templates/fastapi_panel/import.html +76 -0
- fastapi_panel/templates/fastapi_panel/index.html +43 -0
- fastapi_panel/templates/fastapi_panel/list.html +131 -0
- fastapi_panel/templates/fastapi_panel/login.html +15 -0
- fastapi_panel/templates/fastapi_panel/password.html +17 -0
- fastapi_panel/templates/fastapi_panel/popup_done.html +4 -0
- fastapi_panel/templates/fastapi_panel/widgets.html +74 -0
- fastapi_panel/views.py +710 -0
- fastapi_panel-0.1.0.dist-info/METADATA +151 -0
- fastapi_panel-0.1.0.dist-info/RECORD +34 -0
- fastapi_panel-0.1.0.dist-info/WHEEL +4 -0
- fastapi_panel-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const $ = (selector, root = document) => root.querySelector(selector);
|
|
4
|
+
const $$ = (selector, root = document) => [...root.querySelectorAll(selector)];
|
|
5
|
+
|
|
6
|
+
function loadCollapsedGroups() {
|
|
7
|
+
try {
|
|
8
|
+
return new Set(JSON.parse(localStorage.getItem("fastapi-panel-nav-collapsed")));
|
|
9
|
+
} catch {
|
|
10
|
+
return new Set();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function saveCollapsedGroups(collapsed) {
|
|
15
|
+
try {
|
|
16
|
+
localStorage.setItem("fastapi-panel-nav-collapsed", JSON.stringify([...collapsed]));
|
|
17
|
+
} catch {}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function setupChrome() {
|
|
21
|
+
$("[data-toggle-theme]")?.addEventListener("click", () => {
|
|
22
|
+
const root = document.documentElement;
|
|
23
|
+
const isDark = root.dataset.theme
|
|
24
|
+
? root.dataset.theme === "dark"
|
|
25
|
+
: matchMedia("(prefers-color-scheme: dark)").matches;
|
|
26
|
+
root.dataset.theme = isDark ? "light" : "dark";
|
|
27
|
+
try {
|
|
28
|
+
localStorage.setItem("fastapi-panel-theme", root.dataset.theme);
|
|
29
|
+
} catch {}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
$("[data-toggle-sidebar]")?.addEventListener("click", () => {
|
|
33
|
+
$("#sidebar").classList.toggle("open");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const groups = $$("[data-nav-group]");
|
|
37
|
+
const collapsed = loadCollapsedGroups();
|
|
38
|
+
const restoreGroup = (group) => {
|
|
39
|
+
group.open = !collapsed.has(group.dataset.navGroup) || $(".nav-item.active", group) !== null;
|
|
40
|
+
};
|
|
41
|
+
groups.forEach((group) => {
|
|
42
|
+
restoreGroup(group);
|
|
43
|
+
$("summary", group).addEventListener("click", () => {
|
|
44
|
+
// The click fires before the group toggles, so it's still in its old state here.
|
|
45
|
+
if (group.open) collapsed.add(group.dataset.navGroup);
|
|
46
|
+
else collapsed.delete(group.dataset.navGroup);
|
|
47
|
+
saveCollapsedGroups(collapsed);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
$("[data-nav-filter]")?.addEventListener("input", (event) => {
|
|
52
|
+
const term = event.target.value.toLowerCase();
|
|
53
|
+
groups.forEach((group) => {
|
|
54
|
+
const items = $$("[data-nav-item]", group);
|
|
55
|
+
items.forEach((item) => {
|
|
56
|
+
item.hidden = !$("a", item).textContent.toLowerCase().includes(term);
|
|
57
|
+
});
|
|
58
|
+
group.hidden = items.every((item) => item.hidden);
|
|
59
|
+
if (term) group.open = true;
|
|
60
|
+
else restoreGroup(group);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function setupChangelist() {
|
|
66
|
+
// On small screens the filters sit above the table, so only keep the ones in use open.
|
|
67
|
+
if (matchMedia("(max-width: 1000px)").matches) {
|
|
68
|
+
$$(".filter:not([data-active])").forEach((filter) => (filter.open = false));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const form = $("[data-changelist]");
|
|
72
|
+
if (!form) return;
|
|
73
|
+
|
|
74
|
+
const rows = $$("[data-row-check]", form);
|
|
75
|
+
const all = $("[data-select-all]", form);
|
|
76
|
+
const counter = $("[data-counter]", form);
|
|
77
|
+
const acrossButton = $("[data-select-across]", form);
|
|
78
|
+
const acrossInput = $("[data-across-input]", form);
|
|
79
|
+
|
|
80
|
+
const refresh = () => {
|
|
81
|
+
const count = rows.filter((row) => row.checked).length;
|
|
82
|
+
const allOnPage = rows.length > 0 && count === rows.length;
|
|
83
|
+
if (all) all.checked = allOnPage;
|
|
84
|
+
if (counter) counter.textContent = `${count} of ${rows.length} selected`;
|
|
85
|
+
if (acrossButton) acrossButton.hidden = !allOnPage;
|
|
86
|
+
if (acrossInput && !allOnPage) acrossInput.value = "";
|
|
87
|
+
rows.forEach((row) => row.closest("tr").classList.toggle("selected", row.checked));
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
rows.forEach((row) => row.addEventListener("change", refresh));
|
|
91
|
+
all?.addEventListener("change", () => {
|
|
92
|
+
rows.forEach((row) => (row.checked = all.checked));
|
|
93
|
+
refresh();
|
|
94
|
+
});
|
|
95
|
+
acrossButton?.addEventListener("click", () => {
|
|
96
|
+
acrossInput.value = "1";
|
|
97
|
+
counter.textContent = `All ${acrossButton.textContent.replace("Select all ", "")} selected`;
|
|
98
|
+
acrossButton.hidden = true;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
form.addEventListener("submit", (event) => {
|
|
102
|
+
if (event.submitter?.name !== "_run_action") return;
|
|
103
|
+
const option = $("[data-action-select]", form).selectedOptions[0];
|
|
104
|
+
if (option?.dataset.confirm && !confirm(option.dataset.confirm)) event.preventDefault();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function setupPrepopulate(root = document) {
|
|
109
|
+
const slugify = (text) =>
|
|
110
|
+
text
|
|
111
|
+
.normalize("NFKD")
|
|
112
|
+
.replace(/[̀-ͯ]/g, "")
|
|
113
|
+
.toLowerCase()
|
|
114
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
115
|
+
.replace(/^-+|-+$/g, "");
|
|
116
|
+
|
|
117
|
+
$$("[data-prepopulate]", root).forEach((target) => {
|
|
118
|
+
const sources = target.dataset.prepopulate.split(",").map((name) => document.getElementsByName(name)[0]);
|
|
119
|
+
let touched = target.value !== "";
|
|
120
|
+
target.addEventListener("input", () => (touched = target.value !== ""));
|
|
121
|
+
sources.forEach((source) =>
|
|
122
|
+
source?.addEventListener("input", () => {
|
|
123
|
+
if (!touched) target.value = slugify(sources.map((s) => s?.value ?? "").join(" "));
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function setupAutocomplete(root = document) {
|
|
130
|
+
$$("[data-autocomplete]", root).forEach((box) => {
|
|
131
|
+
const search = $("[data-search]", box);
|
|
132
|
+
const results = $("[data-results]", box);
|
|
133
|
+
const chips = $("[data-chips]", box);
|
|
134
|
+
const multiple = "multiple" in box.dataset;
|
|
135
|
+
let timer;
|
|
136
|
+
|
|
137
|
+
const addChip = (id, text) => {
|
|
138
|
+
if (!multiple) chips.replaceChildren();
|
|
139
|
+
if ($$("input", chips).some((input) => input.value === id)) return;
|
|
140
|
+
const chip = document.createElement("span");
|
|
141
|
+
chip.className = "chip";
|
|
142
|
+
chip.dataset.chip = "";
|
|
143
|
+
const input = document.createElement("input");
|
|
144
|
+
input.type = "hidden";
|
|
145
|
+
input.name = box.dataset.name;
|
|
146
|
+
input.value = id;
|
|
147
|
+
const remove = document.createElement("button");
|
|
148
|
+
remove.type = "button";
|
|
149
|
+
remove.dataset.remove = "";
|
|
150
|
+
remove.setAttribute("aria-label", `Remove ${text}`);
|
|
151
|
+
remove.textContent = "×";
|
|
152
|
+
chip.append(input, text, remove);
|
|
153
|
+
chips.append(chip);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
box.addEventListener("panel:choose", (event) => addChip(event.detail.id, event.detail.label));
|
|
157
|
+
|
|
158
|
+
chips.addEventListener("click", (event) => {
|
|
159
|
+
if (event.target.matches("[data-remove]")) event.target.closest("[data-chip]").remove();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const load = async () => {
|
|
163
|
+
const url = `${box.dataset.autocomplete}?q=${encodeURIComponent(search.value)}`;
|
|
164
|
+
const response = await fetch(url, { credentials: "same-origin" });
|
|
165
|
+
if (!response.ok) return;
|
|
166
|
+
const data = await response.json();
|
|
167
|
+
results.replaceChildren(
|
|
168
|
+
...data.results.map((item) => {
|
|
169
|
+
const li = document.createElement("li");
|
|
170
|
+
const button = document.createElement("button");
|
|
171
|
+
button.type = "button";
|
|
172
|
+
button.textContent = item.text;
|
|
173
|
+
button.addEventListener("click", () => {
|
|
174
|
+
addChip(item.id, item.text);
|
|
175
|
+
search.value = "";
|
|
176
|
+
results.hidden = true;
|
|
177
|
+
});
|
|
178
|
+
li.append(button);
|
|
179
|
+
return li;
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
182
|
+
results.hidden = data.results.length === 0;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
search.addEventListener("focus", load);
|
|
186
|
+
search.addEventListener("input", () => {
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
timer = setTimeout(load, 200);
|
|
189
|
+
});
|
|
190
|
+
search.addEventListener("keydown", (event) => {
|
|
191
|
+
if (event.key === "Enter") event.preventDefault();
|
|
192
|
+
if (event.key === "Escape") results.hidden = true;
|
|
193
|
+
});
|
|
194
|
+
document.addEventListener("click", (event) => {
|
|
195
|
+
if (!box.contains(event.target)) results.hidden = true;
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function setupDualSelects(root = document) {
|
|
201
|
+
$$("select[data-dual]", root).forEach((select) => {
|
|
202
|
+
select.hidden = true;
|
|
203
|
+
const wrapper = document.createElement("div");
|
|
204
|
+
wrapper.className = "dual";
|
|
205
|
+
|
|
206
|
+
const makeColumn = (title) => {
|
|
207
|
+
const column = document.createElement("div");
|
|
208
|
+
column.className = "dual-column";
|
|
209
|
+
const heading = document.createElement("strong");
|
|
210
|
+
heading.textContent = title;
|
|
211
|
+
const list = document.createElement("select");
|
|
212
|
+
list.multiple = true;
|
|
213
|
+
list.size = 8;
|
|
214
|
+
list.setAttribute("aria-label", title);
|
|
215
|
+
column.append(heading, list);
|
|
216
|
+
return { column, list };
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const left = makeColumn("Available");
|
|
220
|
+
const right = makeColumn("Chosen");
|
|
221
|
+
const filter = document.createElement("input");
|
|
222
|
+
filter.type = "search";
|
|
223
|
+
filter.placeholder = "Filter";
|
|
224
|
+
filter.setAttribute("aria-label", "Filter available options");
|
|
225
|
+
left.column.insertBefore(filter, left.list);
|
|
226
|
+
|
|
227
|
+
const render = () => {
|
|
228
|
+
const term = filter.value.toLowerCase();
|
|
229
|
+
left.list.replaceChildren();
|
|
230
|
+
right.list.replaceChildren();
|
|
231
|
+
[...select.options].forEach((option) => {
|
|
232
|
+
const copy = new Option(option.text, option.value);
|
|
233
|
+
if (option.selected) right.list.add(copy);
|
|
234
|
+
else if (option.text.toLowerCase().includes(term)) left.list.add(copy);
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const move = (list, selected) => {
|
|
239
|
+
const values = new Set([...list.selectedOptions].map((option) => option.value));
|
|
240
|
+
[...select.options].forEach((option) => {
|
|
241
|
+
if (values.has(option.value)) option.selected = selected;
|
|
242
|
+
});
|
|
243
|
+
render();
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const buttons = document.createElement("div");
|
|
247
|
+
buttons.className = "dual-buttons";
|
|
248
|
+
[
|
|
249
|
+
["→", "Add selected", () => move(left.list, true)],
|
|
250
|
+
["←", "Remove selected", () => move(right.list, false)],
|
|
251
|
+
].forEach(([text, label, handler]) => {
|
|
252
|
+
const button = document.createElement("button");
|
|
253
|
+
button.type = "button";
|
|
254
|
+
button.className = "button";
|
|
255
|
+
button.textContent = text;
|
|
256
|
+
button.setAttribute("aria-label", label);
|
|
257
|
+
button.addEventListener("click", handler);
|
|
258
|
+
buttons.append(button);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
filter.addEventListener("input", render);
|
|
262
|
+
select.addEventListener("change", render);
|
|
263
|
+
filter.addEventListener("keydown", (event) => {
|
|
264
|
+
if (event.key === "Enter") event.preventDefault();
|
|
265
|
+
});
|
|
266
|
+
left.list.addEventListener("dblclick", () => move(left.list, true));
|
|
267
|
+
right.list.addEventListener("dblclick", () => move(right.list, false));
|
|
268
|
+
|
|
269
|
+
wrapper.append(left.column, buttons, right.column);
|
|
270
|
+
select.after(wrapper);
|
|
271
|
+
render();
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function setupFileFields(root = document) {
|
|
276
|
+
$$("[data-file-field]", root).forEach((box) => {
|
|
277
|
+
const input = $("input[type=file]", box);
|
|
278
|
+
const preview = $("[data-file-preview]", box);
|
|
279
|
+
const error = $("[data-file-error]", box);
|
|
280
|
+
input.addEventListener("change", () => {
|
|
281
|
+
const file = input.files[0];
|
|
282
|
+
const tooLarge = file && file.size > Number(input.dataset.maxSize);
|
|
283
|
+
error.hidden = !tooLarge;
|
|
284
|
+
error.textContent = tooLarge ? "This file is too large." : "";
|
|
285
|
+
if (tooLarge) input.value = "";
|
|
286
|
+
if (preview.src) URL.revokeObjectURL(preview.src);
|
|
287
|
+
const showPreview = file && !tooLarge && file.type.startsWith("image/") && file.type !== "image/svg+xml";
|
|
288
|
+
preview.hidden = !showPreview;
|
|
289
|
+
if (showPreview) preview.src = URL.createObjectURL(file);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function chooseCreated(button, id, label) {
|
|
295
|
+
const box = button.closest("[data-related]");
|
|
296
|
+
const search = $("[data-autocomplete]", box);
|
|
297
|
+
if (search) {
|
|
298
|
+
search.dispatchEvent(new CustomEvent("panel:choose", { detail: { id, label } }));
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const url = button.dataset.relatedAdd;
|
|
302
|
+
// Every dropdown for this model gets the new option, such as the other rows of an inline.
|
|
303
|
+
$$("[data-related-add]")
|
|
304
|
+
.filter((other) => other.dataset.relatedAdd === url)
|
|
305
|
+
.forEach((other) => {
|
|
306
|
+
const select = $("select", other.closest("[data-related]"));
|
|
307
|
+
if (select && ![...select.options].some((option) => option.value === id)) select.add(new Option(label, id));
|
|
308
|
+
});
|
|
309
|
+
const select = $("select", box);
|
|
310
|
+
if (select) {
|
|
311
|
+
[...select.options].find((option) => option.value === id).selected = true;
|
|
312
|
+
select.dispatchEvent(new Event("change"));
|
|
313
|
+
} else {
|
|
314
|
+
$("input", box).value = id;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function setupRelatedAdd() {
|
|
319
|
+
const result = $("[data-popup-result]");
|
|
320
|
+
if (result) {
|
|
321
|
+
const { id, label } = result.dataset;
|
|
322
|
+
window.parent.postMessage({ type: "panel-created", id, label }, location.origin);
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
$("[data-popup-cancel]")?.addEventListener("click", () => {
|
|
326
|
+
window.parent.postMessage({ type: "panel-cancel" }, location.origin);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
let open = null;
|
|
330
|
+
|
|
331
|
+
document.addEventListener("click", (event) => {
|
|
332
|
+
const button = event.target.closest("[data-related-add]");
|
|
333
|
+
if (!button) return;
|
|
334
|
+
const dialog = document.createElement("dialog");
|
|
335
|
+
dialog.className = "modal";
|
|
336
|
+
const head = document.createElement("div");
|
|
337
|
+
head.className = "modal-head";
|
|
338
|
+
const title = document.createElement("strong");
|
|
339
|
+
title.textContent = button.dataset.relatedTitle;
|
|
340
|
+
const close = document.createElement("button");
|
|
341
|
+
close.type = "button";
|
|
342
|
+
close.className = "link-button";
|
|
343
|
+
close.textContent = "×";
|
|
344
|
+
close.setAttribute("aria-label", "Close");
|
|
345
|
+
close.addEventListener("click", () => dialog.close());
|
|
346
|
+
head.append(title, close);
|
|
347
|
+
const frame = document.createElement("iframe");
|
|
348
|
+
frame.title = button.dataset.relatedTitle;
|
|
349
|
+
frame.src = button.dataset.relatedAdd;
|
|
350
|
+
dialog.append(head, frame);
|
|
351
|
+
dialog.addEventListener("close", () => {
|
|
352
|
+
dialog.remove();
|
|
353
|
+
open = null;
|
|
354
|
+
});
|
|
355
|
+
document.body.append(dialog);
|
|
356
|
+
dialog.showModal();
|
|
357
|
+
open = { dialog, frame, button };
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
window.addEventListener("message", (event) => {
|
|
361
|
+
if (!open || event.origin !== location.origin || event.source !== open.frame.contentWindow) return;
|
|
362
|
+
if (event.data?.type === "panel-created") chooseCreated(open.button, event.data.id, event.data.label);
|
|
363
|
+
if (event.data?.type === "panel-created" || event.data?.type === "panel-cancel") open.dialog.close();
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function setupInlines() {
|
|
368
|
+
$$("[data-inline]").forEach((inline) => {
|
|
369
|
+
const rows = $("[data-inline-rows]", inline);
|
|
370
|
+
const total = $("[data-inline-total]", inline);
|
|
371
|
+
const blank = $("[data-inline-blank]", inline);
|
|
372
|
+
let next = Number(inline.dataset.nextIndex);
|
|
373
|
+
|
|
374
|
+
$("[data-inline-add]", inline)?.addEventListener("click", () => {
|
|
375
|
+
const holder = document.createElement("div");
|
|
376
|
+
holder.innerHTML = blank.innerHTML.replaceAll("__prefix__", String(next));
|
|
377
|
+
const row = $("[data-inline-template]", holder);
|
|
378
|
+
row.removeAttribute("data-inline-template");
|
|
379
|
+
row.setAttribute("data-inline-row", "");
|
|
380
|
+
rows.append(row);
|
|
381
|
+
next += 1;
|
|
382
|
+
total.value = String(next);
|
|
383
|
+
setupPrepopulate(row);
|
|
384
|
+
setupAutocomplete(row);
|
|
385
|
+
setupDualSelects(row);
|
|
386
|
+
setupFileFields(row);
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
// Removed rows leave a gap in the numbering. The server skips blanks, so that's fine.
|
|
390
|
+
inline.addEventListener("click", (event) => {
|
|
391
|
+
const remove = event.target.closest("[data-inline-remove]");
|
|
392
|
+
if (remove) remove.closest("[data-inline-row]").remove();
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
setupChrome();
|
|
398
|
+
setupChangelist();
|
|
399
|
+
setupPrepopulate();
|
|
400
|
+
setupAutocomplete();
|
|
401
|
+
setupDualSelects();
|
|
402
|
+
setupFileFields();
|
|
403
|
+
setupInlines();
|
|
404
|
+
setupRelatedAdd();
|
fastapi_panel/storage.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import shutil
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any, BinaryIO
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Storage:
|
|
9
|
+
"""Where uploads go. Subclass it to use S3 or anything else. Files are always served through the panel."""
|
|
10
|
+
|
|
11
|
+
def save(self, file: Any, name: str) -> None:
|
|
12
|
+
raise NotImplementedError
|
|
13
|
+
|
|
14
|
+
def open(self, name: str) -> BinaryIO:
|
|
15
|
+
"""Raises FileNotFoundError when there's no such file."""
|
|
16
|
+
raise NotImplementedError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class LocalStorage(Storage):
|
|
20
|
+
"""Keeps files in a folder. Put it outside anything your web server serves, or uploads become public."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, root: str | Path) -> None:
|
|
23
|
+
self.root = Path(root).resolve()
|
|
24
|
+
|
|
25
|
+
def _path(self, name: str) -> Path:
|
|
26
|
+
path = (self.root / name).resolve()
|
|
27
|
+
if not path.is_relative_to(self.root) or path == self.root:
|
|
28
|
+
raise ValueError(f"{name!r} is outside the storage folder")
|
|
29
|
+
return path
|
|
30
|
+
|
|
31
|
+
def save(self, file: Any, name: str) -> None:
|
|
32
|
+
path = self._path(name)
|
|
33
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
34
|
+
with path.open("xb") as target:
|
|
35
|
+
shutil.copyfileobj(file, target)
|
|
36
|
+
|
|
37
|
+
def open(self, name: str) -> BinaryIO:
|
|
38
|
+
return self._path(name).open("rb")
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="robots" content="noindex, nofollow">
|
|
7
|
+
<link rel="icon" href="data:,">
|
|
8
|
+
<title>{{ title }} | {{ site.title }}</title>
|
|
9
|
+
<script src="{{ static }}/theme.js?v={{ version }}"></script>
|
|
10
|
+
<link rel="stylesheet" href="{{ static }}/panel.css?v={{ version }}">
|
|
11
|
+
{% for href in site.extra_css %}<link rel="stylesheet" href="{{ href }}">{% endfor %}
|
|
12
|
+
<script src="{{ static }}/panel.js?v={{ version }}" defer></script>
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
{% block body %}
|
|
16
|
+
<div class="layout">
|
|
17
|
+
{% if not popup %}
|
|
18
|
+
<aside class="sidebar" id="sidebar">
|
|
19
|
+
<div class="sidebar-inner">
|
|
20
|
+
<a class="brand" href="{{ root }}/"><span class="brand-mark" aria-hidden="true">{{ site.header[:1] }}</span>{{ site.header }}</a>
|
|
21
|
+
<input class="nav-search" type="search" placeholder="Find a model" aria-label="Find a model" data-nav-filter>
|
|
22
|
+
<nav aria-label="Models">
|
|
23
|
+
{% for group in nav %}
|
|
24
|
+
<details class="nav-group" data-nav-group="{{ group.name }}" open>
|
|
25
|
+
<summary class="nav-title">{{ group.name }}</summary>
|
|
26
|
+
<ul>
|
|
27
|
+
{% for item in group["items"] %}
|
|
28
|
+
<li class="nav-item{% if item.active %} active{% endif %}" data-nav-item>
|
|
29
|
+
<a href="{{ item.url }}"{% if item.active %} aria-current="page"{% endif %}>{{ item.label }}</a>
|
|
30
|
+
{% if item.add_url %}<a class="nav-add" href="{{ item.add_url }}" aria-label="Add to {{ item.label }}">+ Add</a>{% endif %}
|
|
31
|
+
</li>
|
|
32
|
+
{% endfor %}
|
|
33
|
+
</ul>
|
|
34
|
+
</details>
|
|
35
|
+
{% endfor %}
|
|
36
|
+
</nav>
|
|
37
|
+
</div>
|
|
38
|
+
</aside>
|
|
39
|
+
{% endif %}
|
|
40
|
+
|
|
41
|
+
<div class="main">
|
|
42
|
+
{% if not popup %}
|
|
43
|
+
<header class="topbar">
|
|
44
|
+
<button class="icon-button menu-button" type="button" data-toggle-sidebar aria-controls="sidebar" aria-label="Toggle menu">☰</button>
|
|
45
|
+
{% if crumbs %}
|
|
46
|
+
<nav class="crumbs" aria-label="Breadcrumb">
|
|
47
|
+
{% for label, url in crumbs %}
|
|
48
|
+
{% if url %}<a href="{{ url }}">{{ label }}</a><span aria-hidden="true">/</span>{% else %}<span aria-current="page">{{ label }}</span>{% endif %}
|
|
49
|
+
{% endfor %}
|
|
50
|
+
</nav>
|
|
51
|
+
{% else %}<span class="crumbs"></span>{% endif %}
|
|
52
|
+
<div class="topbar-end">
|
|
53
|
+
<button class="icon-button" type="button" data-toggle-theme aria-label="Switch color theme" title="Switch color theme">◐</button>
|
|
54
|
+
{% if user is not none %}
|
|
55
|
+
<span class="user-name">{{ user_label }}</span>
|
|
56
|
+
{% if can_change_password %}<a class="top-link" href="{{ root }}/password">Password</a>{% endif %}
|
|
57
|
+
<form method="post" action="{{ root }}/logout">
|
|
58
|
+
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
|
59
|
+
<button class="top-link" type="submit">Log out</button>
|
|
60
|
+
</form>
|
|
61
|
+
{% endif %}
|
|
62
|
+
</div>
|
|
63
|
+
</header>
|
|
64
|
+
{% endif %}
|
|
65
|
+
|
|
66
|
+
<main class="content">
|
|
67
|
+
{% for message in messages %}
|
|
68
|
+
<div class="message message-{{ message.level }}" role="status">{{ message.text }}</div>
|
|
69
|
+
{% endfor %}
|
|
70
|
+
{% block content %}{% endblock %}
|
|
71
|
+
</main>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
{% endblock %}
|
|
75
|
+
</body>
|
|
76
|
+
</html>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{% extends "fastapi_panel/base.html" %}
|
|
2
|
+
{% block content %}
|
|
3
|
+
<h1 class="page-title">{{ title }}</h1>
|
|
4
|
+
<div class="card card-padded">
|
|
5
|
+
<p>{% if soft %}You can restore these later from Deleted. These will be deleted:{% else %}This can't be undone. These will be deleted:{% endif %}</p>
|
|
6
|
+
<ul class="delete-list">
|
|
7
|
+
{% for label in labels %}<li>{{ label }}</li>{% endfor %}
|
|
8
|
+
{% if extra_count %}<li class="muted">and {{ extra_count }} more</li>{% endif %}
|
|
9
|
+
</ul>
|
|
10
|
+
{% if related %}
|
|
11
|
+
<p>Along with their related records:</p>
|
|
12
|
+
<ul class="delete-list">
|
|
13
|
+
{% for entry in related %}
|
|
14
|
+
<li><strong>{{ entry.name }}</strong> ({{ entry.count }}): {{ entry["items"]|join(", ") }}{% if entry.count > entry["items"]|length %}, …{% endif %}</li>
|
|
15
|
+
{% endfor %}
|
|
16
|
+
</ul>
|
|
17
|
+
{% endif %}
|
|
18
|
+
<form method="post" action="{{ post_url }}" class="submit-row">
|
|
19
|
+
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
|
20
|
+
{% for name, value in hidden %}<input type="hidden" name="{{ name }}" value="{{ value }}">{% endfor %}
|
|
21
|
+
<button class="button button-danger" type="submit">Yes, delete</button>
|
|
22
|
+
<a class="button" href="{{ cancel_url }}">Cancel</a>
|
|
23
|
+
</form>
|
|
24
|
+
</div>
|
|
25
|
+
{% endblock %}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
{% extends "fastapi_panel/base.html" %}
|
|
2
|
+
{% from "fastapi_panel/widgets.html" import widget, errors, form_row %}
|
|
3
|
+
|
|
4
|
+
{% macro submit_row(top=False) %}
|
|
5
|
+
{% if can_save %}
|
|
6
|
+
<div class="submit-row{% if top %} submit-row-top{% endif %}">
|
|
7
|
+
<button class="button button-primary" type="submit" name="_save" value="1">Save</button>
|
|
8
|
+
{% if popup %}
|
|
9
|
+
<button class="button" type="button" data-popup-cancel>Cancel</button>
|
|
10
|
+
{% else %}
|
|
11
|
+
<button class="button" type="submit" name="_continue" value="1">Save and continue editing</button>
|
|
12
|
+
{% if can_add %}<button class="button" type="submit" name="_addanother" value="1">Save and add another</button>{% endif %}
|
|
13
|
+
{% if save_as %}<button class="button" type="submit" name="_saveasnew" value="1">Save as new</button>{% endif %}
|
|
14
|
+
{% if can_delete %}<a class="button button-danger push-end" href="{{ delete_url }}">Delete</a>{% endif %}
|
|
15
|
+
{% endif %}
|
|
16
|
+
</div>
|
|
17
|
+
{% endif %}
|
|
18
|
+
{% endmacro %}
|
|
19
|
+
|
|
20
|
+
{% macro inline_hidden(row) %}
|
|
21
|
+
<input type="hidden" name="{{ row.prefix }}_pk" value="{{ row.pk }}">
|
|
22
|
+
{% endmacro %}
|
|
23
|
+
|
|
24
|
+
{% macro tabular_row(formset, row, template=False) %}
|
|
25
|
+
<tr {% if template %}data-inline-template{% else %}data-inline-row{% endif %}>
|
|
26
|
+
{% for field in row.form.fields %}
|
|
27
|
+
<td>{{ widget(field) }}{{ errors(field) }}</td>
|
|
28
|
+
{% endfor %}
|
|
29
|
+
<td class="col-delete">
|
|
30
|
+
{{ inline_hidden(row) }}
|
|
31
|
+
{% if row.persisted and formset.can_delete %}
|
|
32
|
+
<label class="delete-toggle"><input type="checkbox" name="{{ row.prefix }}DELETE"{% if row.delete %} checked{% endif %}> Delete</label>
|
|
33
|
+
{% elif not row.persisted and not formset.view_only %}
|
|
34
|
+
<button class="link-button" type="button" data-inline-remove aria-label="Remove row">×</button>
|
|
35
|
+
{% endif %}
|
|
36
|
+
</td>
|
|
37
|
+
</tr>
|
|
38
|
+
{% endmacro %}
|
|
39
|
+
|
|
40
|
+
{% macro stacked_row(formset, row, template=False) %}
|
|
41
|
+
<div class="stacked-row" {% if template %}data-inline-template{% else %}data-inline-row{% endif %}>
|
|
42
|
+
<div class="stacked-head">
|
|
43
|
+
<strong>{{ row.label or ("New " ~ formset.inline.name.lower()) }}</strong>
|
|
44
|
+
{% if row.persisted and formset.can_delete %}
|
|
45
|
+
<label class="delete-toggle"><input type="checkbox" name="{{ row.prefix }}DELETE"{% if row.delete %} checked{% endif %}> Delete</label>
|
|
46
|
+
{% elif not row.persisted and not formset.view_only %}
|
|
47
|
+
<button class="link-button" type="button" data-inline-remove>Remove</button>
|
|
48
|
+
{% endif %}
|
|
49
|
+
</div>
|
|
50
|
+
{{ inline_hidden(row) }}
|
|
51
|
+
{% for field in row.form.fields %}{{ form_row(field) }}{% endfor %}
|
|
52
|
+
</div>
|
|
53
|
+
{% endmacro %}
|
|
54
|
+
|
|
55
|
+
{% block content %}
|
|
56
|
+
{% if not popup %}
|
|
57
|
+
<div class="page-head">
|
|
58
|
+
<h1 class="page-title">{{ title }}</h1>
|
|
59
|
+
<div class="page-links">
|
|
60
|
+
{% if history_url %}<a class="button" href="{{ history_url }}">History</a>{% endif %}
|
|
61
|
+
{% if view_on_site %}<a class="button" href="{{ view_on_site }}" rel="noopener noreferrer" target="_blank">View on site</a>{% endif %}
|
|
62
|
+
</div>
|
|
63
|
+
</div>
|
|
64
|
+
{% endif %}
|
|
65
|
+
|
|
66
|
+
<form method="post" action="{{ post_url }}" enctype="multipart/form-data" novalidate data-change-form>
|
|
67
|
+
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
|
|
68
|
+
{% if save_on_top %}{{ submit_row(top=True) }}{% endif %}
|
|
69
|
+
|
|
70
|
+
{% for error in form.errors %}<div class="message message-error" role="alert">{{ error }}</div>{% endfor %}
|
|
71
|
+
|
|
72
|
+
{% for fieldset in form.fieldsets %}
|
|
73
|
+
{% if fieldset.collapsed %}<details class="card fieldset"><summary class="card-title">{{ fieldset.title or "More" }}</summary>
|
|
74
|
+
{% else %}<fieldset class="card fieldset">{% if fieldset.title %}<legend class="card-title">{{ fieldset.title }}</legend>{% endif %}{% endif %}
|
|
75
|
+
{% if fieldset.description %}<p class="help fieldset-description">{{ fieldset.description }}</p>{% endif %}
|
|
76
|
+
{% for row in fieldset.rows %}
|
|
77
|
+
<div class="form-row{% if row|length > 1 %} form-row-split{% endif %}">
|
|
78
|
+
{% for field in row %}{{ form_row(field) }}{% endfor %}
|
|
79
|
+
</div>
|
|
80
|
+
{% endfor %}
|
|
81
|
+
{% if fieldset.collapsed %}</details>{% else %}</fieldset>{% endif %}
|
|
82
|
+
{% endfor %}
|
|
83
|
+
|
|
84
|
+
{% for formset in formsets %}
|
|
85
|
+
<section class="card inline" data-inline="{{ formset.prefix }}" data-next-index="{{ formset.rows|length }}">
|
|
86
|
+
<h2 class="card-title">{{ formset.inline.name_plural }}</h2>
|
|
87
|
+
{% for error in formset.errors %}<div class="message message-error" role="alert">{{ error }}</div>{% endfor %}
|
|
88
|
+
<input type="hidden" name="{{ formset.prefix }}-TOTAL" value="{{ formset.rows|length }}" data-inline-total>
|
|
89
|
+
{% if formset.inline.template == "tabular" %}
|
|
90
|
+
<div class="table-wrap">
|
|
91
|
+
<table class="table table-inline">
|
|
92
|
+
<thead><tr>{% for label in formset.labels %}<th scope="col">{{ label }}</th>{% endfor %}<th></th></tr></thead>
|
|
93
|
+
<tbody data-inline-rows>
|
|
94
|
+
{% for row in formset.rows %}{{ tabular_row(formset, row) }}{% endfor %}
|
|
95
|
+
</tbody>
|
|
96
|
+
</table>
|
|
97
|
+
</div>
|
|
98
|
+
{% if formset.blank %}<template data-inline-blank><table><tbody>{{ tabular_row(formset, formset.blank, True) }}</tbody></table></template>{% endif %}
|
|
99
|
+
{% else %}
|
|
100
|
+
<div data-inline-rows>
|
|
101
|
+
{% for row in formset.rows %}{{ stacked_row(formset, row) }}{% endfor %}
|
|
102
|
+
</div>
|
|
103
|
+
{% if formset.blank %}<template data-inline-blank>{{ stacked_row(formset, formset.blank, True) }}</template>{% endif %}
|
|
104
|
+
{% endif %}
|
|
105
|
+
{% if formset.blank and formset.inline.has_add_permission(request) %}
|
|
106
|
+
<button class="link-button inline-add" type="button" data-inline-add>+ Add another {{ formset.inline.name.lower() }}</button>
|
|
107
|
+
{% endif %}
|
|
108
|
+
</section>
|
|
109
|
+
{% endfor %}
|
|
110
|
+
|
|
111
|
+
{{ submit_row() }}
|
|
112
|
+
</form>
|
|
113
|
+
{% endblock %}
|