diff --git a/tugboat/tests/tests_unit/test_search_redirect.py b/tugboat/tests/tests_unit/test_search_redirect.py index 857f032..18cf6a2 100644 --- a/tugboat/tests/tests_unit/test_search_redirect.py +++ b/tugboat/tests/tests_unit/test_search_redirect.py @@ -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?') diff --git a/tugboat/views.py b/tugboat/views.py index f6e399b..3be5a3d 100644 --- a/tugboat/views.py +++ b/tugboat/views.py @@ -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: