Add HTMX endpoint for truncating series images and corresponding result template
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<div class="alert alert-success">
|
||||
<strong>Truncate complete.</strong>
|
||||
<div class="small mt-1">Removed {{ images_removed|length }} images from series {{ series.pk }}.</div>
|
||||
</div>
|
||||
{% if images_removed %}
|
||||
<details class="mt-2">
|
||||
<summary class="small">Show removed image IDs</summary>
|
||||
<div class="small mt-1">{{ images_removed|join:", " }}</div>
|
||||
</details>
|
||||
{% endif %}
|
||||
@@ -251,8 +251,10 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if(confirm(`Trucated series. This will leave ${upper_bound - lower_bound + 1} images.`) == true) {
|
||||
htmx.ajax("GET", `{% url 'api-1:series_truncate' series.id '****' '++++' %}`.replace("****", lower_bound).replace("++++", upper_bound), "#truncate-output")
|
||||
if(confirm(`Trucated series. This will leave ${upper_bound - lower_bound + 1} images.`) == true) {
|
||||
const truncateTemplate = `{% url 'atlas:series_truncate' series.id 0 0 %}`; // e.g. /atlas/series/920/truncate/0/0/
|
||||
const truncateUrl = truncateTemplate.replace('/0/0/', `/${lower_bound}/${upper_bound}/`);
|
||||
htmx.ajax("GET", truncateUrl, "#truncate-output")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -472,6 +472,11 @@ urlpatterns = [
|
||||
# TODO: case context series viewing (so that we can view series in the context of a case)
|
||||
path("series/<int:pk>", views.series_detail, name="series_detail"),
|
||||
path("series/<int:pk>/viewer", views.series_viewer, name="series_viewer"),
|
||||
path(
|
||||
"series/<int:series_id>/truncate/<int:start>/<int:end>/",
|
||||
views.series_truncate_htmx,
|
||||
name="series_truncate",
|
||||
),
|
||||
path("series/<int:pk>/images/", views.series_images_partial, name="series_images"),
|
||||
path("series/<int:pk>/authors", views.SeriesAuthorUpdate.as_view(), name="series_authors"),
|
||||
path("series/<int:series_id>/finding/related", views.series_finding_related, name="series_finding_related"),
|
||||
|
||||
@@ -196,6 +196,43 @@ from helpers.cimar import CimarAPI
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@login_required
|
||||
def series_truncate_htmx(request, series_id, start, end):
|
||||
"""HTMX endpoint to truncate a series' images to the given inclusive
|
||||
start/end bounds (zero-based). Returns an HTML fragment with the result.
|
||||
"""
|
||||
series = get_object_or_404(Series, pk=series_id)
|
||||
|
||||
if not series.check_user_can_edit(request.user):
|
||||
return HttpResponse("<div class=\"alert alert-danger\">Permission denied</div>")
|
||||
|
||||
images_removed = []
|
||||
for n, image in enumerate(series.get_images()):
|
||||
if n >= int(start) and n <= int(end):
|
||||
# keep images within bounds
|
||||
continue
|
||||
else:
|
||||
image.removed = True
|
||||
image.image = None
|
||||
image.save()
|
||||
images_removed.append(image.pk)
|
||||
|
||||
# Recalculate stored series size
|
||||
try:
|
||||
series.get_total_image_size(refresh=True)
|
||||
except Exception:
|
||||
# ignore errors when recalculating
|
||||
pass
|
||||
|
||||
# Render a simple message fragment for HTMX swap
|
||||
html = render_to_string(
|
||||
"atlas/partials/truncate_result.html",
|
||||
{"images_removed": images_removed, "series": series},
|
||||
request=request,
|
||||
)
|
||||
return HttpResponse(html)
|
||||
|
||||
|
||||
@login_required
|
||||
def case_search_partial(request):
|
||||
"""Return an HTMX partial listing cases matching the query parameter `q`.
|
||||
|
||||
Reference in New Issue
Block a user