diff options
Diffstat (limited to 'ishtar_common/models_imports.py')
-rw-r--r-- | ishtar_common/models_imports.py | 71 |
1 files changed, 43 insertions, 28 deletions
diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 0157b33ee..23fb4f38e 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -100,6 +100,7 @@ from ishtar_common.data_importer import ( LowerCaseFormater, ) from ishtar_common.utils import task +from ishtar_common.views_item import get_item from ishtar_common.ignf_utils import IGNF @@ -3052,8 +3053,8 @@ class MediaExporter(models.Model): query = models.TextField( _("Filter query"), blank=True, null=False, default="", help_text=_( - "Use 'text' query used in Ishtar search input. Can be left empty " - "to export all." + "Use 'text' query used in Ishtar search input. The query is from the " + "document point of view. Can be left empty to export all attached media." ) ) naming = models.TextField( @@ -3128,13 +3129,7 @@ class MediaExporter(models.Model): down += list(getattr(o, attr).all()) return down - def export(self, obj, tmpdir=None): - if not tmpdir: - tmpdir = tempfile.mkdtemp() - if not hasattr(obj, "documents"): - # database inconstency - should not occur - return - q = None + def _get_query(self, ishtaruser): media_attrs = [] if self.files_to_export in ("A", "I"): q = Q(image__isnull=False) @@ -3149,24 +3144,18 @@ class MediaExporter(models.Model): else: q = q2 media_attrs.append("associated_file") - archive_path = os.path.join(tmpdir, "archive") - os.mkdir(archive_path) - items = [obj] - if self.cascade: - down = self._get_down_objects([obj]) - items += down - while down: - down = self._get_down_objects(down) - items += down - counters = {} - for item in items: - if not hasattr(item, "documents"): - continue - self._copy_media(item, q, media_attrs, counters, archive_path) - now = datetime.datetime.now() - archive_name = os.path.join(tmpdir, f"media-{now.strftime('%Y-%m-%d-%H%M%S')}") - shutil.make_archive(archive_name, "zip", archive_path) - return archive_name + ".zip" + # if query is empty do also the query in order to filter with permissions + Document = apps.get_model("ishtar_common", "Document") + _get_item = get_item( + Document, + "", "", no_permission_check=True, + ) + query = {"search_vector": self.query or ""} + ids = _get_item( + None, return_query=True, ishtaruser=ishtaruser, query=query + ).values_list("id", flat=True) + q = q & Q(pk__in=ids) + return q, media_attrs def _copy_media(self, item, q, media_attrs, counters, archive_path): item_type = item.SLUG @@ -3189,5 +3178,31 @@ class MediaExporter(models.Model): if key not in counters: counters[key] = 0 counters[key] += 1 - name += f"_{idx + counters[key]:05d}.{ext}" + name += f"_{counters[key]:05d}.{ext}" shutil.copy(media.path, os.path.join(archive_path, name)) + + def export(self, obj, ishtaruser, tmpdir=None): + if not tmpdir: + tmpdir = tempfile.mkdtemp() + if not hasattr(obj, "documents"): + # database inconstency - should not occur + return + q, media_attrs = self._get_query(ishtaruser) + archive_path = os.path.join(tmpdir, "archive") + os.mkdir(archive_path) + items = [obj] + if self.cascade: + down = self._get_down_objects([obj]) + items += down + while down: + down = self._get_down_objects(down) + items += down + counters = {} + for item in items: + if not hasattr(item, "documents"): + continue + self._copy_media(item, q, media_attrs, counters, archive_path) + now = datetime.datetime.now() + archive_name = os.path.join(tmpdir, f"media-{now.strftime('%Y-%m-%d-%H%M%S')}") + shutil.make_archive(archive_name, "zip", archive_path) + return archive_name + ".zip" |