Configuring for having an all in sites
This commit is contained in:
parent
0ad64933c2
commit
a6f4ea7bc4
1633
data/denue/CDMX_all.csv
Normal file
1633
data/denue/CDMX_all.csv
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,47 @@
|
||||
from __future__ import annotations
|
||||
from pathlib import Path
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
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)
|
||||
class SiteRunAdmin(admin.ModelAdmin):
|
||||
list_display = ("created_at", "city", "business", "short_id", "preview", "download")
|
||||
list_display = ("created_at", "city", "business", "short_id", "artifact_status", "preview", "download")
|
||||
list_filter = ("city", "business", "created_at")
|
||||
search_fields = ("search_id", "city", "business")
|
||||
readonly_fields = ("created_at", "search_id", "city", "business", "payload_json", "result_json")
|
||||
actions = [delete_runs_and_artifacts]
|
||||
|
||||
def short_id(self, obj: SiteRun) -> str:
|
||||
return obj.search_id[:8]
|
||||
@ -24,3 +57,9 @@ class SiteRunAdmin(admin.ModelAdmin):
|
||||
if url:
|
||||
return format_html('<a href="{}" target="_blank">download</a>', url)
|
||||
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