def package_show(context, data_dict): class DatasetType: (specific_version, latest_version, unversioned) = range(3) # The parent dataset is private so it doesn't appear in the lists # but we want to override the authentication checks so we can # access the child datasets that represent the different versions ignore_auth = context.get('ignore_auth') context['ignore_auth'] = True # Get the dataset we actually asked for requested_dataset = ckan_package_show(context, data_dict) version_to_display = requested_dataset parent_names = _get_parent_dataset_names( get_context(context), requested_dataset['id']) if len(parent_names) > 0: base_name = parent_names[0] dataset_type = DatasetType.specific_version all_version_names = _get_child_dataset_names( get_context(context), base_name) else: # Requesting the latest version or an unversioned dataset base_name = requested_dataset['name'] all_version_names = _get_child_dataset_names( get_context(context), base_name) if len(all_version_names) > 0: dataset_type = DatasetType.latest_version else: dataset_type = DatasetType.unversioned all_active_versions = _get_ordered_active_dataset_versions( get_context(context), data_dict.copy(), # Will get modified so make a copy all_version_names) # Show the most recent, public active version if dataset_type == DatasetType.latest_version and \ len(all_active_versions) > 0: version_to_display = all_active_versions[0] if dataset_type in (DatasetType.unversioned, DatasetType.specific_version): # Do default CKAN authentication context['ignore_auth'] = ignore_auth logic.check_access('package_show', get_context(context), data_dict) version_to_display['_versions'] = _get_version_names_and_urls( all_active_versions, base_name) # Reindexing fails if we don't do this # Later versions of CKAN will not include these in the package # See https://github.com/ckan/ckan/issues/3114 version_to_display.pop('relationships_as_subject', False) version_to_display.pop('relationships_as_object', False) return version_to_display
def _get_or_create_parent_dataset(context, data_dict): try: dataset = ckan_package_show( get_context(context), {'id': data_dict['name']}) except (logic.NotFound): dataset = toolkit.get_action('package_create')( get_context(context), data_dict) return dataset
def _get_or_create_parent_dataset(context, data_dict): try: dataset = ckan_package_show(get_context(context), {'id': data_dict['name']}) except (logic.NotFound): dataset = toolkit.get_action('package_create')(get_context(context), data_dict) return dataset
def dataset_version_create(context, data_dict): id = data_dict.get('id') parent_name = data_dict.get('base_name') owner_org = data_dict.get('owner_org') parent_dict = { 'name': parent_name, } if owner_org: parent_dict['owner_org'] = owner_org parent_dict['private'] = True else: parent_dict['private'] = False parent = _get_or_create_parent_dataset( context, parent_dict ) toolkit.get_action('package_relationship_create')( get_context(context), { 'subject': id, 'object': parent['id'], 'type': 'child_of', } )
def dataset_version_create(context, data_dict): id = data_dict.get('id') parent_name = data_dict.get('base_name') owner_org = data_dict.get('owner_org') parent_dict = { 'name': parent_name, } if owner_org: parent_dict['owner_org'] = owner_org parent_dict['private'] = True else: parent_dict['private'] = False parent = _get_or_create_parent_dataset(context, parent_dict) toolkit.get_action('package_relationship_create')(get_context(context), { 'subject': id, 'object': parent['id'], 'type': 'child_of', })