mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-11 17:55:37 +00:00
Merge pull request #209 from mouse-reeve/connector-errors
Raise errors when connectors fail
This commit is contained in:
commit
6b5246f06a
6 changed files with 20 additions and 8 deletions
|
@ -26,8 +26,10 @@ def get_or_create_book(remote_id):
|
||||||
|
|
||||||
connector = get_or_create_connector(remote_id)
|
connector = get_or_create_connector(remote_id)
|
||||||
|
|
||||||
|
# raises ConnectorException
|
||||||
book = connector.get_or_create_book(remote_id)
|
book = connector.get_or_create_book(remote_id)
|
||||||
load_more_data.delay(book.id)
|
if book:
|
||||||
|
load_more_data.delay(book.id)
|
||||||
return book
|
return book
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
''' bring connectors into the namespace '''
|
''' bring connectors into the namespace '''
|
||||||
from .settings import CONNECTORS
|
from .settings import CONNECTORS
|
||||||
|
from .abstract_connector import ConnectorException
|
||||||
|
|
|
@ -9,6 +9,10 @@ from django.db import transaction
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectorException(Exception):
|
||||||
|
''' when the connector can't do what was asked '''
|
||||||
|
|
||||||
|
|
||||||
class AbstractConnector(ABC):
|
class AbstractConnector(ABC):
|
||||||
''' generic book data connector '''
|
''' generic book data connector '''
|
||||||
|
|
||||||
|
@ -128,6 +132,9 @@ class AbstractConnector(ABC):
|
||||||
edition.author_text = work.author_text
|
edition.author_text = work.author_text
|
||||||
edition.save()
|
edition.save()
|
||||||
|
|
||||||
|
if not edition:
|
||||||
|
raise ConnectorException('Unable to create book: %s' % remote_id)
|
||||||
|
|
||||||
return edition
|
return edition
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ from django.core.files.base import ContentFile
|
||||||
|
|
||||||
from bookwyrm import models
|
from bookwyrm import models
|
||||||
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
from .abstract_connector import AbstractConnector, SearchResult, Mapping
|
||||||
|
from .abstract_connector import ConnectorException
|
||||||
from .abstract_connector import update_from_mappings
|
from .abstract_connector import update_from_mappings
|
||||||
from .abstract_connector import get_date, get_data
|
from .abstract_connector import get_date, get_data
|
||||||
from .openlibrary_languages import languages
|
from .openlibrary_languages import languages
|
||||||
|
@ -80,7 +81,7 @@ class Connector(AbstractConnector):
|
||||||
try:
|
try:
|
||||||
key = data['key']
|
key = data['key']
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
raise ConnectorException('Invalid book data')
|
||||||
url = '%s/%s/editions' % (self.books_url, key)
|
url = '%s/%s/editions' % (self.books_url, key)
|
||||||
data = get_data(url)
|
data = get_data(url)
|
||||||
return pick_default_edition(data['entries'])
|
return pick_default_edition(data['entries'])
|
||||||
|
@ -90,7 +91,7 @@ class Connector(AbstractConnector):
|
||||||
try:
|
try:
|
||||||
key = data['works'][0]['key']
|
key = data['works'][0]['key']
|
||||||
except (IndexError, KeyError):
|
except (IndexError, KeyError):
|
||||||
return False
|
raise ConnectorException('No work found for edition')
|
||||||
url = '%s/%s' % (self.books_url, key)
|
url = '%s/%s' % (self.books_url, key)
|
||||||
return get_data(url)
|
return get_data(url)
|
||||||
|
|
||||||
|
|
|
@ -50,10 +50,6 @@ class Connector(AbstractConnector):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_book(self, remote_id):
|
|
||||||
''' this COULD be semi-implemented but I think it shouldn't be used '''
|
|
||||||
|
|
||||||
|
|
||||||
def is_work_data(self, data):
|
def is_work_data(self, data):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ from django.db import models
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from bookwyrm import books_manager
|
from bookwyrm import books_manager
|
||||||
|
from bookwyrm.connectors import ConnectorException
|
||||||
from bookwyrm.models import ReadThrough, User, Book
|
from bookwyrm.models import ReadThrough, User, Book
|
||||||
from bookwyrm.utils.fields import JSONField
|
from bookwyrm.utils.fields import JSONField
|
||||||
|
|
||||||
|
@ -65,7 +66,11 @@ class ImportItem(models.Model):
|
||||||
''' search by isbn '''
|
''' search by isbn '''
|
||||||
search_result = books_manager.first_search_result(self.isbn)
|
search_result = books_manager.first_search_result(self.isbn)
|
||||||
if search_result:
|
if search_result:
|
||||||
return books_manager.get_or_create_book(search_result.key)
|
try:
|
||||||
|
# don't crash the import when the connector fails
|
||||||
|
return books_manager.get_or_create_book(search_result.key)
|
||||||
|
except ConnectorException:
|
||||||
|
pass
|
||||||
|
|
||||||
def get_book_from_title_author(self):
|
def get_book_from_title_author(self):
|
||||||
''' search by title and author '''
|
''' search by title and author '''
|
||||||
|
|
Loading…
Reference in a new issue