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 #12124] GNIP 100: Assets #12179

Closed
wants to merge 24 commits into from
Closed

[Fixes #12124] GNIP 100: Assets #12179

wants to merge 24 commits into from

Conversation

etj
Copy link
Contributor

@etj etj commented Apr 22, 2024

First implementation of assets.

Also these changes are needed: GeoNode/geonode-importer#235

Checklist

Reviewing is a process done by project maintainers, mostly on a volunteer basis. We try to keep the overhead as small as possible and appreciate if you help us to do so by completing the following items. Feel free to ask in a comment if you have troubles with any of them.

For all pull requests:

  • Confirm you have read the contribution guidelines
  • You have sent a Contribution Licence Agreement (CLA) as necessary (not required for small changes, e.g., fixing typos in the documentation)
  • Make sure the first PR targets the master branch, eventual backports will be managed later. This can be ignored if the PR is fixing an issue that only happens in a specific branch, but not in newer ones.

The following are required only for core and extension modules (they are welcomed, but not required, for contrib modules):

  • There is a ticket in https://github.com/GeoNode/geonode/issues describing the issue/improvement/feature (a notable exemption is, changes not visible to end-users)
  • The issue connected to the PR must have Labels and Milestone assigned
  • PR for bug fixes and small new features are presented as a single commit
  • Commit message must be in the form "[Fixes #<issue_number>] Title of the Issue"
  • New unit tests have been added covering the changes, unless there is an explanation on why the tests are not necessary/implemented
  • This PR passes all existing unit tests (test results will be reported by travis-ci after opening this PR)
  • This PR passes the QA checks: black geonode && flake8 geonode
  • Commits changing the settings, UI, existing user workflows, or adding new functionality, need to include documentation updates
  • Commits adding new texts do use gettext and have updated .po / .mo files (without location infos)

Submitting the PR does not require you to check all items, but by the time it gets merged, they should be either satisfied or inapplicable.

@cla-bot cla-bot bot added the cla-signed CLA Bot: community license agreement signed label Apr 22, 2024
@etj etj force-pushed the 12124_assets branch 3 times, most recently from ef0d851 to c04e13a Compare April 23, 2024 11:40
return HttpResponse(status=401)


def get_default_asset(resource: ResourceBase) -> Asset or None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this. i suggest to call it get_resource_asset otherwise i would expect to have a the value of DEFAULT_ASSET_HANDLER

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns an Asset, not an AssetHandler.

The first one is the default default bc at the moment we have only a single Asset.
When we'll have multiple assets per resource there will be a way to choose the default one (highest priority?, a pluggable selector?)
get_resource_asset would imply there is only one asset per resource

geonode/base/models.py Outdated Show resolved Hide resolved
geonode/base/models.py Outdated Show resolved Hide resolved
asset_handler_registry.get_handler(instance).remove_data(instance)


signals.post_delete.connect(cleanup_asset_data, sender=LocalAsset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you connect the signal to "Asset" it should work without defining (in the future) one for each asset

signals.post_delete.connect(cleanup_asset_data, sender=Asset)

if self.resource_type == "dataset":
allowed_file = select_relevant_files(get_allowed_extensions(), self.files)
from geonode.assets.utils import get_default_asset
from geonode.geoserver.helpers import select_relevant_files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just move on top this imports? This API si widely called. Having this here means re-import them everytime. They are cached, yes but part of them can be still re-imported:
https://stackoverflow.com/a/46662587/7597536

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would create a circular reference error

if file:
manager = StorageManager(remote_files={"base_file": file})
manager.clone_remote_files()
create_asset_and_link(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of cloning the remote files and then copy them again in the settings.ASSET_ROOT, whey dont we improve the clone_remote_files by giving a destination path? In this way we can avoid to perform 3 operations on the file.
In geonode deployment with a huge quanity of document, this file system work can be an issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keeping the current behaviour so far

geonode/documents/tasks.py Outdated Show resolved Hide resolved
links = self._copy_data(instance, target=_resource)
files = []
for link in links:
files.extend(link.asset.location)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant' we just return the location from the _copy_data function instead of the whole object? If yes, this for is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding as comment

# 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


def _get_file(self, request, pk, attachment: bool):
asset = get_object_or_404(Asset, pk=pk)
if bad_response := get_perms_response(request, asset):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be replaced by the IsOwnerOrAdmin permission class? If yes, let's improve the perms class (if needed)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope: assets have their own permission semantic:
As described in the issue, step 0 is "visibile if owner or admin, or if user has read access on at least one resource attached to the asset"
Furthermore, we may want to make permission also more complex, for instance including group admin or permission directly attached to the Asset. The get_perms_response will encapsulate such logic.

@etj etj force-pushed the 12124_assets branch 4 times, most recently from ce319e1 to 49d489b Compare April 24, 2024 14:19
Copy link

codecov bot commented May 7, 2024

Codecov Report

Attention: Patch coverage is 80.08721% with 137 lines in your changes are missing coverage. Please review.

Project coverage is 64.18%. Comparing base (3454ae5) to head (da90a2a).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12179      +/-   ##
==========================================
+ Coverage   64.03%   64.18%   +0.14%     
==========================================
  Files         870      882      +12     
  Lines       52221    52788     +567     
  Branches     6487     6530      +43     
==========================================
+ Hits        33438    33880     +442     
- Misses      17281    17400     +119     
- Partials     1502     1508       +6     

@etj etj linked an issue May 10, 2024 that may be closed by this pull request
5 tasks
@etj etj mentioned this pull request May 13, 2024
12 tasks
@etj etj mentioned this pull request May 23, 2024
12 tasks
@mattiagiupponi
Copy link
Contributor

closed in favor of #12338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed CLA Bot: community license agreement signed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GNIP 100: Assets
2 participants