From 15f5f8543d6e3bf7d4a06afef75c160ddcc3f252 Mon Sep 17 00:00:00 2001 From: Neda Taherifar Date: Wed, 5 Jun 2024 14:12:45 +0800 Subject: [PATCH] Generate sample name based on sample number for single case #201 Add related resource to package when parent is selected #202 - Added logic to find the highest related resource index and append new related resource --- .../ckanext/igsn_theme/logic/action.py | 44 ++++++++++++++++++- .../igsn_theme/sample_repository_schema.yaml | 21 ++++++--- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/logic/action.py b/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/logic/action.py index d7bb1af4..41f0c0c9 100644 --- a/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/logic/action.py +++ b/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/logic/action.py @@ -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): @@ -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: @@ -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. diff --git a/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/sample_repository_schema.yaml b/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/sample_repository_schema.yaml index 965e6fb1..c60564f0 100644 --- a/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/sample_repository_schema.yaml +++ b/ckan/src/ckanext-igsn-theme/ckanext/igsn_theme/sample_repository_schema.yaml @@ -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