Normalize image galleries
This commit is contained in:
@@ -252,6 +252,8 @@ class PersistenceService:
|
||||
images: list[dict[str, object]],
|
||||
origin_id: str,
|
||||
) -> int:
|
||||
if not images:
|
||||
return 0
|
||||
nested = session.begin_nested()
|
||||
try:
|
||||
session.execute(delete(Image).where(Image.car_id == car_id))
|
||||
@@ -325,6 +327,14 @@ class PersistenceService:
|
||||
entry["car_id"] = car_id
|
||||
|
||||
insert_images_by_car_id: dict[int, list[dict[str, object]]] = {}
|
||||
updated_entries = [
|
||||
entry for entry in entries
|
||||
if str(entry.get("action") or "updated") == "updated"
|
||||
]
|
||||
existing_images_map = self._load_existing_image_urls(
|
||||
session,
|
||||
{int(entry["car_id"]) for entry in updated_entries},
|
||||
)
|
||||
updated_entries_needing_compare: list[dict[str, object]] = []
|
||||
|
||||
for entry in entries:
|
||||
@@ -332,11 +342,13 @@ class PersistenceService:
|
||||
action = str(entry.get("action") or "updated")
|
||||
images = entry["images"]
|
||||
skip_image_sync = bool(entry.get("skip_image_sync"))
|
||||
if action == "updated" and (MOBILEDE_SKIP_IMAGES_FOR_UPDATED or skip_image_sync):
|
||||
continue
|
||||
if action == "inserted":
|
||||
insert_images_by_car_id[car_id] = images
|
||||
continue
|
||||
if not images or skip_image_sync:
|
||||
continue
|
||||
if MOBILEDE_SKIP_IMAGES_FOR_UPDATED and existing_images_map.get(car_id):
|
||||
continue
|
||||
updated_entries_needing_compare.append(entry)
|
||||
|
||||
for car_id, images in insert_images_by_car_id.items():
|
||||
@@ -344,8 +356,6 @@ class PersistenceService:
|
||||
images_total += len(images)
|
||||
|
||||
if updated_entries_needing_compare:
|
||||
car_ids = {int(entry["car_id"]) for entry in updated_entries_needing_compare}
|
||||
existing_images_map = self._load_existing_image_urls(session, car_ids)
|
||||
images_by_car_id: dict[int, list[dict[str, object]]] = {}
|
||||
replace_ids: list[int] = []
|
||||
|
||||
@@ -474,7 +484,7 @@ class PersistenceService:
|
||||
existing_by_id, existing_by_url = self._load_existing_cars(session, origin_ids, origin_urls)
|
||||
|
||||
new_cars: list[tuple[Car, list[dict]]] = []
|
||||
update_image_candidates: list[tuple[Car, list[dict]]] = []
|
||||
updated_cars: list[tuple[Car, list[dict], bool]] = []
|
||||
|
||||
for record in records:
|
||||
payload = self._car_payload(record)
|
||||
@@ -489,9 +499,7 @@ class PersistenceService:
|
||||
else:
|
||||
self._apply_update_payload(car, payload)
|
||||
updated += 1
|
||||
if MOBILEDE_SKIP_IMAGES_FOR_UPDATED or self._skip_image_sync(record):
|
||||
continue
|
||||
update_image_candidates.append((car, images))
|
||||
updated_cars.append((car, images, self._skip_image_sync(record)))
|
||||
|
||||
# Один flush.
|
||||
session.flush()
|
||||
@@ -502,12 +510,16 @@ class PersistenceService:
|
||||
images_total += len(images)
|
||||
|
||||
update_cars_needing_images: list[tuple[Car, list[dict]]] = []
|
||||
if update_image_candidates:
|
||||
update_ids = [int(car.id) for car, _ in update_image_candidates]
|
||||
if updated_cars:
|
||||
update_ids = [int(item[0].id) for item in updated_cars]
|
||||
existing_images_map = self._load_existing_image_urls(session, set(update_ids))
|
||||
for car, images in update_image_candidates:
|
||||
new_image_urls = {img.get("fullres_image", "") for img in images}
|
||||
for car, images, skip_image_sync in updated_cars:
|
||||
old_image_urls = existing_images_map.get(int(car.id), set())
|
||||
if not images or skip_image_sync:
|
||||
continue
|
||||
if MOBILEDE_SKIP_IMAGES_FOR_UPDATED and old_image_urls:
|
||||
continue
|
||||
new_image_urls = {img.get("fullres_image", "") for img in images}
|
||||
if new_image_urls != old_image_urls:
|
||||
update_cars_needing_images.append((car, images))
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user