summaryrefslogtreecommitdiff
path: root/ishtar_common/data_importer.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-18 18:42:42 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2024-04-19 10:16:28 +0200
commit45769bc5b440183dd5e46b40ec2cdd364a3a01db (patch)
treeb5975ad978b4e9ef62b712aa5d2962b0c876db4e /ishtar_common/data_importer.py
parent7e631d9a322a8bb530652e7adac073d18baf326b (diff)
downloadIshtar-45769bc5b440183dd5e46b40ec2cdd364a3a01db.tar.bz2
Ishtar-45769bc5b440183dd5e46b40ec2cdd364a3a01db.zip
✨ imports: allow media import from a web location
Diffstat (limited to 'ishtar_common/data_importer.py')
-rw-r--r--ishtar_common/data_importer.py40
1 files changed, 36 insertions, 4 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py
index 546d29b2d..d16d878e5 100644
--- a/ishtar_common/data_importer.py
+++ b/ishtar_common/data_importer.py
@@ -39,7 +39,12 @@ from django.db.models import Q
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _
-from ishtar_common.utils import get_all_field_names, update_data, get_current_profile
+from ishtar_common.utils import (
+ get_all_field_names,
+ get_current_profile,
+ get_file_from_link,
+ update_data,
+)
NEW_LINE_BREAK = "#####@@@#####"
@@ -620,6 +625,29 @@ class FileFormater(Formater):
value = value.strip()
if not value:
return
+ if isinstance(archive, str) and (archive.startswith("http://") or
+ archive.startswith("https://")):
+ return self._format_url(value, archive)
+ return self._format_zip(value, archive)
+
+ def _format_url(self, value, link):
+ print(link)
+ if not link.endswith("/"):
+ link += "/"
+ full_link = link + value
+ try:
+ filename, tmp_file = get_file_from_link(full_link)
+ except ValueError:
+ raise ValueError(
+ _('"%(full_link)s" is not a valid path')
+ % {"full_link": full_link}
+ )
+ my_file = File(tmp_file, name=filename)
+ # manually set the file size because of an issue with TempFile
+ my_file.size = os.stat(tmp_file.name).st_size
+ return my_file
+
+ def _format_zip(self, value, archive):
zp = zipfile.ZipFile(archive)
value = value.strip().replace("\\", "/")
items = value.replace("/", "_").split(".")
@@ -639,7 +667,7 @@ class FileFormater(Formater):
return my_file
except KeyError:
raise ValueError(
- _('"%(value)s" is not a valid path for the ' "given archive")
+ _('"%(value)s" is not a valid path for the given archive')
% {"value": value}
)
@@ -830,8 +858,12 @@ class Importer(object):
self.concats = set()
self.concat_str = {}
self.to_be_close = []
- if import_instance and import_instance.get_imported_images():
- self.archive = import_instance.get_imported_images()
+ if import_instance:
+ imported_images = import_instance.get_imported_images()
+ if imported_images:
+ self.archive = imported_images
+ elif import_instance.imported_media_link:
+ self.archive = import_instance.imported_media_link
self._defaults = self.DEFAULTS.copy()
self._pre_import_values = self.PRE_IMPORT_VALUES.copy()
self.history_modifier = history_modifier