(feature) Add snapshot discovery action to host view
Add a staff-only POST action on host detail pages to discover existing snapshots for that host and record them into SQL. Show success or failure feedback through Django messages, and keep the action non-destructive before adding heavier backup or retention controls. Cover the action with view tests for successful discovery, redirect behavior, and method safety.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.db.models import Count
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.views.decorators.http import require_POST
|
||||
|
||||
from .models import BackupRun, HostConfig, ScheduleConfig, SnapshotRecord
|
||||
from .snapshot_discovery import discover_snapshots
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@@ -48,6 +51,25 @@ def host_detail(request, host: str):
|
||||
return render(request, "pobsync_backend/host_detail.html", context)
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_POST
|
||||
def discover_host_snapshots(request, host: str):
|
||||
host_config = get_object_or_404(HostConfig, host=host)
|
||||
try:
|
||||
result = discover_snapshots(host=host_config)
|
||||
except Exception as exc:
|
||||
messages.error(request, f"Snapshot discovery failed for {host_config.host}: {exc}")
|
||||
else:
|
||||
messages.success(
|
||||
request,
|
||||
(
|
||||
f"Snapshot discovery scanned {result['scanned']} items for {host_config.host}: "
|
||||
f"{result['created']} created, {result['updated']} updated."
|
||||
),
|
||||
)
|
||||
return redirect("host_detail", host=host_config.host)
|
||||
|
||||
|
||||
def _schedule_for_host(host_config: HostConfig) -> ScheduleConfig | None:
|
||||
try:
|
||||
return host_config.schedule
|
||||
|
||||
Reference in New Issue
Block a user