1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-06 21:55:18 +02:00

feat(import): enhance import functionality with confirmation check and improved city/region/country handling

This commit is contained in:
Sean Morley 2025-06-18 09:45:17 -04:00
parent 3ad5455731
commit fb5bd09c93

View file

@ -18,7 +18,7 @@ from adventures.models import (
Adventure, Collection, Transportation, Note, Checklist, ChecklistItem, Adventure, Collection, Transportation, Note, Checklist, ChecklistItem,
AdventureImage, Attachment, Category, Lodging, Visit AdventureImage, Attachment, Category, Lodging, Visit
) )
from worldtravel.models import VisitedCity, VisitedRegion, City, Region from worldtravel.models import VisitedCity, VisitedRegion, City, Region, Country
User = get_user_model() User = get_user_model()
@ -281,6 +281,10 @@ class BackupViewSet(viewsets.ViewSet):
if 'file' not in request.FILES: if 'file' not in request.FILES:
return Response({'error': 'No file provided'}, status=status.HTTP_400_BAD_REQUEST) return Response({'error': 'No file provided'}, status=status.HTTP_400_BAD_REQUEST)
if 'confirm' not in request.data or request.data['confirm'] != 'yes':
return Response({'error': 'Confirmation required to proceed with import'},
status=status.HTTP_400_BAD_REQUEST)
backup_file = request.FILES['file'] backup_file = request.FILES['file']
user = request.user user = request.user
@ -411,7 +415,29 @@ class BackupViewSet(viewsets.ViewSet):
# Import Adventures # Import Adventures
for adv_data in backup_data.get('adventures', []): for adv_data in backup_data.get('adventures', []):
adventure = Adventure.objects.create(
city = None
if adv_data.get('city'):
try:
city = City.objects.get(id=adv_data['city'])
except City.DoesNotExist:
city = None
region = None
if adv_data.get('region'):
try:
region = Region.objects.get(id=adv_data['region'])
except Region.DoesNotExist:
region = None
country = None
if adv_data.get('country'):
try:
country = Country.objects.get(id=adv_data['country'])
except Country.DoesNotExist:
country = None
adventure = Adventure(
user_id=user, user_id=user,
name=adv_data['name'], name=adv_data['name'],
location=adv_data.get('location'), location=adv_data.get('location'),
@ -422,11 +448,12 @@ class BackupViewSet(viewsets.ViewSet):
is_public=adv_data.get('is_public', False), is_public=adv_data.get('is_public', False),
longitude=adv_data.get('longitude'), longitude=adv_data.get('longitude'),
latitude=adv_data.get('latitude'), latitude=adv_data.get('latitude'),
city_id=adv_data.get('city'), city=city,
region_id=adv_data.get('region'), region=region,
country_id=adv_data.get('country'), country=country,
category=category_map.get(adv_data.get('category_name')) category=category_map.get(adv_data.get('category_name'))
) )
adventure.save(_skip_geocode=True) # Skip geocoding for now
# Add to collections using export_ids # Add to collections using export_ids
for collection_export_id in adv_data.get('collection_export_ids', []): for collection_export_id in adv_data.get('collection_export_ids', []):
@ -445,6 +472,16 @@ class BackupViewSet(viewsets.ViewSet):
# Import images # Import images
for img_data in adv_data.get('images', []): for img_data in adv_data.get('images', []):
immich_id = img_data.get('immich_id')
if immich_id:
AdventureImage.objects.create(
user_id=user,
adventure=adventure,
immich_id=immich_id,
is_primary=img_data.get('is_primary', False)
)
summary['images'] += 1
else:
filename = img_data.get('filename') filename = img_data.get('filename')
if filename: if filename:
try: try:
@ -454,7 +491,6 @@ class BackupViewSet(viewsets.ViewSet):
user_id=user, user_id=user,
adventure=adventure, adventure=adventure,
image=img_file, image=img_file,
immich_id=img_data.get('immich_id'),
is_primary=img_data.get('is_primary', False) is_primary=img_data.get('is_primary', False)
) )
summary['images'] += 1 summary['images'] += 1