def after_create(self, context, pkg_dict): """ A new dataset has been created, so we need to create a new DOI NB: This is called after creation of a dataset, and before resources have been added so state = draft @param context: @param pkg_dict: @return: """ create_unique_identifier(pkg_dict['id'])
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 after_create(self, context, pkg_dict): """ A new dataset has been created, so we need to create a new DOI NB: This is called after creation of a dataset, and before resources have been added so state = draft @param context: @param pkg_dict: @return: """ # Only create a new DOI if the user has requested it if "dataset_category" in pkg_dict: # Load the local DOI doi = get_doi(pkg_dict['id']) # There is a chance that a doi has already been created for this pkg_id # and could cause an integrity error if another is added if not doi: # Create a new doi create_unique_identifier(pkg_dict['id']) # Remove the auto create field from the dataset pkg pkg_dict.pop('dataset_category') return pkg_dict
def after_create(self, context, pkg_dict): ''' A new dataset has been created, so we need to create a new DOI. NB: This is called after creation of a dataset, and before resources have been added so state = draft @param context: @param pkg_dict: @return: ''' if pkg_dict.get('auto_doi_identifier'): # create a doi and populate pkg.doi_identifier with it. prefix = pkg_dict.get('doi_prefix') doi = create_unique_identifier(pkg_dict['id'], prefix) self._update_pkg_doi(context, pkg_dict['id'], doi.identifier)
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 test_doi_create_identifier(self): """ Test a DOI has been created with the package We won't have tried pushing to the DataCite service :return: """ # Import now otherwise we get model creation errors import ckanext.doi.lib as doi_lib # Show the package - which will load the DOI identifier = doi_lib.create_unique_identifier(self.package_dict['id']) # Make sure we have a DOI model assert_is_instance(identifier, doi_lib.DOI) # And the package ID is correct assert_equal(identifier.package_id, self.package_dict['id']) # And published should be none assert_is_none(identifier.published)
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 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