Merge pull request #1278 from bookwyrm-social/csv-import-failures

Store best-guess csv import matches
This commit is contained in:
Mouse Reeve 2021-09-11 15:04:54 -07:00 committed by GitHub
commit 741df38f55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 8 deletions

View file

@ -3,6 +3,7 @@ import csv
import logging import logging
from django.utils import timezone from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from bookwyrm import models from bookwyrm import models
from bookwyrm.models import ImportJob, ImportItem from bookwyrm.models import ImportJob, ImportItem
@ -71,19 +72,20 @@ def import_data(source, job_id):
item.resolve() item.resolve()
except Exception as err: # pylint: disable=broad-except except Exception as err: # pylint: disable=broad-except
logger.exception(err) logger.exception(err)
item.fail_reason = "Error loading book" item.fail_reason = _("Error loading book")
item.save() item.save()
continue continue
if item.book: if item.book or item.book_guess:
item.save() item.save()
if item.book:
# shelves book and handles reviews # shelves book and handles reviews
handle_imported_book( handle_imported_book(
source, job.user, item, job.include_reviews, job.privacy source, job.user, item, job.include_reviews, job.privacy
) )
else: else:
item.fail_reason = "Could not find a match for book" item.fail_reason = _("Could not find a match for book")
item.save() item.save()
finally: finally:
job.complete = True job.complete = True

View file

@ -0,0 +1,25 @@
# Generated by Django 3.2.4 on 2021-09-11 14:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0093_alter_sitesettings_instance_short_description"),
]
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",
),
),
]

View file

@ -0,0 +1,13 @@
# Generated by Django 3.2.4 on 2021-09-11 21:43
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0094_auto_20210911_1550"),
("bookwyrm", "0094_importitem_book_guess"),
]
operations = []

View file

@ -71,6 +71,13 @@ class ImportItem(models.Model):
index = models.IntegerField() index = models.IntegerField()
data = models.JSONField() data = models.JSONField()
book = models.ForeignKey(Book, on_delete=models.SET_NULL, null=True, blank=True) 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,
related_name="book_guess",
)
fail_reason = models.TextField(null=True) fail_reason = models.TextField(null=True)
def resolve(self): def resolve(self):
@ -78,9 +85,13 @@ class ImportItem(models.Model):
if self.isbn: if self.isbn:
self.book = self.get_book_from_isbn() self.book = self.get_book_from_isbn()
else: else:
# don't fall back on title/author search is isbn is present. # don't fall back on title/author search if isbn is present.
# you're too likely to mismatch # 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): def get_book_from_isbn(self):
"""search by isbn""" """search by isbn"""
@ -96,12 +107,15 @@ class ImportItem(models.Model):
"""search by title and author""" """search by title and author"""
search_term = construct_search_term(self.title, self.author) search_term = construct_search_term(self.title, self.author)
search_result = connector_manager.first_search_result( search_result = connector_manager.first_search_result(
search_term, min_confidence=0.999 search_term, min_confidence=0.1
) )
if search_result: if search_result:
# raises ConnectorException # raises ConnectorException
return search_result.connector.get_or_create_book(search_result.key) return (
return None search_result.connector.get_or_create_book(search_result.key),
search_result.confidence,
)
return None, 0
@property @property
def title(self): def title(self):