def after_update(self, context, pkg_dict): """ Dataset has been created / updated Check status of the dataset to determine if we should publish DOI to datacite network @param pkg_dict: @return: pkg_dict """ # Is this active and public? If so we need to make sure we have an active DOI if pkg_dict.get('state', 'active') == 'active' and not pkg_dict.get('private', False): package_id = pkg_dict['id'] # Load the original package, so we can determine if user has changed any fields orig_pkg_dict = get_action('package_show')(context, {'id': package_id}) # Metadata created isn't populated in pkg_dict - so copy from the original pkg_dict['metadata_created'] = orig_pkg_dict['metadata_created'] # Load the local DOI doi = get_doi(package_id) # If we don't have a DOI, create one # This could happen if the DOI module is enabled after a dataset has been creates if not doi: doi = create_unique_identifier(package_id) # Build the metadata dict to pass to DataCite service metadata_dict = self.build_metadata(pkg_dict, doi) # Perform some basic checks against the data - we require at the very least # title and author fields - they're mandatory in the DataCite Schema # This will only be an issue if another plugin has removed a mandatory field self.validate_metadata(metadata_dict) # Is this an existing DOI? Update it if doi.published: # Before updating, check if any of the metadata has been changed - otherwise # We end up sending loads of revisions to DataCite for minor edits # Load the current version orig_metadata_dict = self.build_metadata(orig_pkg_dict, doi) # Check if the two dictionaries are the same if cmp(orig_metadata_dict, metadata_dict) != 0: # Not the same, so we want to update the metadata update_doi(package_id, **metadata_dict) h.flash_success('DataCite DOI metadata updated') # TODO: If editing a dataset older than 5 days, create DOI revision # New DOI - publish to datacite else: h.flash_success('DataCite DOI created') publish_doi(package_id, **metadata_dict) return pkg_dict
def test_doi_publish_datacite(self): import ckanext.doi.lib as doi_lib doi = doi_lib.get_doi(self.package_dict['id']) if not doi: doi = doi_lib.create_unique_identifier(self.package_dict['id']) # Build the metadata dict to pass to DataCite service metadata_dict = doi_lib.build_metadata(self.package_dict, doi) # Perform some basic checks against the data - we require at the very least # title and author fields - they're mandatory in the DataCite Schema # This will only be an issue if another plugin has removed a mandatory field doi_lib.validate_metadata(metadata_dict) doi_lib.publish_doi(self.package_dict['id'], **metadata_dict)
def after_update(self, context, pkg_dict): """ Dataset has been created / updated Check status of the dataset to determine if we should publish DOI to datacite network @param pkg_dict: @return: pkg_dict """ # Is this active and public? If so we need to make sure we have an active DOI if pkg_dict.get('state', 'active') == 'active' and not pkg_dict.get( 'private', False): package_id = pkg_dict['id'] # Load the original package, so we can determine if user has changed any fields orig_pkg_dict = get_action('package_show')(context, { 'id': package_id }) # Metadata created isn't populated in pkg_dict - so copy from the original pkg_dict['metadata_created'] = orig_pkg_dict['metadata_created'] # Load the local DOI doi = get_doi(package_id) # If we don't have a DOI, create one # This could happen if the DOI module is enabled after a dataset has been creates if not doi: doi = create_unique_identifier(package_id) # Build the metadata dict to pass to DataCite service metadata_dict = self.build_metadata(pkg_dict, doi) # Perform some basic checks against the data - we require at the very least # title and author fields - they're mandatory in the DataCite Schema # This will only be an issue if another plugin has removed a mandatory field self.validate_metadata(metadata_dict) # Is this an existing DOI? Update it if doi.published: # Before updating, check if any of the metadata has been changed - otherwise # We end up sending loads of revisions to DataCite for minor edits # Load the current version orig_metadata_dict = self.build_metadata(orig_pkg_dict, doi) # Check if the two dictionaries are the same if cmp(orig_metadata_dict, metadata_dict) != 0: # Not the same, so we want to update the metadata update_doi(package_id, **metadata_dict) h.flash_success('DataCite DOI metadata updated') # TODO: If editing a dataset older than 5 days, create DOI revision # New DOI - publish to datacite else: h.flash_success('DataCite DOI created') publish_doi(package_id, **metadata_dict) return pkg_dict
def test_publish_to_datacite(self, mock_request): doi = doi_lib.get_or_create_doi(self.package_dict[u'id']) metadata_dict = doi_lib.build_metadata(self.package_dict, doi) doi_lib.validate_metadata(metadata_dict) doi_lib.publish_doi(self.package_dict[u'id'], **metadata_dict)
def after_update(self, context, pkg_dict): ''' Dataset has been created / updated. Check status of the dataset to determine if we should publish DOI. @param pkg_dict: @return: pkg_dict ''' package_id = pkg_dict['id'] # Load the local DOI doi = get_doi(package_id) # If we're not auto managing the doi, but there is a DOI object # associated with the package, delete it. if not pkg_dict.get('auto_doi_identifier') and doi: delete_doi(package_id) doi = None # We might be short circuiting the after_update if context.get('no_after_update') \ or not pkg_dict.get('auto_doi_identifier'): return pkg_dict # If we don't have a DOI, create one. # This could happen if the DOI module is enabled after a dataset # has been created, or if a user has added their own on dataset # creation, but subsequently deleted it. if not doi: prefix = pkg_dict.get('doi_prefix') doi = create_unique_identifier(package_id, prefix) # ensure doi.identifier and pkg['doi_identifier'] are the same if doi.identifier != pkg_dict['doi_identifier']: self._update_pkg_doi(context, package_id, doi.identifier) # Is this active and public? If so we need to make sure we have an # active DOI if pkg_dict.get('state', 'active') == 'active' \ and not pkg_dict.get('private', False): # Load the original package, so we can determine if user has # changed any fields orig_pkg_dict = get_action('package_show')(context, {'id': package_id}) # Metadata created isn't populated in pkg_dict - so copy from the # original pkg_dict['metadata_created'] = orig_pkg_dict['metadata_created'] # Build the metadata dict to pass to DataCite service metadata_dict = build_metadata(pkg_dict, doi) # Perform some basic checks against the data - we require at the # very least title and author fields - they're mandatory in the # DataCite Schema This will only be an issue if another plugin has # removed a mandatory field validate_metadata(metadata_dict) # Is this an existing DOI? Update it if doi.published: # Before updating, check if any of the metadata has been # changed - otherwise we end up sending loads of revisions to # DataCite for minor edits Load the current version orig_metadata_dict = build_metadata(orig_pkg_dict, doi) # Check if the two dictionaries are the same if cmp(orig_metadata_dict, metadata_dict) != 0: # Not the same, so we want to update the metadata update_doi(package_id, **metadata_dict) # TODO: If editing a dataset older than 5 days, create DOI # revision # New DOI - publish to datacite else: publish_doi(package_id, **metadata_dict) return pkg_dict