summaryrefslogtreecommitdiff
path: root/ishtar_common/models_imports.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2023-08-08 17:56:48 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-16 16:38:32 +0200
commitdf7044e23e0123558124a600e5b470ed2d537135 (patch)
treee2c5d2b1b017bc752674c6a6a67ac3843ae7da8d /ishtar_common/models_imports.py
parent6e5740c977ea4eb425b68b5cacb982916a43ba73 (diff)
downloadIshtar-df7044e23e0123558124a600e5b470ed2d537135.tar.bz2
Ishtar-df7044e23e0123558124a600e5b470ed2d537135.zip
✨ Imports groups: archive_required field - automatically create related imports on group creation - adapt import list
Diffstat (limited to 'ishtar_common/models_imports.py')
-rw-r--r--ishtar_common/models_imports.py37
1 files changed, 34 insertions, 3 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py
index 76148a1a6..735ad0b10 100644
--- a/ishtar_common/models_imports.py
+++ b/ishtar_common/models_imports.py
@@ -184,6 +184,7 @@ class ImporterType(models.Model):
)
is_template = models.BooleanField(_("Can be exported"), default=False)
is_import = models.BooleanField(_("Can be import"), default=True)
+ archive_required = models.BooleanField(_("Archive required"), default=False)
unicity_keys = models.CharField(
_('Unicity keys (separator ";")'), blank=True, null=True, max_length=500,
help_text=_("Mandatory for update importer. Set to key that identify items "
@@ -209,7 +210,7 @@ class ImporterType(models.Model):
@property
def type_label(self):
if self.type in IMPORT_TYPES_DICT:
- return IMPORT_TYPES_DICT[self.type]
+ return IMPORT_TYPES_DICT[str(self.type)]
return ""
def get_libreoffice_template(self):
@@ -416,6 +417,8 @@ class ImporterGroupImporter(models.Model):
class Meta:
ordering = ("group", "order")
+ unique_together = ("group", "order")
+ verbose_name = _("Importer - Group <-> Importer")
def get_associated_model(parent_model, keys):
@@ -1255,7 +1258,7 @@ class BaseImport(models.Model):
null=True,
)
imported_images = models.FileField(
- _("Associated images (zip file)"),
+ _("Associated documents (zip file)"),
upload_to="upload/imports/%Y/%m/",
blank=True,
null=True,
@@ -1305,6 +1308,9 @@ class ImportGroup(BaseImport):
verbose_name_plural = _("Import - Groups")
ADMIN_SECTION = _("Imports")
+ def __str__(self):
+ return f"{self.name} ({self.importer_type.name})"
+
@property
def status(self):
if self.state not in IMPORT_GROUP_STATE_DCT:
@@ -1326,7 +1332,7 @@ class ImportGroup(BaseImport):
actions.append(("I", _("Re-import")))
actions.append(("AC", _("Archive")))
if self.state == "AC":
- state = "FE" if self.error_file else "F"
+ state = "FE" if any([1 for imp in self.imports.all() if imp.error_file]) else "F"
actions.append((state, _("Unarchive")))
if self.state in ("C", "A"):
actions.append(("ED", _("Edit")))
@@ -1334,7 +1340,32 @@ class ImportGroup(BaseImport):
return actions
def save(self, *args, **kwargs):
+ add = self._state.adding
super().save(*args, **kwargs)
+ if not add:
+ return
+ name = f"{self.name} ({self.importer_type.name})"
+ for import_type_relation in self.importer_type.importer_types.all():
+ import_type = import_type_relation.importer_type
+ imp = Import.objects.create(
+ name=name,
+ importer_type=import_type,
+ group=self
+ )
+ modified = False
+ # TODO: only get the relevant sheet
+ if self.imported_file:
+ imported_file = ContentFile(self.imported_file.read())
+ imported_file.name = self.imported_file.name
+ imp.imported_file = imported_file
+ modified = True
+ if import_type.archive_required and self.imported_images:
+ imported_image = ContentFile(self.imported_images.read())
+ imported_image.name = self.imported_images.name
+ imp.imported_images = imported_image
+ modified = True
+ if modified:
+ imp.save()
class Import(BaseImport):