Skip to content

Commit

Permalink
[Fixes #12124] Implementation of assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiagiupponi committed Apr 11, 2024
1 parent dd38142 commit a56d39c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 6 additions & 2 deletions geonode/documents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from django.utils.translation import gettext_lazy as _

from geonode.client.hooks import hookset
from geonode.base.models import ResourceBase
from geonode.base.models import Asset, ResourceBase
from geonode.groups.conf import settings as groups_settings
from geonode.documents.enumerations import DOCUMENT_TYPE_MAP, DOCUMENT_MIMETYPE_MAP
from geonode.security.permissions import VIEW_PERMISSIONS, OWNER_PERMISSIONS, DOWNLOAD_PERMISSIONS
Expand Down Expand Up @@ -76,6 +76,10 @@ def compact_permission_labels(cls):
"owner": _("Owner"),
}

@property
def files(self):
return Asset.objects.filter(link__resource=self).first()

@property
def name(self):
if not self.title:
Expand Down Expand Up @@ -107,7 +111,7 @@ def download_is_ajax_safe(self):

@property
def is_file(self):
return self.files and self.extension
return self.files is not None and self.extension is not None

@property
def mime_type(self):
Expand Down
9 changes: 7 additions & 2 deletions geonode/documents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# Standard Modules
import os
import logging
from geonode.base.models import Asset
from geonode.storage.manager import storage_manager

# Django functionality
Expand Down Expand Up @@ -78,9 +79,13 @@ def get_download_response(request, docid, attachment=False):
register_event(request, EventType.EVENT_DOWNLOAD, document)
filename = slugify(os.path.splitext(os.path.basename(document.title))[0])

if document.files and storage_manager.exists(document.files[0]):
resource_asset = Asset.objects.filter(link__resource=document.pk).first()

manager = resource_asset.get_storage_manager() or storage_manager

if resource_asset and manager.exists(resource_asset.location[0]):
return DownloadResponse(
storage_manager.open(document.files[0]).file,
manager.open(resource_asset.location[0]).file,
basename=f"{filename}.{document.extension}",
attachment=attachment,
)
Expand Down

0 comments on commit a56d39c

Please sign in to comment.