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

[QOL-8943] look up package ID from resource ID #84

Merged
merged 3 commits into from
Jun 9, 2022
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: 21 additions & 0 deletions ckanext/s3filestore/tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,27 @@ def test_resource_upload(self):
data = obj['Body'].read()
assert_equal(data, io.open(file_path, 'rb').read())

def test_package_update(self):
''' Test a typical package_update API call.
'''
dataset = self._test_dataset()
test_resource = self._upload_test_resource(dataset)
pkg_dict = helpers.call_action('package_show', id=dataset['id'])
# package_update calls won't necessarily provide package ID
# on each resource
for resource in pkg_dict['resources']:
resource['description'] = 'updated description'
del resource['package_id']

helpers.call_action(
'package_update',
context={'user': self.sysadmin['name']},
**pkg_dict
)
assert helpers.call_action(
'resource_show', id=test_resource['id']
)['description'] == 'updated description'

def test_uploader_get_path(self):
'''Uploader get_path returns as expected'''
dataset = factories.Dataset()
Expand Down
15 changes: 12 additions & 3 deletions ckanext/s3filestore/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ def __init__(self, resource):
self.old_filename = old_resource.url
resource['url_type'] = ''

def _get_package(self, resource_id=None):
context = {'ignore_auth': True}
if resource_id:
resource = toolkit.get_action('resource_show')(
context=context, data_dict={'id': resource_id})
else:
resource = self.resource
return toolkit.get_action('package_show')(
context=context, data_dict={'id': resource.get('package_id')})

def get_path(self, id, filename=None):
'''Return the key used for this resource in S3.

Expand All @@ -582,7 +592,7 @@ def get_path(self, id, filename=None):

def _get_target_acl(self, resource_id):
if self.acl == 'auto':
package = toolkit.get_action('package_show')({'ignore_auth': True}, {'id': self.resource['package_id']})
package = self._get_package(resource_id)
return PRIVATE_ACL if package['private'] else PUBLIC_ACL
else:
return self.acl
Expand Down Expand Up @@ -661,8 +671,7 @@ def _get_resource_metadata(self):
to be added to the S3 object.
'''
username = g.user if 'user' in dir(g) else '__anonymous__'
package = toolkit.get_action('package_show')(
context={'ignore_auth': True}, data_dict={'id': self.resource['package_id']})
package = self._get_package()
metadata = {
'package_' + field: ensure_ascii(package[field]) for field in package.keys()
if field != 'notes' and isinstance(package[field], six.string_types)
Expand Down