Fixes first_search_result behavior

This commit is contained in:
Mouse Reeve 2021-11-12 13:48:31 -08:00
parent 1e8269b6c9
commit 80c1954aa3
2 changed files with 10 additions and 5 deletions

View file

@ -82,6 +82,8 @@ def search_identifiers(query, *filters, return_first=False):
*filters, reduce(operator.or_, (Q(**f) for f in or_filters)) *filters, reduce(operator.or_, (Q(**f) for f in or_filters))
).distinct() ).distinct()
if results.count() <= 1: if results.count() <= 1:
if return_first:
return results.first()
return results return results
# when there are multiple editions of the same work, pick the default. # when there are multiple editions of the same work, pick the default.
@ -124,8 +126,9 @@ def search_title_author(query, min_confidence, *filters, return_first=False):
result = default result = default
else: else:
result = editions.first() result = editions.first()
if return_first: if return_first:
return result return result[0]
list_results.append(result) list_results.append(result)
return list_results return list_results

View file

@ -6,7 +6,7 @@ from django.db import models
from django.utils import timezone from django.utils import timezone
from bookwyrm.connectors import connector_manager from bookwyrm.connectors import connector_manager
from bookwyrm.models import ReadThrough, User, Book from bookwyrm.models import ReadThrough, User, Book, Edition
from .fields import PrivacyLevels from .fields import PrivacyLevels
@ -79,6 +79,10 @@ class ImportItem(models.Model):
self.isbn, min_confidence=0.999 self.isbn, min_confidence=0.999
) )
if search_result: if search_result:
# it's already in the right format
if isinstance(search_result, Edition):
return search_result
# it's just a search result, book needs to be created
# raises ConnectorException # raises ConnectorException
return search_result.connector.get_or_create_book(search_result.key) return search_result.connector.get_or_create_book(search_result.key)
return None return None
@ -183,9 +187,7 @@ class ImportItem(models.Model):
def __repr__(self): def __repr__(self):
# pylint: disable=consider-using-f-string # pylint: disable=consider-using-f-string
return "<{!r}Item {!r}>".format( return "<{!r}Item {!r}>".format(self.index, self.normalized_data["title"])
self.normalized_data["import_source"], self.normalized_data["title"]
)
def __str__(self): def __str__(self):
# pylint: disable=consider-using-f-string # pylint: disable=consider-using-f-string