Skip to content

Commit 0b792cf

Browse files
[Fixes #12732] Asset file migration for documents does not work for d… (#12733)
* [Fixes #12732] Asset file migration for documents does not work for document * [Fixes #12732] Asset file migration for documents does not work for document
1 parent e46f092 commit 0b792cf

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

geonode/assets/management/commands/migrate_file_to_assets.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,19 @@ def handle(self, **options):
9898

9999
logger.info("Moving file to the asset folder")
100100

101-
dest = shutil.move(source, handler._create_asset_dir())
102-
103-
logger.info("Fixing perms")
101+
if len(asset.location) == 1:
102+
# In older installations, all documents are stored in a single folder.
103+
# Instead of moving the entire folder, we can simply move the individual document.
104+
# This approach prevents the risk of breaking the other documents
105+
# that are stored in the same folder
106+
# oldpath = {MEDIA_ROOT}/documents/document/file.extension
107+
dest = shutil.move(asset.location[0], handler._create_asset_dir())
108+
else:
109+
dest = shutil.move(source, handler._create_asset_dir())
110+
111+
logger.info(f"New destination path: {dest}")
112+
113+
logger.info("Fixing file/folder perms if required")
104114
if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
105115
os.chmod(os.path.dirname(dest), settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)
106116

@@ -113,7 +123,10 @@ def handle(self, **options):
113123

114124
logger.info("Updating location field with new folder value")
115125

116-
asset.location = [x.replace(source, dest) for x in asset.location]
126+
if len(asset.location) == 1:
127+
asset.location = dest
128+
else:
129+
asset.location = [x.replace(source, dest) for x in asset.location]
117130
asset.save()
118131

119132
logger.info("Checking if geoserver should be updated")

geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Generated by Django 4.2.9 on 2024-03-12 11:55
2+
import itertools
23
import logging
34
import os
45

@@ -10,7 +11,7 @@
1011

1112
from geonode.base.models import Link
1213
from geonode.assets.models import LocalAsset
13-
from geonode.utils import build_absolute_uri
14+
from geonode.utils import build_absolute_uri, get_supported_datasets_file_types
1415

1516
logger = logging.getLogger(__name__)
1617

@@ -24,9 +25,9 @@ def get_ext(filename):
2425
logger.warning(f"Could not find extension for Resource '{res_hm.title}, file '{filename}': {e}")
2526
return None
2627

27-
ResourceBase_hm = apps.get_model('base', 'ResourceBase')
28-
Dataset_hm = apps.get_model('layers', 'Dataset')
29-
Document_hm = apps.get_model('documents', 'Document')
28+
ResourceBase_hm = apps.get_model("base", "ResourceBase")
29+
Dataset_hm = apps.get_model("layers", "Dataset")
30+
Document_hm = apps.get_model("documents", "Document")
3031

3132
if hasattr(ResourceBase_hm, "files"):
3233
# looping on available resources with files to generate the LocalAssets
@@ -37,12 +38,7 @@ def get_ext(filename):
3738

3839
files = res_hm.files
3940
# creating the local asset object
40-
asset = LocalAsset(
41-
title="Files",
42-
description="Original uploaded files",
43-
owner=owner,
44-
location=files
45-
)
41+
asset = LocalAsset(title="Files", description="Original uploaded files", owner=owner, location=files)
4642
asset.save()
4743

4844
### creating the association between asset and Link
@@ -60,10 +56,14 @@ def get_ext(filename):
6056
ext = get_ext(files[0])
6157
else:
6258
ext = None
59+
supported_file_types = get_supported_datasets_file_types()
6360
for file in files:
64-
for filetype in settings.SUPPORTED_DATASET_FILE_TYPES:
61+
for filetype in supported_file_types:
6562
file_ext = get_ext(file)
66-
if file_ext in filetype["ext"]:
63+
_ext = list(
64+
itertools.chain.from_iterable(y for y in [x["required_ext"] for x in filetype["formats"]])
65+
)
66+
if file_ext in _ext:
6767
ext = filetype["id"]
6868
break
6969
if ext:
@@ -75,14 +75,13 @@ def get_ext(filename):
7575
link_type="uploaded",
7676
name="Original upload",
7777
extension=ext or "unknown",
78-
url=url
78+
url=url,
7979
)
8080

8181

8282
class Migration(migrations.Migration):
8383

8484
dependencies = [
85-
8685
("base", "0091_create_link_asset_alter_link_type"),
8786
]
8887

0 commit comments

Comments
 (0)