origin/main #1
@ -19,7 +19,7 @@ from ..models.models import (Backup, BackupStatus, Category, CategoryRead,
|
|||||||
TripRead, User, UserRead)
|
TripRead, User, UserRead)
|
||||||
from .date import dt_utc, iso_to_dt
|
from .date import dt_utc, iso_to_dt
|
||||||
from .utils import (assets_folder_path, attachments_trip_folder_path,
|
from .utils import (assets_folder_path, attachments_trip_folder_path,
|
||||||
b64img_decode, save_image_to_file)
|
b64img_decode, remove_image, save_image_to_file)
|
||||||
|
|
||||||
|
|
||||||
def process_backup_export(session: SessionDep, backup_id: int):
|
def process_backup_export(session: SessionDep, backup_id: int):
|
||||||
@ -147,7 +147,6 @@ async def process_backup_import(
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise HTTPException(status_code=400, detail="Invalid file")
|
raise HTTPException(status_code=400, detail="Invalid file")
|
||||||
|
|
||||||
try:
|
|
||||||
with ZipFile(io.BytesIO(zip_content), "r") as zipf:
|
with ZipFile(io.BytesIO(zip_content), "r") as zipf:
|
||||||
zip_filenames = zipf.namelist()
|
zip_filenames = zipf.namelist()
|
||||||
if "data.json" not in zip_filenames:
|
if "data.json" not in zip_filenames:
|
||||||
@ -170,6 +169,9 @@ async def process_backup_import(
|
|||||||
if path.startswith("attachments/") and not path.endswith("/")
|
if path.startswith("attachments/") and not path.endswith("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
error_details = "Bad request"
|
||||||
|
created_image_filenames = []
|
||||||
|
created_attachment_trips = []
|
||||||
try:
|
try:
|
||||||
existing_categories = {
|
existing_categories = {
|
||||||
category.name: category
|
category.name: category
|
||||||
@ -196,6 +198,7 @@ async def process_backup_import(
|
|||||||
session.add(image)
|
session.add(image)
|
||||||
session.flush()
|
session.flush()
|
||||||
session.refresh(image)
|
session.refresh(image)
|
||||||
|
created_image_filenames.append(filename)
|
||||||
|
|
||||||
if category_exists.image_id:
|
if category_exists.image_id:
|
||||||
old_image = session.get(Image, category_exists.image_id)
|
old_image = session.get(Image, category_exists.image_id)
|
||||||
@ -213,9 +216,7 @@ async def process_backup_import(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
new_category = {
|
new_category = {
|
||||||
key: category[key]
|
key: category[key] for key in category.keys() if key not in {"id", "image", "image_id"}
|
||||||
for key in category.keys()
|
|
||||||
if key not in {"id", "image", "image_id"}
|
|
||||||
}
|
}
|
||||||
new_category["user"] = current_user
|
new_category["user"] = current_user
|
||||||
|
|
||||||
@ -230,6 +231,7 @@ async def process_backup_import(
|
|||||||
session.add(image)
|
session.add(image)
|
||||||
session.flush()
|
session.flush()
|
||||||
session.refresh(image)
|
session.refresh(image)
|
||||||
|
created_image_filenames.append(filename)
|
||||||
new_category["image_id"] = image.id
|
new_category["image_id"] = image.id
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -270,6 +272,7 @@ async def process_backup_import(
|
|||||||
session.add(image)
|
session.add(image)
|
||||||
session.flush()
|
session.flush()
|
||||||
session.refresh(image)
|
session.refresh(image)
|
||||||
|
created_image_filenames.append(filename)
|
||||||
new_place["image_id"] = image.id
|
new_place["image_id"] = image.id
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -341,6 +344,7 @@ async def process_backup_import(
|
|||||||
session.add(image)
|
session.add(image)
|
||||||
session.flush()
|
session.flush()
|
||||||
session.refresh(image)
|
session.refresh(image)
|
||||||
|
created_image_filenames.append(filename)
|
||||||
new_trip["image_id"] = image.id
|
new_trip["image_id"] = image.id
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -372,13 +376,14 @@ async def process_backup_import(
|
|||||||
new_attachment = {
|
new_attachment = {
|
||||||
key: attachment[key]
|
key: attachment[key]
|
||||||
for key in attachment
|
for key in attachment
|
||||||
if key not in {"id", "trip_id", "trip"}
|
if key not in {"id", "trip_id", "trip", "uploaded_by"}
|
||||||
}
|
}
|
||||||
new_attachment["trip_id"] = new_trip.id
|
new_attachment["trip_id"] = new_trip.id
|
||||||
new_attachment["user"] = current_user
|
new_attachment["uploaded_by"] = current_user
|
||||||
new_attachment_obj = TripAttachment(**new_attachment)
|
new_attachment_obj = TripAttachment(**new_attachment)
|
||||||
|
|
||||||
attachment_path = attachments_trip_folder_path(new_trip.id) / stored_filename
|
attachment_path = attachments_trip_folder_path(new_trip.id) / stored_filename
|
||||||
|
created_attachment_trips.append(new_trip.id)
|
||||||
attachment_path.write_bytes(attachment_bytes)
|
attachment_path.write_bytes(attachment_bytes)
|
||||||
session.add(new_attachment_obj)
|
session.add(new_attachment_obj)
|
||||||
session.flush()
|
session.flush()
|
||||||
@ -397,13 +402,19 @@ async def process_backup_import(
|
|||||||
session.flush()
|
session.flush()
|
||||||
|
|
||||||
for item in day.get("items", []):
|
for item in day.get("items", []):
|
||||||
|
if item.get("paid_by"):
|
||||||
|
u = item.get("paid_by")
|
||||||
|
db_user = session.get(User, u)
|
||||||
|
if not db_user:
|
||||||
|
error_details = f"User <{u}> does not exist and is specified in Paid By"
|
||||||
|
raise
|
||||||
|
|
||||||
item_data = {
|
item_data = {
|
||||||
key: item[key]
|
key: item[key]
|
||||||
for key in item
|
for key in item
|
||||||
if key not in {"id", "place", "place_id", "image", "image_id", "attachments"}
|
if key not in {"id", "place", "place_id", "image", "image_id", "attachments"}
|
||||||
}
|
}
|
||||||
item_data["day_id"] = new_day.id
|
item_data["day_id"] = new_day.id
|
||||||
item_data["user"] = current_user
|
|
||||||
|
|
||||||
place = item.get("place")
|
place = item.get("place")
|
||||||
if place and (place_id := place.get("id")):
|
if place and (place_id := place.get("id")):
|
||||||
@ -421,6 +432,7 @@ async def process_backup_import(
|
|||||||
session.add(image)
|
session.add(image)
|
||||||
session.flush()
|
session.flush()
|
||||||
session.refresh(image)
|
session.refresh(image)
|
||||||
|
created_image_filenames.append(filename)
|
||||||
item_data["image_id"] = image.id
|
item_data["image_id"] = image.id
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -482,14 +494,21 @@ async def process_backup_import(
|
|||||||
"settings": UserRead.serialize(session.get(User, current_user)),
|
"settings": UserRead.serialize(session.get(User, current_user)),
|
||||||
}
|
}
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(exc)
|
for filename in created_image_filenames:
|
||||||
raise HTTPException(status_code=400, detail="Bad request")
|
remove_image(filename)
|
||||||
|
for trip_id in created_attachment_trips:
|
||||||
except Exception as exc:
|
try:
|
||||||
print(exc)
|
folder = attachments_trip_folder_path(trip_id)
|
||||||
raise HTTPException(status_code=400, detail="Bad request")
|
if not folder.exists():
|
||||||
|
return
|
||||||
|
for file in folder.iterdir():
|
||||||
|
file.unlink()
|
||||||
|
folder.rmdir()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
raise HTTPException(status_code=400, detail=error_details)
|
||||||
|
|
||||||
|
|
||||||
async def process_legacy_import(
|
async def process_legacy_import(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user