loko/streetup/assets/migrations/0022_sync_trafficlights_documents.py
2026-07-22 14:48:40 +02:00

48 lines
2 KiB
Python

# Generated by hand
from django.db import migrations
def sync_trafficlights_documents(apps, schema_editor):
from assets.models import TrafficLightIntersection as ActiveIntersection, TrafficLightIntersectionDocument as ActiveIntersectionDoc
from assets.views.trafficlights import ensure_intersection_document_folder, generate_dxf_overlay_managed_document
from documents.models import ensure_document_for_legacy
for intersection in ActiveIntersection.objects.all():
try:
ensure_intersection_document_folder(intersection)
except Exception as e:
print(f"Error ensuring document folder for intersection {intersection.id}: {e}")
for doc in ActiveIntersectionDoc.objects.all():
try:
import os
if not doc.file or not doc.file.name:
continue
# If the file does not exist on disk, we skip it gracefully
if not os.path.exists(doc.file.path):
print(f"Migration: File not found on disk, skipping sync for {doc.file.name}")
continue
# Sync it to the ManagedDocument system
ensure_document_for_legacy(doc)
# If it's a DXF, generate the overlay
if doc.file.name.lower().endswith('.dxf') and doc.managed_document:
generate_dxf_overlay_managed_document(doc.managed_document, user=doc.uploaded_by)
except Exception as e:
print(f"Error syncing document during migration for {doc.file.name if doc.file else 'None'}: {e}")
def reverse_sync_trafficlights_documents(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('assets', '0021_set_default_validation_status_for_active'),
('documents', '0003_manageddocument_georeference_coords_and_more'),
]
operations = [
migrations.RunPython(sync_trafficlights_documents, reverse_sync_trafficlights_documents),
]