From 77aeaefb38898a4667f6bf8b75420cb3c15dd47e Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 21 Nov 2024 11:29:51 +0100 Subject: [PATCH 1/2] [Fixes #12732] Asset file migration for documents does not work for document --- .../commands/migrate_file_to_assets.py | 21 ++++++++++++--- ...2_migrate and_remove_resourcebase_files.py | 27 +++++++++---------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/geonode/assets/management/commands/migrate_file_to_assets.py b/geonode/assets/management/commands/migrate_file_to_assets.py index 1b3297c18f0..8210fd9d301 100644 --- a/geonode/assets/management/commands/migrate_file_to_assets.py +++ b/geonode/assets/management/commands/migrate_file_to_assets.py @@ -98,9 +98,19 @@ def handle(self, **options): logger.info("Moving file to the asset folder") - dest = shutil.move(source, handler._create_asset_dir()) - - logger.info("Fixing perms") + if len(asset.location) == 1: + # In older installations, all documents are stored in a single folder. + # Instead of moving the entire folder, we can simply move the individual document. + # This approach prevents the risk of breaking the other documents + # that are stored in the same folder + # oldpath = {MEDIA_ROOT}/documents/document/file.extension + dest = shutil.move(asset.location[0], handler._create_asset_dir()) + else: + dest = shutil.move(source, handler._create_asset_dir()) + + logger.info(f"New destination path: {dest}") + + logger.info("Fixing file/folder perms if required") if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None: os.chmod(os.path.dirname(dest), settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS) @@ -113,7 +123,10 @@ def handle(self, **options): logger.info("Updating location field with new folder value") - asset.location = [x.replace(source, dest) for x in asset.location] + if len(asset.location) == 1: + asset.location = dest + else: + asset.location = [x.replace(source, dest) for x in asset.location] asset.save() logger.info("Checking if geoserver should be updated") diff --git a/geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py b/geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py index 76b564cd918..ea89c795557 100644 --- a/geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py +++ b/geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py @@ -1,4 +1,5 @@ # Generated by Django 4.2.9 on 2024-03-12 11:55 +import itertools import logging import os @@ -10,7 +11,7 @@ from geonode.base.models import Link from geonode.assets.models import LocalAsset -from geonode.utils import build_absolute_uri +from geonode.utils import build_absolute_uri, get_supported_datasets_file_types logger = logging.getLogger(__name__) @@ -24,9 +25,9 @@ def get_ext(filename): logger.warning(f"Could not find extension for Resource '{res_hm.title}, file '{filename}': {e}") return None - ResourceBase_hm = apps.get_model('base', 'ResourceBase') - Dataset_hm = apps.get_model('layers', 'Dataset') - Document_hm = apps.get_model('documents', 'Document') + ResourceBase_hm = apps.get_model("base", "ResourceBase") + Dataset_hm = apps.get_model("layers", "Dataset") + Document_hm = apps.get_model("documents", "Document") if hasattr(ResourceBase_hm, "files"): # looping on available resources with files to generate the LocalAssets @@ -37,12 +38,7 @@ def get_ext(filename): files = res_hm.files # creating the local asset object - asset = LocalAsset( - title="Files", - description="Original uploaded files", - owner=owner, - location=files - ) + asset = LocalAsset(title="Files", description="Original uploaded files", owner=owner, location=files) asset.save() ### creating the association between asset and Link @@ -60,10 +56,14 @@ def get_ext(filename): ext = get_ext(files[0]) else: ext = None + supported_file_types = get_supported_datasets_file_types() for file in files: - for filetype in settings.SUPPORTED_DATASET_FILE_TYPES: + for filetype in supported_file_types: file_ext = get_ext(file) - if file_ext in filetype["ext"]: + _ext = list( + itertools.chain.from_iterable(y for y in [x["required_ext"] for x in filetype["formats"]]) + ) + if file_ext in _ext: ext = filetype["id"] break if ext: @@ -75,14 +75,13 @@ def get_ext(filename): link_type="uploaded", name="Original upload", extension=ext or "unknown", - url=url + url=url, ) class Migration(migrations.Migration): dependencies = [ - ("base", "0091_create_link_asset_alter_link_type"), ] From cdc8ca68a01fb5d0af6cb64a1e122d7d731d04a5 Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 21 Nov 2024 11:36:09 +0100 Subject: [PATCH 2/2] [Fixes #12732] Asset file migration for documents does not work for document --- .../assets/management/commands/migrate_file_to_assets.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geonode/assets/management/commands/migrate_file_to_assets.py b/geonode/assets/management/commands/migrate_file_to_assets.py index 8210fd9d301..ab75c7ca161 100644 --- a/geonode/assets/management/commands/migrate_file_to_assets.py +++ b/geonode/assets/management/commands/migrate_file_to_assets.py @@ -99,9 +99,9 @@ def handle(self, **options): logger.info("Moving file to the asset folder") if len(asset.location) == 1: - # In older installations, all documents are stored in a single folder. - # Instead of moving the entire folder, we can simply move the individual document. - # This approach prevents the risk of breaking the other documents + # In older installations, all documents are stored in a single folder. + # Instead of moving the entire folder, we can simply move the individual document. + # This approach prevents the risk of breaking the other documents # that are stored in the same folder # oldpath = {MEDIA_ROOT}/documents/document/file.extension dest = shutil.move(asset.location[0], handler._create_asset_dir())