Compare commits
No commits in common. "d7ab546650617e89b33306cfedb994d332b8f597" and "0ad64933c2ea03018bcc083a98c96274e0b050a1" have entirely different histories.
d7ab546650
...
0ad64933c2
@ -1,47 +1,14 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from pathlib import Path
|
|
||||||
from django.conf import settings
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils.html import format_html
|
from django.utils.html import format_html
|
||||||
from .models import SiteRun
|
from .models import SiteRun
|
||||||
|
|
||||||
|
|
||||||
def _artifact_paths(search_id: str) -> list[Path]:
|
|
||||||
media_dir = Path(settings.MEDIA_ROOT) / "sites"
|
|
||||||
return [
|
|
||||||
media_dir / f"run_{search_id}.json",
|
|
||||||
media_dir / f"sites_{search_id}.png",
|
|
||||||
media_dir / f"demand_{search_id}.png",
|
|
||||||
media_dir / f"competition_{search_id}.png",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@admin.action(description="Delete selected runs and artifacts")
|
|
||||||
def delete_runs_and_artifacts(modeladmin, request, queryset):
|
|
||||||
removed_files = 0
|
|
||||||
for run in queryset:
|
|
||||||
for path in _artifact_paths(run.search_id):
|
|
||||||
try:
|
|
||||||
if path.exists():
|
|
||||||
path.unlink()
|
|
||||||
removed_files += 1
|
|
||||||
except Exception:
|
|
||||||
continue
|
|
||||||
count = queryset.count()
|
|
||||||
queryset.delete()
|
|
||||||
modeladmin.message_user(
|
|
||||||
request,
|
|
||||||
f"Deleted {count} runs and {removed_files} artifact files.",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@admin.register(SiteRun)
|
@admin.register(SiteRun)
|
||||||
class SiteRunAdmin(admin.ModelAdmin):
|
class SiteRunAdmin(admin.ModelAdmin):
|
||||||
list_display = ("created_at", "city", "business", "short_id", "artifact_status", "preview", "download")
|
list_display = ("created_at", "city", "business", "short_id", "preview", "download")
|
||||||
list_filter = ("city", "business", "created_at")
|
list_filter = ("city", "business", "created_at")
|
||||||
search_fields = ("search_id", "city", "business")
|
search_fields = ("search_id", "city", "business")
|
||||||
readonly_fields = ("created_at", "search_id", "city", "business", "payload_json", "result_json")
|
readonly_fields = ("created_at", "search_id", "city", "business", "payload_json", "result_json")
|
||||||
actions = [delete_runs_and_artifacts]
|
|
||||||
|
|
||||||
def short_id(self, obj: SiteRun) -> str:
|
def short_id(self, obj: SiteRun) -> str:
|
||||||
return obj.search_id[:8]
|
return obj.search_id[:8]
|
||||||
@ -57,9 +24,3 @@ class SiteRunAdmin(admin.ModelAdmin):
|
|||||||
if url:
|
if url:
|
||||||
return format_html('<a href="{}" target="_blank">download</a>', url)
|
return format_html('<a href="{}" target="_blank">download</a>', url)
|
||||||
return "—"
|
return "—"
|
||||||
|
|
||||||
def artifact_status(self, obj: SiteRun) -> bool:
|
|
||||||
return (Path(settings.MEDIA_ROOT) / "sites" / f"run_{obj.search_id}.json").exists()
|
|
||||||
|
|
||||||
artifact_status.boolean = True
|
|
||||||
artifact_status.short_description = "Artifacts"
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user