Skip to content

Commit

Permalink
[Fixes #12124] GNIP 100: Assets - improvements in clone and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
etj committed May 7, 2024
1 parent c50a11c commit 5806872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 39 deletions.
13 changes: 13 additions & 0 deletions geonode/assets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ def create_asset_and_link_dict(resource, values: dict, clone_files=True):
)


def copy_assets_and_links(resource, target=None) -> list:
assets_and_links = []
links_with_assets = Link.objects.filter(resource=resource, asset__isnull=False).prefetch_related("asset")

for link in links_with_assets:
link.asset = asset_handler_registry.get_handler(link.asset).clone(link.asset)
link.pk = None
link.resource = target
link.save()
assets_and_links.append((link.asset, link))
return assets_and_links


def rollback_asset_and_link(asset, link):
try:

Check warning on line 131 in geonode/assets/utils.py

View check run for this annotation

Codecov / codecov/patch

geonode/assets/utils.py#L131

Added line #L131 was not covered by tests
if link:
Expand Down
23 changes: 3 additions & 20 deletions geonode/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,26 +610,9 @@ def cleanup_uploaded_files(resource_id):

if ResourceBase.objects.filter(id=resource_id).exists():
_resource = ResourceBase.objects.filter(id=resource_id).get()
_uploaded_folder = None
asset = get_default_asset(_resource)
files = asset.location if asset else []
if files:
for _file in _resource.files:
try:
if storage_manager.exists(_file):
if not _uploaded_folder:
_uploaded_folder = os.path.split(storage_manager.path(_file))[0]
storage_manager.delete(_file)
except Exception as e:
logger.warning(e)
try:
if _uploaded_folder and storage_manager.exists(_uploaded_folder):
storage_manager.delete(_uploaded_folder)
except Exception as e:
logger.warning(e)

# Do we want to delete the files also from the resource?
ResourceBase.objects.filter(id=resource_id).update(files={})
asset = get_default_asset(_resource) # TODO: make sure to select the proper "uploaded" asset
if asset:
asset.delete()

# Remove generated thumbnails, if any
filename = f"{_resource.get_real_instance().resource_type}-{_resource.get_real_instance().uuid}"
Expand Down
25 changes: 6 additions & 19 deletions geonode/resource/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

from . import settings as rm_settings
from .utils import update_resource, resourcebase_post_save
from geonode.assets.utils import create_asset_and_link_dict, rollback_asset_and_link
from geonode.assets.utils import create_asset_and_link_dict, rollback_asset_and_link, copy_assets_and_links
from geonode.assets.handlers import asset_handler_registry

from ..base import enumerations
Expand Down Expand Up @@ -500,18 +500,6 @@ def ingest(
instance.clear_dirty_state()
return instance

def _copy_data(self, resource, target=None) -> list[Link]:
links = []
links_with_assets = Link.objects.filter(resource=resource, asset__isnull=False).prefetch_related("asset")

for link in links_with_assets:
link.asset = asset_handler_registry.get_handler(link.asset).clone(link.asset)
link.pk = None
link.resource = target
link.save()
links.append(link)
return links

def copy(
self, instance: ResourceBase, /, uuid: str = None, owner: settings.AUTH_USER_MODEL = None, defaults: dict = {}
) -> ResourceBase:
Expand Down Expand Up @@ -555,15 +543,14 @@ def copy(
_maplayer.map = _resource.get_real_instance()
_maplayer.save()

links = self._copy_data(instance, target=_resource)
assets_and_links = copy_assets_and_links(instance, target=_resource)
# we're just merging all the files together: it won't work once we have multiple assets per resource
# TODO: get the files from the proper Asset
# TODO: get the files from the proper Asset, or make the _concrete_resource_manager.copy use assets
to_update = {}

if not isinstance(instance.get_real_instance(), (Map, GeoApp)):
files = list(itertools.chain.from_iterable([link.asset.location for link in links]))
if files:
to_update = {"files": files}
files = list(itertools.chain.from_iterable([asset.location for asset, _ in assets_and_links]))
if files:
to_update = {"files": files}

_resource = self._concrete_resource_manager.copy(instance, uuid=_resource.uuid, defaults=to_update)

Expand Down

0 comments on commit 5806872

Please sign in to comment.