46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import time
|
|
|
|
from django.contrib import admin
|
|
from django.http import HttpRequest
|
|
|
|
from locations.models import GeoLocation
|
|
|
|
from scrobbles.admin import ScrobbleInline
|
|
|
|
|
|
@admin.register(GeoLocation)
|
|
class GeoLocationAdmin(admin.ModelAdmin):
|
|
date_hierarchy = "created"
|
|
list_display = (
|
|
"created",
|
|
"lat",
|
|
"lon",
|
|
"title",
|
|
"altitude",
|
|
"city",
|
|
"state_province",
|
|
"country",
|
|
)
|
|
ordering = ("-created",)
|
|
search_fields = ("title",)
|
|
actions = ["reverse_geocode_selected"]
|
|
inlines = [
|
|
ScrobbleInline,
|
|
]
|
|
|
|
@admin.action(description="Reverse geocode selected locations")
|
|
def reverse_geocode_selected(self, request: HttpRequest, queryset):
|
|
updated = 0
|
|
errors = 0
|
|
for i, location in enumerate(queryset.iterator()):
|
|
if location.reverse_geocode():
|
|
updated += 1
|
|
else:
|
|
errors += 1
|
|
if i < queryset.count() - 1:
|
|
time.sleep(1.1)
|
|
msg = f"Reverse geocoded {updated} locations"
|
|
if errors:
|
|
msg += f", {errors} failed"
|
|
self.message_user(request, msg)
|