Skip to content

Commit

Permalink
Merge pull request #551 from CDLUC3/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jsjiang authored Jan 18, 2024
2 parents 8d160ab + df73324 commit 550c088
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 2 deletions.
98 changes: 98 additions & 0 deletions impl/datacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import django.conf
import lxml.etree
import xmltodict

import ezidapp.models.shoulder
import ezidapp.models.validation
Expand Down Expand Up @@ -616,6 +617,103 @@ def dcmsRecordToHtml(record):
except Exception:
return None

def removeXMLNamespacePrefix(record):
"""Remove namespace prefixes from XML elements andd attributes.
Args:
record: The record should be unencoded.
Returns:
XML record or None on error.
"""
try:
r = lxml.etree.tostring(
lxml.etree.XSLT(
lxml.etree.parse(
os.path.join(django.conf.settings.PROJECT_ROOT, "profiles", "remove_ns_prefix.xsl")
)
)(impl.util.parseXmlString(record)),
encoding=str,
)
return r
except Exception:
return None

def dcmsRecordToDict(record):
"""Convert a DataCite Metadata Scheme <http://schema.datacite.org/> record
to a dict.
Args:
record: The record should be unencoded.
Returns:
A dict or None on error.
"""
record_dict = xmltodict.parse(str(record))
return record_dict

def briefDataciteRecord(record):
"""Convert a DataCite Metadata Scheme <http://schema.datacite.org/> record
to a dictionary in simple DataCite format with data fields for Citation Preview.
Args:
record: Datacite record in XML.
Returns:
A dict or None on error. The dict contains the following keys:
- 'datacite.creator'
- 'datacite.title'
- 'datacite.publisher'
- 'datacite.publicationyear'
- 'datacite.resourcetype'
"""

datacite_dict = dcmsRecordToDict(record)
briefDcRecord = {}
try:
if datacite_dict and 'resource' in datacite_dict:
if 'creators' in datacite_dict['resource'] and 'creator' in datacite_dict['resource']['creators']:
creator = datacite_dict['resource']['creators']['creator']
if isinstance(creator, list):
et_al = ''
for i in range(len(creator)):
if 'creatorName' in creator[i]:
if 'datacite.creator' not in briefDcRecord:
briefDcRecord['datacite.creator'] = get_dict_value_by_key(creator[i]['creatorName'], '#text')
else:
et_al = 'et al.'
if briefDcRecord['datacite.creator'] and et_al != '':
briefDcRecord['datacite.creator'] += f' {et_al}'
else:
if 'creatorName' in creator:
briefDcRecord['datacite.creator'] = get_dict_value_by_key(creator['creatorName'], '#text')

if 'titles' in datacite_dict['resource'] and 'title' in datacite_dict['resource']['titles']:
title = datacite_dict['resource']['titles']['title']
if isinstance(title, list):
if len(title) > 0:
briefDcRecord['datacite.title'] = get_dict_value_by_key(title[0], '#text')
else:
briefDcRecord['datacite.title'] = get_dict_value_by_key(title, '#text')

if 'publisher' in datacite_dict['resource']:
briefDcRecord['datacite.publisher'] = get_dict_value_by_key(datacite_dict['resource']['publisher'], '#text')

if 'publicationYear' in datacite_dict['resource']:
briefDcRecord['datacite.publicationyear'] = datacite_dict['resource']['publicationYear']

if 'resourceType' in datacite_dict['resource']:
briefDcRecord['datacite.resourcetype'] = get_dict_value_by_key(datacite_dict['resource']['resourceType'], '@resourceTypeGeneral')
except Exception as ex:
print(f'error: {ex} - brief record: {briefDcRecord}')

return briefDcRecord

def get_dict_value_by_key(input_dict, key):
if isinstance(input_dict, dict) and key in input_dict:
return input_dict.get(key)
else:
return input_dict

def crossrefToDatacite(record, overrides=None):
"""Convert a Crossref Deposit Schema <http://help.crossref.org/deposit_schema>
Expand Down
16 changes: 15 additions & 1 deletion impl/ui_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,9 +425,23 @@ def details(request):
time.time() - float(id_metadata['_updated']) < 60 * 30
)
if d['current_profile'].name == 'datacite' and 'datacite' in id_metadata:
r = impl.datacite.dcmsRecordToHtml(id_metadata["datacite"])
r = impl.datacite.dcmsRecordToHtml(id_metadata['datacite'])
if r:
d['datacite_html'] = r

converted_rd = impl.datacite.removeXMLNamespacePrefix(id_metadata['datacite'])
brief_record = impl.datacite.briefDataciteRecord(converted_rd)
brief_record_keys = [
'datacite.creator',
'datacite.title',
'datacite.publicationyear',
'datacite.publisher',
'datacite.resourcetype',
]
for key in brief_record_keys:
if key in brief_record:
d["identifier"][key] = brief_record[key]

if (
d['current_profile'].name == 'crossref'
and 'crossref' in id_metadata
Expand Down
28 changes: 28 additions & 0 deletions profiles/remove_ns_prefix.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>

<!-- ==========================================================================
transform an XML document by stripping the namespaces from element names and attributes
=========================================================================== -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<!-- Template for elements -->
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>

<!-- Template for attributes -->
<xsl:template match="@*">
<xsl:attribute name="{local-name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>

</xsl:stylesheet>


1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ requests~=2.31.0
setuptools~=65.5.1
simplegist~=1.0.1
toml~=0.10.2
xmltodict~=0.13.0

PyMySQL~=0.9.3
configparser~=5.0.2
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ requests~=2.31.0
setuptools~=65.5.1
simplegist~=1.0.1
toml~=0.10.2
xmltodict~=0.13.0

PyMySQL~=0.9.3
configparser~=5.0.2
Expand Down
2 changes: 1 addition & 1 deletion templates/manage/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h2 class="heading__icon-4">{% trans "About the Identifier" %}</h2>
</div -->
</div>

{% if not has_block_data and current_profile.name == 'datacite' %}
{% if current_profile.name == 'datacite' %}
<div class="row vertical-buffer-20">
<div class="call-out">
<h2 class="call-out__heading">{% trans "Citation Preview" %}</h2>
Expand Down
Loading

0 comments on commit 550c088

Please sign in to comment.