mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-23 06:49:37 +02:00
Add bulk geocoding command and trigger geocoding action in admin
This commit is contained in:
parent
ac32f9ac5b
commit
84cd136401
3 changed files with 40 additions and 1 deletions
|
@ -8,11 +8,25 @@ from allauth.account.decorators import secure_admin_login
|
||||||
admin.autodiscover()
|
admin.autodiscover()
|
||||||
admin.site.login = secure_admin_login(admin.site.login)
|
admin.site.login = secure_admin_login(admin.site.login)
|
||||||
|
|
||||||
|
@admin.action(description="Trigger geocoding")
|
||||||
|
def trigger_geocoding(modeladmin, request, queryset):
|
||||||
|
count = 0
|
||||||
|
for adventure in queryset:
|
||||||
|
try:
|
||||||
|
adventure.save() # Triggers geocoding logic in your model
|
||||||
|
count += 1
|
||||||
|
except Exception as e:
|
||||||
|
modeladmin.message_user(request, f"Error geocoding {adventure}: {e}", level='error')
|
||||||
|
modeladmin.message_user(request, f"Geocoding triggered for {count} adventures.", level='success')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class AdventureAdmin(admin.ModelAdmin):
|
class AdventureAdmin(admin.ModelAdmin):
|
||||||
list_display = ('name', 'get_category', 'get_visit_count', 'user_id', 'is_public')
|
list_display = ('name', 'get_category', 'get_visit_count', 'user_id', 'is_public')
|
||||||
list_filter = ( 'user_id', 'is_public')
|
list_filter = ( 'user_id', 'is_public')
|
||||||
search_fields = ('name',)
|
search_fields = ('name',)
|
||||||
readonly_fields = ('city', 'region', 'country')
|
readonly_fields = ('city', 'region', 'country')
|
||||||
|
actions = [trigger_geocoding]
|
||||||
|
|
||||||
def get_category(self, obj):
|
def get_category(self, obj):
|
||||||
if obj.category and obj.category.display_name and obj.category.icon:
|
if obj.category and obj.category.display_name and obj.category.icon:
|
||||||
|
|
|
@ -579,7 +579,6 @@ class Adventure(models.Model):
|
||||||
|
|
||||||
if self.latitude and self.longitude:
|
if self.latitude and self.longitude:
|
||||||
reverse_geocode_result = reverse_geocode(self.latitude, self.longitude, self.user_id)
|
reverse_geocode_result = reverse_geocode(self.latitude, self.longitude, self.user_id)
|
||||||
print(reverse_geocode_result)
|
|
||||||
if 'region_id' in reverse_geocode_result:
|
if 'region_id' in reverse_geocode_result:
|
||||||
region = Region.objects.filter(id=reverse_geocode_result['region_id']).first()
|
region = Region.objects.filter(id=reverse_geocode_result['region_id']).first()
|
||||||
if region:
|
if region:
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from adventures.models import Adventure
|
||||||
|
import time
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Bulk geocode all adventures by triggering save on each one'
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
adventures = Adventure.objects.all()
|
||||||
|
total = adventures.count()
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS(f'Starting bulk geocoding of {total} adventures'))
|
||||||
|
|
||||||
|
for i, adventure in enumerate(adventures):
|
||||||
|
try:
|
||||||
|
self.stdout.write(f'Processing adventure {i+1}/{total}: {adventure}')
|
||||||
|
adventure.save() # This should trigger any geocoding in the save method
|
||||||
|
self.stdout.write(self.style.SUCCESS(f'Successfully processed adventure {i+1}/{total}'))
|
||||||
|
except Exception as e:
|
||||||
|
self.stdout.write(self.style.ERROR(f'Error processing adventure {i+1}/{total}: {adventure} - {e}'))
|
||||||
|
|
||||||
|
# Sleep for 2 seconds between each save
|
||||||
|
if i < total - 1: # Don't sleep after the last one
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
self.stdout.write(self.style.SUCCESS('Finished processing all adventures'))
|
Loading…
Add table
Add a link
Reference in a new issue