From 27eac8dc6cf0e96f78edfe4e09845e454fb6d510 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 21 Sep 2023 16:35:51 +0200 Subject: ⚡️ imports: prevent unecessary copy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/data_importer.py | 4 ++-- ishtar_common/models_imports.py | 23 ++++++++++++++++------- ishtar_common/templates/ishtar/import_table.html | 8 ++++---- 3 files changed, 22 insertions(+), 13 deletions(-) (limited to 'ishtar_common') diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 8e3c41e62..45aea5c13 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -830,8 +830,8 @@ class Importer(object): self.concats = set() self.concat_str = {} self.to_be_close = [] - if import_instance and import_instance.imported_images: - self.archive = import_instance.imported_images + if import_instance and import_instance.get_imported_images(): + self.archive = import_instance.get_imported_images() self._defaults = self.DEFAULTS.copy() self._pre_import_values = self.PRE_IMPORT_VALUES.copy() self.history_modifier = history_modifier diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 78398ea1a..ca0a38832 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -1421,12 +1421,16 @@ class BaseImport(models.Model): self.state = state self.save() # only save if no save previously + def get_imported_images(self): + return self.imported_images + def save(self, *args, **kwargs): super().save(*args, **kwargs) if ( self.state == "AC" and not getattr(self, "_archive_pending", False) and not self.archive_file + and not getattr(self, "group", False) ): self._archive() @@ -1618,7 +1622,7 @@ class ImportGroup(BaseImport): f.path = w_filename setattr(current_import, attr, f) current_import.save() - getattr(current_import, attr).name = w_filename + getattr(current_import, attr).name = w_filename[len(settings.MEDIA_ROOT):] current_import.save() os.remove(self.archive_file.path) self.refresh_from_db() @@ -1694,12 +1698,11 @@ class ImportGroup(BaseImport): except FileNotFoundError: pass setattr(sub_import, attr, None) + sub_import.state = "AC" + sub_import.save() self.save() self._archive_pending = False - def archive(self): - super().archive() - self.imports.update(state="AC") def get_all_imported(self): imported = [] @@ -1725,10 +1728,10 @@ class ImportGroup(BaseImport): imported_images.name = self.imported_images.name.split(os.sep)[-1] for import_type_relation in self.importer_type.importer_types.all(): - import_type = import_type_relation.importer_type + importer_type = import_type_relation.importer_type imp = Import.objects.create( name=name, - importer_type=import_type, + importer_type=importer_type, group=self, ) imports.append(imp) @@ -1737,7 +1740,7 @@ class ImportGroup(BaseImport): if imported_file: imp.imported_file = imported_file modified = True - if import_type.archive_required and imported_images: + if importer_type.archive_required and imported_images: imp.imported_images = imported_images modified = True if modified: @@ -1841,6 +1844,12 @@ class Import(BaseImport): TargetKey.objects.filter(associated_import=self, is_set=False).count() ) + def get_imported_images(self): + if not self.group: + return self.imported_images + if self.importer_type.archive_required: + return self.group.imported_images + @property def errors(self): if not self.error_file: diff --git a/ishtar_common/templates/ishtar/import_table.html b/ishtar_common/templates/ishtar/import_table.html index c04248088..3be92d42d 100644 --- a/ishtar_common/templates/ishtar/import_table.html +++ b/ishtar_common/templates/ishtar/import_table.html @@ -78,8 +78,8 @@ {% if import.imported_file %}
  • {% trans "Source" %}
  • - {% if import.imported_images %}
  • - {% trans "Media" %} + {% if import.get_imported_images %}
  • + {% trans "Media" %}
  • {% endif %} {% elif import.archive_file %}
  • {% trans "Archive" context "name" %} @@ -133,8 +133,8 @@ {% if sub.imported_file %}
  • {% trans "Source" %}
  • {% endif %} - {% if sub.imported_images %}
  • - {% trans "Media" %} + {% if sub.get_imported_images %}
  • + {% trans "Media" %}
  • {% endif %} {% if not ARCHIVE_PAGE %}