From 51988d217224f0bf97982057663ecb1ac686c390 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 2 Jul 2026 10:18:17 +0000 Subject: [PATCH] Duplicate embedded images when copying notes --- src/backend/InvenTree/InvenTree/models.py | 35 +++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/models.py b/src/backend/InvenTree/InvenTree/models.py index b95e89c1b3..be29bb59d2 100644 --- a/src/backend/InvenTree/InvenTree/models.py +++ b/src/backend/InvenTree/InvenTree/models.py @@ -704,6 +704,10 @@ class InvenTreeNoteMixin(InvenTreePermissionCheckMixin, models.Model): Arguments: other: The other model instance to copy notes from """ + import os + + from django.core.files.base import ContentFile + import common.models content_type = ContentType.objects.get_for_model(self.__class__) @@ -713,14 +717,41 @@ class InvenTreeNoteMixin(InvenTreePermissionCheckMixin, models.Model): source_notes = sorted(other.notes.all(), key=lambda n: n.primary) for source_note in source_notes: - common.models.Note( + new_note = common.models.Note( model_type=content_type, model_id=self.pk, primary=source_note.primary, title=source_note.title, description=source_note.description, content=source_note.content, - ).save() + ) + new_note.save() + + content_updated = False + for img in source_note.images.all(): + if not img.image: + continue + + old_url = img.image.url + filename = os.path.basename(img.image.name) + + try: + img.image.open('rb') + data = img.image.read() + finally: + img.image.close() + + new_img = common.models.NotesImage(note=new_note, user=img.user) + new_img.image.save(filename, ContentFile(data), save=True) + + if old_url in new_note.content: + new_note.content = new_note.content.replace( + old_url, new_img.image.url + ) + content_updated = True + + if content_updated: + new_note.save() @property def primary_note(self):