mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-08-05 05:05:17 +02:00
Refactor response handling to use arrayBuffer instead of bytes
This commit is contained in:
parent
c58dc726f7
commit
15eece8882
2 changed files with 69 additions and 63 deletions
|
@ -15,8 +15,8 @@ from rest_framework.response import Response
|
||||||
from rest_framework.parsers import MultiPartParser
|
from rest_framework.parsers import MultiPartParser
|
||||||
|
|
||||||
from adventures.models import (
|
from adventures.models import (
|
||||||
Adventure, Collection, Transportation, Note, Checklist, ChecklistItem,
|
Location, Collection, Transportation, Note, Checklist, ChecklistItem,
|
||||||
AdventureImage, Attachment, Category, Lodging, Visit
|
LocationImage, Attachment, Category, Lodging, Visit
|
||||||
)
|
)
|
||||||
from worldtravel.models import VisitedCity, VisitedRegion, City, Region, Country
|
from worldtravel.models import VisitedCity, VisitedRegion, City, Region, Country
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
'user_email': user.email,
|
'user_email': user.email,
|
||||||
'categories': [],
|
'categories': [],
|
||||||
'collections': [],
|
'collections': [],
|
||||||
'adventures': [],
|
'locations': [],
|
||||||
'transportation': [],
|
'transportation': [],
|
||||||
'notes': [],
|
'notes': [],
|
||||||
'checklists': [],
|
'checklists': [],
|
||||||
|
@ -87,32 +87,32 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
# Create collection name to export_id mapping
|
# Create collection name to export_id mapping
|
||||||
collection_name_to_id = {col.name: idx for idx, col in enumerate(user.collection_set.all())}
|
collection_name_to_id = {col.name: idx for idx, col in enumerate(user.collection_set.all())}
|
||||||
|
|
||||||
# Export Adventures with related data
|
# Export locations with related data
|
||||||
for idx, adventure in enumerate(user.adventure_set.all()):
|
for idx, location in enumerate(user.location_set.all()):
|
||||||
adventure_data = {
|
location_data = {
|
||||||
'export_id': idx, # Add unique identifier for this export
|
'export_id': idx, # Add unique identifier for this export
|
||||||
'name': adventure.name,
|
'name': location.name,
|
||||||
'location': adventure.location,
|
'location': location.location,
|
||||||
'activity_types': adventure.activity_types,
|
'tags': location.tags,
|
||||||
'description': adventure.description,
|
'description': location.description,
|
||||||
'rating': adventure.rating,
|
'rating': location.rating,
|
||||||
'link': adventure.link,
|
'link': location.link,
|
||||||
'is_public': adventure.is_public,
|
'is_public': location.is_public,
|
||||||
'longitude': str(adventure.longitude) if adventure.longitude else None,
|
'longitude': str(location.longitude) if location.longitude else None,
|
||||||
'latitude': str(adventure.latitude) if adventure.latitude else None,
|
'latitude': str(location.latitude) if location.latitude else None,
|
||||||
'city': adventure.city_id,
|
'city': location.city_id,
|
||||||
'region': adventure.region_id,
|
'region': location.region_id,
|
||||||
'country': adventure.country_id,
|
'country': location.country_id,
|
||||||
'category_name': adventure.category.name if adventure.category else None,
|
'category_name': location.category.name if location.category else None,
|
||||||
'collection_export_ids': [collection_name_to_id[col_name] for col_name in adventure.collections.values_list('name', flat=True) if col_name in collection_name_to_id],
|
'collection_export_ids': [collection_name_to_id[col_name] for col_name in location.collections.values_list('name', flat=True) if col_name in collection_name_to_id],
|
||||||
'visits': [],
|
'visits': [],
|
||||||
'images': [],
|
'images': [],
|
||||||
'attachments': []
|
'attachments': []
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add visits
|
# Add visits
|
||||||
for visit in adventure.visits.all():
|
for visit in location.visits.all():
|
||||||
adventure_data['visits'].append({
|
location_data['visits'].append({
|
||||||
'start_date': visit.start_date.isoformat() if visit.start_date else None,
|
'start_date': visit.start_date.isoformat() if visit.start_date else None,
|
||||||
'end_date': visit.end_date.isoformat() if visit.end_date else None,
|
'end_date': visit.end_date.isoformat() if visit.end_date else None,
|
||||||
'timezone': visit.timezone,
|
'timezone': visit.timezone,
|
||||||
|
@ -120,7 +120,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
})
|
})
|
||||||
|
|
||||||
# Add images
|
# Add images
|
||||||
for image in adventure.images.all():
|
for image in location.images.all():
|
||||||
image_data = {
|
image_data = {
|
||||||
'immich_id': image.immich_id,
|
'immich_id': image.immich_id,
|
||||||
'is_primary': image.is_primary,
|
'is_primary': image.is_primary,
|
||||||
|
@ -128,19 +128,19 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
}
|
}
|
||||||
if image.image:
|
if image.image:
|
||||||
image_data['filename'] = image.image.name.split('/')[-1]
|
image_data['filename'] = image.image.name.split('/')[-1]
|
||||||
adventure_data['images'].append(image_data)
|
location_data['images'].append(image_data)
|
||||||
|
|
||||||
# Add attachments
|
# Add attachments
|
||||||
for attachment in adventure.attachments.all():
|
for attachment in location.attachments.all():
|
||||||
attachment_data = {
|
attachment_data = {
|
||||||
'name': attachment.name,
|
'name': attachment.name,
|
||||||
'filename': None
|
'filename': None
|
||||||
}
|
}
|
||||||
if attachment.file:
|
if attachment.file:
|
||||||
attachment_data['filename'] = attachment.file.name.split('/')[-1]
|
attachment_data['filename'] = attachment.file.name.split('/')[-1]
|
||||||
adventure_data['attachments'].append(attachment_data)
|
location_data['attachments'].append(attachment_data)
|
||||||
|
|
||||||
export_data['adventures'].append(adventure_data)
|
export_data['locations'].append(location_data)
|
||||||
|
|
||||||
# Export Transportation
|
# Export Transportation
|
||||||
for transport in user.transportation_set.all():
|
for transport in user.transportation_set.all():
|
||||||
|
@ -240,9 +240,9 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
# Add images and attachments
|
# Add images and attachments
|
||||||
files_added = set()
|
files_added = set()
|
||||||
|
|
||||||
for adventure in user.adventure_set.all():
|
for location in user.location_set.all():
|
||||||
# Add images
|
# Add images
|
||||||
for image in adventure.images.all():
|
for image in location.images.all():
|
||||||
if image.image and image.image.name not in files_added:
|
if image.image and image.image.name not in files_added:
|
||||||
try:
|
try:
|
||||||
image_content = default_storage.open(image.image.name).read()
|
image_content = default_storage.open(image.image.name).read()
|
||||||
|
@ -253,7 +253,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
print(f"Error adding image {image.image.name}: {e}")
|
print(f"Error adding image {image.image.name}: {e}")
|
||||||
|
|
||||||
# Add attachments
|
# Add attachments
|
||||||
for attachment in adventure.attachments.all():
|
for attachment in location.attachments.all():
|
||||||
if attachment.file and attachment.file.name not in files_added:
|
if attachment.file and attachment.file.name not in files_added:
|
||||||
try:
|
try:
|
||||||
file_content = default_storage.open(attachment.file.name).read()
|
file_content = default_storage.open(attachment.file.name).read()
|
||||||
|
@ -273,7 +273,13 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
os.unlink(tmp_file.name)
|
os.unlink(tmp_file.name)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@action(detail=False, methods=['post'], parser_classes=[MultiPartParser])
|
@action(
|
||||||
|
detail=False,
|
||||||
|
methods=['post'],
|
||||||
|
parser_classes=[MultiPartParser],
|
||||||
|
url_path='import', # changes the URL path to /import
|
||||||
|
url_name='import' # changes the reverse name to 'import'
|
||||||
|
)
|
||||||
def import_data(self, request):
|
def import_data(self, request):
|
||||||
"""
|
"""
|
||||||
Import data from a ZIP backup file
|
Import data from a ZIP backup file
|
||||||
|
@ -336,11 +342,11 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
user.transportation_set.all().delete()
|
user.transportation_set.all().delete()
|
||||||
user.lodging_set.all().delete()
|
user.lodging_set.all().delete()
|
||||||
|
|
||||||
# Delete adventure-related data
|
# Delete location-related data
|
||||||
user.adventureimage_set.all().delete()
|
user.locationimage_set.all().delete()
|
||||||
user.attachment_set.all().delete()
|
user.attachment_set.all().delete()
|
||||||
# Visits are deleted via cascade when adventures are deleted
|
# Visits are deleted via cascade when locations are deleted
|
||||||
user.adventure_set.all().delete()
|
user.location_set.all().delete()
|
||||||
|
|
||||||
# Delete collections and categories last
|
# Delete collections and categories last
|
||||||
user.collection_set.all().delete()
|
user.collection_set.all().delete()
|
||||||
|
@ -356,7 +362,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
category_map = {}
|
category_map = {}
|
||||||
collection_map = {} # Map export_id to actual collection object
|
collection_map = {} # Map export_id to actual collection object
|
||||||
summary = {
|
summary = {
|
||||||
'categories': 0, 'collections': 0, 'adventures': 0,
|
'categories': 0, 'collections': 0, 'locations': 0,
|
||||||
'transportation': 0, 'notes': 0, 'checklists': 0,
|
'transportation': 0, 'notes': 0, 'checklists': 0,
|
||||||
'checklist_items': 0, 'lodging': 0, 'images': 0, 'attachments': 0, 'visited_cities': 0, 'visited_regions': 0
|
'checklist_items': 0, 'lodging': 0, 'images': 0, 'attachments': 0, 'visited_cities': 0, 'visited_regions': 0
|
||||||
}
|
}
|
||||||
|
@ -365,7 +371,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
for city_data in backup_data.get('visited_cities', []):
|
for city_data in backup_data.get('visited_cities', []):
|
||||||
try:
|
try:
|
||||||
city_obj = City.objects.get(id=city_data['city'])
|
city_obj = City.objects.get(id=city_data['city'])
|
||||||
VisitedCity.objects.create(user_id=user, city=city_obj)
|
VisitedCity.objects.create(user=user, city=city_obj)
|
||||||
summary['visited_cities'] += 1
|
summary['visited_cities'] += 1
|
||||||
except City.DoesNotExist:
|
except City.DoesNotExist:
|
||||||
# If city does not exist, we can skip or log it
|
# If city does not exist, we can skip or log it
|
||||||
|
@ -375,7 +381,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
for region_data in backup_data.get('visited_regions', []):
|
for region_data in backup_data.get('visited_regions', []):
|
||||||
try:
|
try:
|
||||||
region_obj = Region.objects.get(id=region_data['region'])
|
region_obj = Region.objects.get(id=region_data['region'])
|
||||||
VisitedRegion.objects.create(user_id=user, region=region_obj)
|
VisitedRegion.objects.create(user=user, region=region_obj)
|
||||||
summary['visited_regions'] += 1
|
summary['visited_regions'] += 1
|
||||||
except Region.DoesNotExist:
|
except Region.DoesNotExist:
|
||||||
# If region does not exist, we can skip or log it
|
# If region does not exist, we can skip or log it
|
||||||
|
@ -384,7 +390,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
# Import Categories
|
# Import Categories
|
||||||
for cat_data in backup_data.get('categories', []):
|
for cat_data in backup_data.get('categories', []):
|
||||||
category = Category.objects.create(
|
category = Category.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=cat_data['name'],
|
name=cat_data['name'],
|
||||||
display_name=cat_data['display_name'],
|
display_name=cat_data['display_name'],
|
||||||
icon=cat_data.get('icon', '🌍')
|
icon=cat_data.get('icon', '🌍')
|
||||||
|
@ -395,7 +401,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
# Import Collections
|
# Import Collections
|
||||||
for col_data in backup_data.get('collections', []):
|
for col_data in backup_data.get('collections', []):
|
||||||
collection = Collection.objects.create(
|
collection = Collection.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=col_data['name'],
|
name=col_data['name'],
|
||||||
description=col_data.get('description', ''),
|
description=col_data.get('description', ''),
|
||||||
is_public=col_data.get('is_public', False),
|
is_public=col_data.get('is_public', False),
|
||||||
|
@ -415,8 +421,8 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Import Adventures
|
# Import Locations
|
||||||
for adv_data in backup_data.get('adventures', []):
|
for adv_data in backup_data.get('locations', []):
|
||||||
|
|
||||||
city = None
|
city = None
|
||||||
if adv_data.get('city'):
|
if adv_data.get('city'):
|
||||||
|
@ -439,11 +445,11 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
except Country.DoesNotExist:
|
except Country.DoesNotExist:
|
||||||
country = None
|
country = None
|
||||||
|
|
||||||
adventure = Adventure(
|
location = Location(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=adv_data['name'],
|
name=adv_data['name'],
|
||||||
location=adv_data.get('location'),
|
location=adv_data.get('location'),
|
||||||
activity_types=adv_data.get('activity_types', []),
|
tags=adv_data.get('tags', []),
|
||||||
description=adv_data.get('description'),
|
description=adv_data.get('description'),
|
||||||
rating=adv_data.get('rating'),
|
rating=adv_data.get('rating'),
|
||||||
link=adv_data.get('link'),
|
link=adv_data.get('link'),
|
||||||
|
@ -455,17 +461,17 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
country=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
|
location.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', []):
|
||||||
if collection_export_id in collection_map:
|
if collection_export_id in collection_map:
|
||||||
adventure.collections.add(collection_map[collection_export_id])
|
location.collections.add(collection_map[collection_export_id])
|
||||||
|
|
||||||
# Import visits
|
# Import visits
|
||||||
for visit_data in adv_data.get('visits', []):
|
for visit_data in adv_data.get('visits', []):
|
||||||
Visit.objects.create(
|
Visit.objects.create(
|
||||||
adventure=adventure,
|
location=location,
|
||||||
start_date=visit_data.get('start_date'),
|
start_date=visit_data.get('start_date'),
|
||||||
end_date=visit_data.get('end_date'),
|
end_date=visit_data.get('end_date'),
|
||||||
timezone=visit_data.get('timezone'),
|
timezone=visit_data.get('timezone'),
|
||||||
|
@ -476,9 +482,9 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
for img_data in adv_data.get('images', []):
|
for img_data in adv_data.get('images', []):
|
||||||
immich_id = img_data.get('immich_id')
|
immich_id = img_data.get('immich_id')
|
||||||
if immich_id:
|
if immich_id:
|
||||||
AdventureImage.objects.create(
|
LocationImage.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
adventure=adventure,
|
location=location,
|
||||||
immich_id=immich_id,
|
immich_id=immich_id,
|
||||||
is_primary=img_data.get('is_primary', False)
|
is_primary=img_data.get('is_primary', False)
|
||||||
)
|
)
|
||||||
|
@ -489,9 +495,9 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
try:
|
try:
|
||||||
img_content = zip_file.read(f'images/{filename}')
|
img_content = zip_file.read(f'images/{filename}')
|
||||||
img_file = ContentFile(img_content, name=filename)
|
img_file = ContentFile(img_content, name=filename)
|
||||||
AdventureImage.objects.create(
|
LocationImage.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
adventure=adventure,
|
location=location,
|
||||||
image=img_file,
|
image=img_file,
|
||||||
is_primary=img_data.get('is_primary', False)
|
is_primary=img_data.get('is_primary', False)
|
||||||
)
|
)
|
||||||
|
@ -507,8 +513,8 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
att_content = zip_file.read(f'attachments/{filename}')
|
att_content = zip_file.read(f'attachments/{filename}')
|
||||||
att_file = ContentFile(att_content, name=filename)
|
att_file = ContentFile(att_content, name=filename)
|
||||||
Attachment.objects.create(
|
Attachment.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
adventure=adventure,
|
location=location,
|
||||||
file=att_file,
|
file=att_file,
|
||||||
name=att_data.get('name')
|
name=att_data.get('name')
|
||||||
)
|
)
|
||||||
|
@ -516,7 +522,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
summary['adventures'] += 1
|
summary['locations'] += 1
|
||||||
|
|
||||||
# Import Transportation
|
# Import Transportation
|
||||||
for trans_data in backup_data.get('transportation', []):
|
for trans_data in backup_data.get('transportation', []):
|
||||||
|
@ -525,7 +531,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
collection = collection_map.get(trans_data['collection_export_id'])
|
collection = collection_map.get(trans_data['collection_export_id'])
|
||||||
|
|
||||||
Transportation.objects.create(
|
Transportation.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
type=trans_data['type'],
|
type=trans_data['type'],
|
||||||
name=trans_data['name'],
|
name=trans_data['name'],
|
||||||
description=trans_data.get('description'),
|
description=trans_data.get('description'),
|
||||||
|
@ -554,7 +560,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
collection = collection_map.get(note_data['collection_export_id'])
|
collection = collection_map.get(note_data['collection_export_id'])
|
||||||
|
|
||||||
Note.objects.create(
|
Note.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=note_data['name'],
|
name=note_data['name'],
|
||||||
content=note_data.get('content'),
|
content=note_data.get('content'),
|
||||||
links=note_data.get('links', []),
|
links=note_data.get('links', []),
|
||||||
|
@ -571,7 +577,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
collection = collection_map.get(check_data['collection_export_id'])
|
collection = collection_map.get(check_data['collection_export_id'])
|
||||||
|
|
||||||
checklist = Checklist.objects.create(
|
checklist = Checklist.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=check_data['name'],
|
name=check_data['name'],
|
||||||
date=check_data.get('date'),
|
date=check_data.get('date'),
|
||||||
is_public=check_data.get('is_public', False),
|
is_public=check_data.get('is_public', False),
|
||||||
|
@ -581,7 +587,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
# Import checklist items
|
# Import checklist items
|
||||||
for item_data in check_data.get('items', []):
|
for item_data in check_data.get('items', []):
|
||||||
ChecklistItem.objects.create(
|
ChecklistItem.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
checklist=checklist,
|
checklist=checklist,
|
||||||
name=item_data['name'],
|
name=item_data['name'],
|
||||||
is_checked=item_data.get('is_checked', False)
|
is_checked=item_data.get('is_checked', False)
|
||||||
|
@ -597,7 +603,7 @@ class BackupViewSet(viewsets.ViewSet):
|
||||||
collection = collection_map.get(lodg_data['collection_export_id'])
|
collection = collection_map.get(lodg_data['collection_export_id'])
|
||||||
|
|
||||||
Lodging.objects.create(
|
Lodging.objects.create(
|
||||||
user_id=user,
|
user=user,
|
||||||
name=lodg_data['name'],
|
name=lodg_data['name'],
|
||||||
type=lodg_data.get('type', 'other'),
|
type=lodg_data.get('type', 'other'),
|
||||||
description=lodg_data.get('description'),
|
description=lodg_data.get('description'),
|
||||||
|
|
|
@ -85,7 +85,7 @@ async function handleRequest(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseData = await response.bytes();
|
const responseData = await response.arrayBuffer();
|
||||||
// Create a new Headers object without the 'set-cookie' header
|
// Create a new Headers object without the 'set-cookie' header
|
||||||
const cleanHeaders = new Headers(response.headers);
|
const cleanHeaders = new Headers(response.headers);
|
||||||
cleanHeaders.delete('set-cookie');
|
cleanHeaders.delete('set-cookie');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue