Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions hs_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2991,42 +2991,43 @@ def extra_capabilites(self):
"""
return None

def parse_citation_name(self, name, first_author=False):
def parse_citation_name(self, name, is_organization=False, is_non_hs_user=False):
"""Return properly formatted citation name from metadata."""
if is_organization:
return name + ", "

CREATOR_NAME_ERROR = "Failed to generate citation - invalid creator name."
first_names = None

if "," in name:
name_parts = name.split(",")
if len(name_parts) == 0:
return CREATOR_NAME_ERROR
elif len(name_parts) == 1:
last_names = name_parts[0]
elif len(name_parts) == 2:
first_names = name_parts[1]
first_names = first_names.split()
last_names = name_parts[0]
name_parts = name.split(",", 1) # Split only on first comma
if len(name_parts) == 2:
last_names = name_parts[0].strip()
first_names = name_parts[1].strip().split()
if is_non_hs_user:
potential_first = last_names
potential_last_parts = first_names
last_names = potential_last_parts[-1]
first_names = [potential_first] + potential_last_parts[:-1]
else:
return CREATOR_NAME_ERROR
else:
name_parts = name.split()
if len(name_parts) == 0:
return CREATOR_NAME_ERROR
elif len(name_parts) > 1:
first_names = name_parts[:-1]
last_names = name_parts[-1]
else:
elif len(name_parts) == 1:
last_names = name_parts[0]
first_names = None
elif len(name_parts) == 2:
first_names = [name_parts[0]]
last_names = name_parts[1]
else:
last_names = name_parts[-1]
first_names = name_parts[:-1]

if first_names:
initials_list = [i[0] for i in first_names]
initials = ". ".join(initials_list) + "."
if first_author:
author_name = "{last_name}, {initials}"
else:
author_name = "{initials} {last_name}"
author_name = author_name.format(last_name=last_names,
initials=initials
)
initials_list = [i[0] + "." for i in first_names if i]
initials = " ".join(initials_list)
author_name = "{last_name}, {initials}".format(last_name=last_names, initials=initials)
else:
author_name = "{last_name}".format(last_name=last_names)

Expand Down Expand Up @@ -3063,7 +3064,8 @@ def get_citation(self, forceHydroshareURI=True):
if first_creator.get('organization', '') and not creator_name:
citation_str_lst.append(first_creator['organization'] + ", ")
else:
citation_str_lst.append(self.parse_citation_name(creator_name, first_author=True))
is_non_hs_user = not first_creator.get('hs_user_id')
citation_str_lst.append(self.parse_citation_name(creator_name, is_non_hs_user=is_non_hs_user))

# Add other creators
other_creators = [c for c in cached_creators if c.get('order', 0) > 1]
Expand All @@ -3074,7 +3076,9 @@ def get_citation(self, forceHydroshareURI=True):
if author.get('organization', '') and not author_name:
citation_str_lst.append(author['organization'] + ", ")
elif author_name and len(author_name) != 0:
citation_str_lst.append(self.parse_citation_name(author_name))
# Check if this is a non-HydroShare user who might have entered name incorrectly
is_non_hs_user = not author.get('hs_user_id')
citation_str_lst.append(self.parse_citation_name(author_name, is_non_hs_user=is_non_hs_user))

# Remove the last added comma and space
if len(citation_str_lst[-1]) > 2:
Expand Down Expand Up @@ -3109,10 +3113,11 @@ def get_citation(self, forceHydroshareURI=True):
if not title:
logger.error(f"{CITATION_ERROR} No title found in cached_metadata for resource {self.short_id}")
return CITATION_ERROR
if not title.get('value', ''):
title_value = title.get('value', '') if isinstance(title, dict) else str(title)
if not title_value:
logger.error(f"{CITATION_ERROR} No title value found in cached_metadata for resource {self.short_id}")
return CITATION_ERROR
citation_str_lst.append(title.get('value'))
citation_str_lst.append(title_value)

# Check for DOI identifier
isPendingActivation = False
Expand Down
18 changes: 9 additions & 9 deletions hs_core/tests/api/native/test_get_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_two_authors_no_comma(self):
hs_url = hs_identifier.url
hs_date = str(date.today().year)
correct_citation = 'Creator_LastName, C., ' \
'J. Smith ({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
'Smith, J. ({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
self.assertEqual(citation, correct_citation)

def test_two_authors_comma(self):
Expand All @@ -66,7 +66,7 @@ def test_two_authors_comma(self):
hs_url = hs_identifier.url
hs_date = str(date.today().year)
correct_citation = 'Creator_LastName, C., ' \
'J. Smith ({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
'John, S. ({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
self.assertEqual(citation, correct_citation)

def test_two_authors_multiple_first_and_last_names_comma(self):
Expand All @@ -79,7 +79,7 @@ def test_two_authors_multiple_first_and_last_names_comma(self):
hs_url = hs_identifier.url
hs_date = str(date.today().year)
correct_citation = 'Creator_LastName, C., ' \
'J. M. J. Smith William ' \
'Jingle, S. J. M. ' \
'({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
self.assertEqual(citation, correct_citation)

Expand All @@ -93,7 +93,7 @@ def test_two_authors_multiple_first_and_last_names_no_comma(self):
hs_url = hs_identifier.url
hs_date = str(date.today().year)
correct_citation = 'Creator_LastName, C., ' \
'J. M. J. S. William ' \
'William, J. M. J. S. ' \
'({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
self.assertEqual(citation, correct_citation)

Expand All @@ -109,24 +109,24 @@ def test_two_authors_and_organization(self):
hs_url = hs_identifier.url
hs_date = str(date.today().year)
correct_citation = 'Creator_LastName, C., ' \
'J. M. J. Smith William, ' \
'Jingle, S. J. M., ' \
'U.S. Geological Survey ' \
'({}). A resource, HydroShare, {}'.format(hs_date, hs_url)
self.assertEqual(citation, correct_citation)

def test_parse_citation_name(self):
name = "John Morley Smith"
parsed_name = self.res.parse_citation_name(name, first_author=True)
parsed_name = self.res.parse_citation_name(name)
self.assertEqual(parsed_name, 'Smith, J. M., ')

name = "John Morley Smith"
parsed_name = self.res.parse_citation_name(name)
self.assertEqual(parsed_name, 'J. M. Smith, ')
self.assertEqual(parsed_name, 'Smith, J. M., ')

name = "Smith Tanner, John Morley"
parsed_name = self.res.parse_citation_name(name, first_author=True)
parsed_name = self.res.parse_citation_name(name)
self.assertEqual(parsed_name, 'Smith Tanner, J. M., ')

name = "Smith Tanner, John Morley"
parsed_name = self.res.parse_citation_name(name)
self.assertEqual(parsed_name, 'J. M. Smith Tanner, ')
self.assertEqual(parsed_name, 'Smith Tanner, J. M., ')