loko/streetup/documents/signals.py
2026-07-22 14:48:40 +02:00

44 lines
No EOL
1.9 KiB
Python

from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.contrib.contenttypes.models import ContentType
from .models import ManagedDocument, DocumentVersion, DocumentAttachment
@receiver(post_save, sender=DocumentVersion)
def document_version_post_save(sender, instance, created, **kwargs):
"""
If a new version of a DXF file is saved and it is attached to an intersection,
automatically generate/update the overlay PNG.
"""
if instance.file and instance.file.name.lower().endswith('.dxf'):
try:
from assets.models import TrafficLightIntersection
from assets.views.trafficlights import generate_dxf_overlay_managed_document
ct_tli = ContentType.objects.get_for_model(TrafficLightIntersection)
attachment = DocumentAttachment.objects.filter(
document=instance.document,
content_type=ct_tli
).first()
if attachment:
generate_dxf_overlay_managed_document(instance.document, user=instance.uploaded_by)
except Exception:
pass
@receiver(post_save, sender=DocumentAttachment)
def document_attachment_post_save(sender, instance, created, **kwargs):
"""
If a document is attached to an intersection and it is a DXF file,
automatically generate/update the overlay PNG.
"""
try:
from assets.models import TrafficLightIntersection
from assets.views.trafficlights import generate_dxf_overlay_managed_document
if isinstance(instance.content_object, TrafficLightIntersection):
document = instance.document
latest_version = document.latest_version
if latest_version and latest_version.file and latest_version.file.name.lower().endswith('.dxf'):
generate_dxf_overlay_managed_document(document, user=instance.attached_by)
except Exception:
pass