Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #12732] Asset file migration for documents does not work for d… #12733

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions geonode/assets/management/commands/migrate_file_to_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@

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())

Check warning on line 107 in geonode/assets/management/commands/migrate_file_to_assets.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/management/commands/migrate_file_to_assets.py#L107

Added line #L107 was not covered by tests
else:
dest = shutil.move(source, handler._create_asset_dir())

Check warning on line 109 in geonode/assets/management/commands/migrate_file_to_assets.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/management/commands/migrate_file_to_assets.py#L109

Added line #L109 was not covered by tests

logger.info(f"New destination path: {dest}")

Check warning on line 111 in geonode/assets/management/commands/migrate_file_to_assets.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/management/commands/migrate_file_to_assets.py#L111

Added line #L111 was not covered by tests

logger.info("Fixing file/folder perms if required")

Check warning on line 113 in geonode/assets/management/commands/migrate_file_to_assets.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/management/commands/migrate_file_to_assets.py#L113

Added line #L113 was not covered by tests
if settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS is not None:
os.chmod(os.path.dirname(dest), settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)

Expand All @@ -113,7 +123,10 @@

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

Check warning on line 127 in geonode/assets/management/commands/migrate_file_to_assets.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/management/commands/migrate_file_to_assets.py#L127

Added line #L127 was not covered by tests
else:
asset.location = [x.replace(source, dest) for x in asset.location]
asset.save()

logger.info("Checking if geoserver should be updated")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by Django 4.2.9 on 2024-03-12 11:55
import itertools
import logging
import os

Expand All @@ -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__)

Expand All @@ -24,9 +25,9 @@
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
Expand All @@ -37,12 +38,7 @@

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)

Check warning on line 41 in geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py

View check run for this annotation

Codecov / codecov/patch

geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py#L41

Added line #L41 was not covered by tests
asset.save()

### creating the association between asset and Link
Expand All @@ -60,10 +56,14 @@
ext = get_ext(files[0])
else:
ext = None
supported_file_types = get_supported_datasets_file_types()

Check warning on line 59 in geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py

View check run for this annotation

Codecov / codecov/patch

geonode/base/migrations/0092_migrate and_remove_resourcebase_files.py#L59

Added line #L59 was not covered by tests
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:
Expand All @@ -75,14 +75,13 @@
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"),
]

Expand Down
Loading