Files
simpleblog/templates/admin/albums.html
T
2026-11-14 17:05:01 +00:00

142 lines
6.0 KiB
HTML

{% extends "admin/base.html" %}
{% block title %}Albums · {{ site_title }}{% endblock %}
{% block admin_content %}
<h1>Albums</h1>
<div class="admin-panel">
<h2>Upload a photo to an album</h2>
<form id="album-upload-form" method="POST" action="{{ url_for('admin.upload') }}" enctype="multipart/form-data" class="stack">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<div class="form-group">
<label for="album-photo-file">Photo file</label>
<input type="file" id="album-photo-file" name="file" accept="image/*" multiple required>
<small>Choose one or more photos to add to this album.</small>
</div>
<div class="form-group">
<label for="album-photo-album">Album</label>
<select id="album-photo-album" name="album" required>
{% for a in albums %}
<option value="{{ a.slug }}">{{ a.name }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-primary">Upload photo</button>
</form>
</div>
<div class="admin-panel">
<h2>New album</h2>
<form method="POST" action="{{ url_for('admin.album_new') }}" class="stack">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<div class="form-group">
<label for="name">Album name</label>
<input type="text" id="name" name="name" placeholder="e.g. Road trip 2026" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create album</button>
</form>
</div>
{% if editing %}
<div class="admin-panel">
<h2>Edit: {{ editing.name }}</h2>
<form method="POST" action="{{ url_for('admin.album_edit', slug=editing.slug) }}" class="stack">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<div class="form-group">
<label for="name">Album name</label>
<input type="text" id="name" name="name" value="{{ editing.name }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3">{{ editing.description or '' }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
</div>
{% endif %}
<div class="admin-panel">
<h2>All albums</h2>
{% if albums %}
<table class="admin-table">
<thead><tr><th>Name</th><th>Photos</th><th></th></tr></thead>
<tbody>
{% for a in albums %}
<tr>
<td><a href="{{ url_for('public.photos', slug=a.slug) }}">{{ a.name }}</a></td>
<td>{{ album_counts.get(a.id, 0) }}</td>
<td>
<a class="primary" href="{{ url_for('admin.album_edit', slug=a.slug) }}">Edit</a>
<span class="field-hint">·</span>
<form method="POST" action="{{ url_for('admin.album_delete', slug=a.slug) }}" style="display:inline;" onsubmit="return confirm('Delete this album? Its photos stay, but become ungrouped.');">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p class="empty-state">No albums yet.</p>
{% endif %}
</div>
{% if ungrouped_count %}
<p><a class="primary" href="{{ url_for('admin.media_list') }}">Manage photos ({{ ungrouped_count }} ungrouped)</a></p>
{% endif %}
<script>
// NOTE: one file per request, with FormData built from scratch. Cloning the
// form (new FormData(form)) also re-sends the <input type="file"> parts, so
// the server stored the selected photo twice.
// NOTE: every upload is awaited before location.reload(). The old version
// fired all the fetches and reloaded synchronously in the same tick, which
// aborted the in-flight requests — the album came back unchanged, so uploads
// looked broken even though /admin/upload was healthy.
(function () {
var form = document.getElementById("album-upload-form");
if (!form) return;
form.addEventListener("submit", async function (e) {
e.preventDefault();
var fileInput = form.elements["album-photo-file"];
var files = Array.prototype.slice.call(fileInput.files);
if (!files.length) { alert("Choose one or more photos."); return; }
var album = form.elements["album-photo-album"].value;
var albumName = album ? form.elements["album-photo-album"].options[form.elements["album-photo-album"].selectedIndex].text : "";
var csrf = form.elements["csrf_token"].value;
var btn = form.querySelector("button[type=submit]");
btn.disabled = true;
var ok = 0, failed = [];
for (var i = 0; i < files.length; i++) {
btn.textContent = "Uploading " + (i + 1) + "/" + files.length + "...";
var fd = new FormData();
fd.append("csrf_token", csrf);
if (album) fd.append("album", album);
fd.append("file", files[i], files[i].name);
try {
var r = await fetch(form.action, { method: "POST", credentials: "same-origin", body: fd });
if (!r.ok) throw new Error("HTTP " + r.status);
await r.json();
ok++;
} catch (err) {
failed.push(files[i].name + ": " + err.message);
}
}
btn.disabled = false;
btn.textContent = "Upload photo";
if (failed.length) {
alert(ok + " added to '" + albumName + "', " + failed.length + " failed:\n" + failed.join("\n"));
} else {
alert(ok + " photo" + (ok === 1 ? "" : "s") + " added to '" + albumName + "'.");
}
location.reload();
});
})();
</script>
{% endblock %}