1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-05 13:15: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()
@ -280,6 +280,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,21 +472,30 @@ class BackupViewSet(viewsets.ViewSet):
# Import images # Import images
for img_data in adv_data.get('images', []): for img_data in adv_data.get('images', []):
filename = img_data.get('filename') immich_id = img_data.get('immich_id')
if filename: if immich_id:
try: AdventureImage.objects.create(
img_content = zip_file.read(f'images/{filename}') user_id=user,
img_file = ContentFile(img_content, name=filename) adventure=adventure,
AdventureImage.objects.create( immich_id=immich_id,
user_id=user, is_primary=img_data.get('is_primary', False)
adventure=adventure, )
image=img_file, summary['images'] += 1
immich_id=img_data.get('immich_id'), else:
is_primary=img_data.get('is_primary', False) filename = img_data.get('filename')
) if filename:
summary['images'] += 1 try:
except KeyError: img_content = zip_file.read(f'images/{filename}')
pass img_file = ContentFile(img_content, name=filename)
AdventureImage.objects.create(
user_id=user,
adventure=adventure,
image=img_file,
is_primary=img_data.get('is_primary', False)
)
summary['images'] += 1
except KeyError:
pass
# Import attachments # Import attachments
for att_data in adv_data.get('attachments', []): for att_data in adv_data.get('attachments', []):