Skip to content

Commit

Permalink
Merge bbccea0 into d49113d
Browse files Browse the repository at this point in the history
  • Loading branch information
golnazads committed Sep 13, 2021
2 parents d49113d + bbccea0 commit 57b769d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
40 changes: 40 additions & 0 deletions tugboat/tests/tests_unit/test_search_redirect.py
Expand Up @@ -102,6 +102,46 @@ def test_authors(self):
'&format=SHORT' + '/',
author_search) # authors with or

# first author
req.args = MultiDict([('author', urllib.parse.quote('^Huchra, John'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:' + '"Huchra, John"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # first author no quotes

# single author
req.args = MultiDict([('author', urllib.parse.quote('^Huchra, John$'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + '(author:"Huchra, John" and author_count:1)' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author no quotes

# multi word last name author, no initials
req.args = MultiDict([('author', urllib.parse.quote('^Dorigo Jones'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:"Dorigo Jones,"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author, no initials, no quotes

# multi word last name author, no initials, quoted
req.args = MultiDict([('author', urllib.parse.quote('"^Dorigo Jones"'))])
req.args.update(self.append_defaults())
view = ClassicSearchRedirectView()
author_search = view.translate(req)
self.assertEqual('q=' + 'author:"Dorigo Jones,"' +
'&sort=' + urllib.parse.quote('date desc, bibcode desc') +
'&format=SHORT' + '/',
author_search) # single author, no initials, with quotes

def test_object(self):
"""object: single, multple, etc"""
req = Request('get', 'http://test.test?')
Expand Down
29 changes: 23 additions & 6 deletions tugboat/views.py
Expand Up @@ -514,12 +514,29 @@ def translate_authors(self, args):
authors_str = args.pop('author', None)
if authors_str:
authors = self.classic_field_to_array(authors_str)
# is it a single author search: ^last, first$
match = re.findall(r'\^(.*)\$', ' '.join(authors))
# yes
if match:
search = '(author:"' + match[0] + '" and author_count:1)'
self.translation.search.append(search)
single_author = ' '.join(authors).replace('"', '')
if re.match(r'\^', single_author):
# is it a single author search: ^last, first$
match = re.findall(r'\^([^$]*)\$', ' '.join(authors).replace('"', ''))
# yes
if match:
search = '(author:"' + match[0] + '" and author_count:1)'
self.translation.search.append(search)
else:
# is it first author search: ^last, first
match = re.findall(r'\^([A-Za-z]+,\s+[A-Za-z]+)', ' '.join(authors).replace('"', ''))
# yes
if match:
search = 'author:"' + match[0] + '"'
self.translation.search.append(search)
else:
# is it first author search, but with no first initials
match = re.findall(r'\^([A-Za-z\s]+)', ' '.join(authors).replace('"', ''))
# yes
if match:
search = 'author:"' + match[0] + ',"'
self.translation.search.append(search)
# multiple authors
else:
search += urllib.parse.quote(author_field) + '('
for author in authors:
Expand Down

0 comments on commit 57b769d

Please sign in to comment.