Skip to content

Commit

Permalink
Generate sample name based on sample number for single case #201
Browse files Browse the repository at this point in the history
Add related resource to package when parent is selected #202
- Added logic to find the highest related resource index and append new related resource
  • Loading branch information
NTaherifar committed Jun 5, 2024
1 parent cb2dd91 commit 15f5f85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
44 changes: 42 additions & 2 deletions ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import ckan.lib.plugins as lib_plugins
from ckan.logic.validators import owner_org_validator as default_owner_org_validator
import logging

from pprint import pformat
import re

@tk.side_effect_free
def igsn_theme_get_sum(context, data_dict):
Expand Down Expand Up @@ -32,6 +33,9 @@ def organization_list_for_user(next_action, context, data_dict):

@tk.chained_action
def package_create(next_action, context, data_dict):
# logger = logging.getLogger(__name__)
# logger.info("data_dict: %s", pformat(data_dict))

package_type = data_dict.get('type')
package_plugin = lib_plugins.lookup_package_plugin(package_type)
if 'schema' in context:
Expand All @@ -46,10 +50,46 @@ def package_create(next_action, context, data_dict):
]

data_dict['private'] = False

data_dict['name'] = generate_sample_name(data_dict)
generate_parent_related_resource(data_dict)
created_package = next_action(context, data_dict)
return created_package

def generate_sample_name(data_dict):

owner_org=data_dict['owner_org']
material_type = data_dict['material_type']
sample_type = data_dict['sample_type']
sample_number = data_dict['sample_number']
org_name= tk.get_action('organization_show')({}, {'id': owner_org})['name']
org_name = org_name.replace(' ', '_')
material_type = material_type.replace(' ', '_')
sample_type = sample_type.replace(' ', '_')
sample_number = sample_number.replace(' ', '_')

name = f"{org_name}-{material_type}-Sample-{sample_type}-{sample_number}"
name = re.sub(r'[^a-z0-9-_]', '', name.lower())
return name

def generate_parent_related_resource(data_dict):
parent_id = data_dict.get('parent')
if parent_id:
parent = tk.get_action('package_show')({}, {'id': parent_id})
highest_index = -1
for key in data_dict.keys():
if key.startswith('related_resource-'):
parts = key.split('-')
if len(parts) > 1 and parts[1].isdigit():
index = int(parts[1])
if index > highest_index:
highest_index = index

new_index = highest_index + 1

data_dict[f'related_resource-{new_index}-related_resource_type'] = "PhysicalObject"
data_dict[f'related_resource-{new_index}-related_resource_url'] = parent.get('doi')
data_dict[f'related_resource-{new_index}-related_resource_title'] = parent.get('title')
data_dict[f'related_resource-{new_index}-relation_type'] = "IsDerivedFrom"

# We do not need user_create customization here.
# Users do not need to be a part of an organization by default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ dataset_fields:
help_text: Persistent identifier (DOI) of the physical sample
groupBy: About Sample

- field_name: title
label: Title
required: true
preset: title
help_text: "The title should include appropriate elements that would help find and distinguish a physical sample. The exact syntax is at the discretion of the sample provider. Appropriate elements might include: (a) The basic form of the object that is registered. For example, polished section, core, pulp, solution, dredge haul in a box, lot, or piece of material. (b) The material or materials that compose the sample. For example, water, granite, or tissue. (c) Local sample identifiers"
groupBy: About Sample
# - field_name: title
# label: Title
# required: true
# preset: title
# help_text: "The title should include appropriate elements that would help find and distinguish a physical sample. The exact syntax is at the discretion of the sample provider. Appropriate elements might include: (a) The basic form of the object that is registered. For example, polished section, core, pulp, solution, dredge haul in a box, lot, or piece of material. (b) The material or materials that compose the sample. For example, water, granite, or tissue. (c) Local sample identifiers"
# groupBy: About Sample

- field_name: name
label: URL
preset: dataset_slug
form_placeholder: eg. my-dataset
form_snippet: hidden_field.html
form_placeholder: eg. my-dataset
groupBy: About Sample

- field_name: sample_number
label: Sample Number
required: true
help_text: ""
groupBy: About Sample

- field_name: parent
Expand Down

0 comments on commit 15f5f85

Please sign in to comment.