Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update related ids upon dataSet change. Fixes #402 #403

Merged
merged 1 commit into from Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 22 additions & 15 deletions server/lib/__init__.py
Expand Up @@ -48,7 +48,7 @@ def pids_to_entities(pids, user=None, base_url=None, lookup=True):
try:
for pid in pids:
entity = Entity(pid.strip(), user)
entity['base_url'] = base_url
entity["base_url"] = base_url
entity = RESOLVERS.resolve(entity)
provider = IMPORT_PROVIDERS.getProvider(entity)
if lookup:
Expand Down Expand Up @@ -80,30 +80,30 @@ def register_dataMap(dataMaps, parent, parentType, user=None, base_url=None):
"""
progress = True
importedData = []
with ProgressContext(progress, user=user, title='Registering resources') as ctx:
with ProgressContext(progress, user=user, title="Registering resources") as ctx:
for dataMap in DataMap.fromList(dataMaps):
# probably would be nicer if Entity kept all details and the dataMap
# would be merged into it
provider = IMPORT_PROVIDERS.getFromDataMap(dataMap)
objType, obj = provider.register(
parent, parentType, ctx, user, dataMap, base_url=base_url
)
importedData.append(obj['_id'])
importedData.append(obj["_id"])
return importedData


def update_citation(event):
tale = event.info['tale']
user = event.info['user']
tale = event.info["tale"]
user = event.info["user"]

dataset_top_identifiers = set()
for obj in tale.get('dataSet', []):
for obj in tale.get("dataSet", []):
try:
doc = ModelImporter.model(obj['_modelType']).load(
obj['itemId'], user=user, level=AccessType.READ, exc=True
doc = ModelImporter.model(obj["_modelType"]).load(
obj["itemId"], user=user, level=AccessType.READ, exc=True
)
provider_name = doc['meta']['provider']
if provider_name.startswith('HTTP'):
provider_name = doc["meta"]["provider"]
if provider_name.startswith("HTTP"):
continue
provider = IMPORT_PROVIDERS.providerMap[provider_name]
except (KeyError, ValidationException):
Expand All @@ -113,22 +113,29 @@ def update_citation(event):
dataset_top_identifiers.add(top_identifier)

citations = []
related_ids = [
related_id
for related_id in tale["relatedIdentifiers"]
if related_id["relation"] != "Cites"
]
for doi in dataset_top_identifiers:
if doi.startswith('doi:'):
related_ids.append(dict(identifier=doi, relation="Cites"))
if doi.startswith("doi:"):
doi = doi[4:]
try:
url = (
'https://api.datacite.org/dois/'
'text/x-bibliography/{}?style=harvard-cite-them-right'
"https://api.datacite.org/dois/"
"text/x-bibliography/{}?style=harvard-cite-them-right"
)
citations.append(
html2markdown.convert(urlopen(url.format(doi)).read().decode())
)
except Exception as ex:
logger.info('Unable to get a citation for %s, getting "%s"', doi, str(ex))

tale['dataSetCitation'] = citations
tale["dataSetCitation"] = citations
tale["relatedIdentifiers"] = related_ids
event.preventDefault().addResponse(tale)


events.bind('tale.update_citation', 'wholetale', update_citation)
events.bind("tale.update_citation", "wholetale", update_citation)