fix: derive image attachment dimensions during import

This commit is contained in:
2026-08-05 15:43:15 +02:00
parent 4ba41953c1
commit 80469ede34
6 changed files with 94 additions and 6 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ class NetBoxExportConfig(PluginConfig):
name = "netbox_export"
verbose_name = "NetBox-Export"
description = "Portable ZIP export and import for tenants and locations"
version = "0.3.9"
version = "0.3.10"
author = "NetBox Export contributors"
base_url = "netbox-export"
min_version = "4.6.0"
+1 -1
View File
@@ -39,7 +39,7 @@ def export_scope(
"created_at": datetime.now(UTC).isoformat(),
"source_instance": str(InstanceIdentity.local_id()),
"source_netbox_version": getattr(getattr(settings, "RELEASE", None), "version", "4.6"),
"plugin_version": "0.3.9",
"plugin_version": "0.3.10",
"scope": {
"type": scope_type,
"source_pk": str(scope_id),
+21 -3
View File
@@ -10,6 +10,7 @@ from django.apps import apps
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.files.base import ContentFile
from django.core.files.images import get_image_dimensions
from django.db import IntegrityError, models, transaction
from netbox_export.models import ImportedObjectMapping
@@ -462,15 +463,32 @@ def _set_generic_relations(obj, record, resolver, *, allow_deferred: bool):
def _set_files(obj, record, assets, saved_files, *, dry_run: bool):
if dry_run:
return
for name, spec in record.get("files", {}).items():
if not spec or "path" not in spec or spec["path"] not in assets:
continue
filename = spec.get("name", spec["path"]).replace("\\", "/").rsplit("/", 1)[-1]
field = obj._meta.get_field(name)
content = ContentFile(assets[spec["path"]])
dimensions = None
if isinstance(field, models.ImageField):
dimensions = get_image_dimensions(content)
content.seek(0)
width, height = dimensions
if field.width_field and width is not None:
setattr(obj, field.width_field, width)
if field.height_field and height is not None:
setattr(obj, field.height_field, height)
if dry_run:
continue
file_value = getattr(obj, name)
file_value.save(filename, ContentFile(assets[spec["path"]]), save=False)
file_value.save(filename, content, save=False)
saved_files.append((file_value.storage, file_value.name))
if dimensions is not None:
width, height = dimensions
if field.width_field and width is not None:
setattr(obj, field.width_field, width)
if field.height_field and height is not None:
setattr(obj, field.height_field, height)
def _cleanup_files(saved_files):