Skip to content

Commit

Permalink
ref: duplicate definition and minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 25, 2024
1 parent ff7fd73 commit 5682efb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
31 changes: 24 additions & 7 deletions ckanext/dcor_schemas/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@
import dclab
from dcor_shared import (
DC_MIME_TYPES, get_resource_path, get_dc_instance, s3, s3cc, sha256sum,
wait_for_resource
wait_for_resource, get_ckan_config_option
)


def admin_context():
return {'ignore_auth': True, 'user': 'default'}


def get_base_metadata(resource):
res_dict_base = {}
if not resource.get("mimetype"):
suffix = "." + resource["name"].rsplit(".", 1)[-1]
for mt in DC_MIME_TYPES:
if suffix in DC_MIME_TYPES[mt]:
res_dict_base["mimetype"] = mt
break

# Also make sure the resource has "url" defined.
if not resource.get("url"):
site_url = get_ckan_config_option("ckan.site_url")
meta_url = (f"{site_url}"
f"/dataset/{resource['package_id']}"
f"/resource/{resource['id']}"
f"/download/{resource['name'].lower()}")
res_dict_base["url"] = meta_url
return res_dict_base


def patch_resource_noauth(package_id, resource_id, data_dict):
"""Patch a resource using package_revise"""
package_revise = logic.get_action("package_revise")
Expand All @@ -28,20 +48,17 @@ def set_resource_meta_base_data(resource):
run before. So it makes sense to perform any additional calls
to `package_revise` in those background jobs.
"""
data_dict = {}
if "url" in resource:
data_dict["url"] = resource["url"]
if "mimetype" in resource:
data_dict["mimetype"] = resource["mimetype"]
res_dict_base = get_base_metadata(resource)
patch_resource_noauth(
package_id=resource["package_id"],
resource_id=resource["id"],
data_dict=data_dict)
data_dict=res_dict_base)
return True


def set_dc_config_job(resource):
"""Store all DC config metadata"""
resource.update(get_base_metadata(resource))
if (resource.get('mimetype') in DC_MIME_TYPES
and resource.get("dc:setup:channel width", None) is None):
rid = resource["id"]
Expand Down
49 changes: 13 additions & 36 deletions ckanext/dcor_schemas/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,33 +403,23 @@ def get_user_dataset_labels(self, user_obj):

# IResourceController
def before_resource_create(self, context, resource):
"""Ran before creating the resource; changes will be in package dict"""
res_data_dict = {
"last_modified": datetime.datetime.now(datetime.timezone.utc)
}

if not resource.get("mimetype"):
suffix = "." + resource["name"].rsplit(".", 1)[-1]
for mt in DC_MIME_TYPES:
if suffix in DC_MIME_TYPES[mt]:
res_data_dict["mimetype"] = mt
break

# Also make sure the resource has "url" defined.
if not resource.get("url"):
site_url = get_ckan_config_option("ckan.site_url")
meta_url = (f"{site_url}"
f"/dataset/{resource['package_id']}"
f"/resource/{resource['id']}"
f"/download/{resource['name'].lower()}")
res_data_dict["url"] = meta_url

resource.update(res_data_dict)
if "upload" in resource:
# set/override the filename
upload = resource["upload"]
if hasattr(upload, "filename"):
filename = upload.filename
elif hasattr(upload, "name"):
filename = pathlib.Path(upload.name).name
else:
raise ValueError(
f"Could not determine filename for {resource}")
resource["name"] = filename
resource.update(jobs.get_base_metadata(resource))

def after_resource_create(self, context, resource):
"""Add custom jobs"""
# Make sure mimetype etc. are set properly
self.before_resource_create(context, resource)
resource.update(jobs.get_base_metadata(resource))

# Make sure the resource has a mimetype if possible. This is a
# workaround for data uploaded via S3.
Expand Down Expand Up @@ -539,19 +529,6 @@ def after_resource_create(self, context, resource):
datapreview.add_views_to_resource(context={"ignore_auth": True},
resource_dict=resource)

def before_resource_create(self, context, resource):
if "upload" in resource:
# set/override the filename
upload = resource["upload"]
if hasattr(upload, "filename"):
filename = upload.filename
elif hasattr(upload, "name"):
filename = pathlib.Path(upload.name).name
else:
raise ValueError(
f"Could not determine filename for {resource}")
resource["name"] = filename

# ITemplateHelpers
def get_helpers(self):
# Template helper function names should begin with the name of the
Expand Down

0 comments on commit 5682efb

Please sign in to comment.