From fa396d4bc8c3419497eeeb9246b583aa33c2e420 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 10 Aug 2021 13:54:52 -0700 Subject: [PATCH] Save best-guess search results on import --- bookwyrm/importers/importer.py | 3 ++- .../migrations/0083_importitem_book_guess.py | 25 +++++++++++++++++++ bookwyrm/models/import_job.py | 18 ++++++++++--- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 bookwyrm/migrations/0083_importitem_book_guess.py diff --git a/bookwyrm/importers/importer.py b/bookwyrm/importers/importer.py index 48629a01f..39d88e100 100644 --- a/bookwyrm/importers/importer.py +++ b/bookwyrm/importers/importer.py @@ -76,9 +76,10 @@ def import_data(source, job_id): item.save() continue - if item.book: + if item.book or item.book_guess: item.save() + if item.book: # shelves book and handles reviews handle_imported_book( source, job.user, item, job.include_reviews, job.privacy diff --git a/bookwyrm/migrations/0083_importitem_book_guess.py b/bookwyrm/migrations/0083_importitem_book_guess.py new file mode 100644 index 000000000..fa4f94ab3 --- /dev/null +++ b/bookwyrm/migrations/0083_importitem_book_guess.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.4 on 2021-08-10 20:49 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0082_auto_20210806_2324"), + ] + + operations = [ + migrations.AddField( + model_name="importitem", + name="book_guess", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + related_name="book_guess", + to="bookwyrm.book", + ), + ), + ] diff --git a/bookwyrm/models/import_job.py b/bookwyrm/models/import_job.py index 81c75b4b2..53368bce2 100644 --- a/bookwyrm/models/import_job.py +++ b/bookwyrm/models/import_job.py @@ -71,7 +71,13 @@ class ImportItem(models.Model): index = models.IntegerField() data = models.JSONField() book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) - book_guess = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) + book_guess = models.ForeignKey( + Book, + on_delete=models.SET_NULL, + null=True, + blank=True, + related_name="book_guess", + ) fail_reason = models.TextField(null=True) def resolve(self): @@ -81,7 +87,11 @@ class ImportItem(models.Model): else: # don't fall back on title/author search is isbn is present. # you're too likely to mismatch - self.book = self.get_book_from_title_author() + book, confidence = self.get_book_from_title_author() + if confidence > 0.999: + self.book = book + else: + self.book_guess = book def get_book_from_isbn(self): """search by isbn""" @@ -97,12 +107,12 @@ class ImportItem(models.Model): """search by title and author""" search_term = construct_search_term(self.title, self.author) search_result = connector_manager.first_search_result( - search_term, min_confidence=0.999 + search_term, min_confidence=0.1 ) if search_result: # raises ConnectorException return search_result.connector.get_or_create_book(search_result.key) - return None + return None, 0 @property def title(self):