2020-03-28 19:55:53 +00:00
|
|
|
''' using a fedireads instance as a source of book data '''
|
2020-04-29 17:57:20 +00:00
|
|
|
from django.contrib.postgres.search import SearchVector
|
2020-03-28 19:55:53 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
|
|
|
|
from fedireads import models
|
2020-04-29 17:57:20 +00:00
|
|
|
from .abstract_connector import AbstractConnector, SearchResult
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Connector(AbstractConnector):
|
|
|
|
''' instantiate a connector '''
|
|
|
|
def __init__(self, identifier):
|
|
|
|
super().__init__(identifier)
|
|
|
|
|
|
|
|
|
|
|
|
def search(self, query):
|
|
|
|
''' right now you can't search fedireads sorry, but when
|
|
|
|
that gets implemented it will totally rule '''
|
2020-04-29 17:57:20 +00:00
|
|
|
results = models.Edition.objects.annotate(
|
|
|
|
search=SearchVector('title', weight='A') +\
|
|
|
|
SearchVector('subtitle', weight='B') +\
|
|
|
|
SearchVector('author_text', weight='A') +\
|
|
|
|
SearchVector('isbn_13', weight='A') +\
|
|
|
|
SearchVector('isbn_10', weight='A') +\
|
|
|
|
SearchVector('openlibrary_key', weight='B') +\
|
|
|
|
SearchVector('goodreads_key', weight='B') +\
|
|
|
|
SearchVector('asin', weight='B') +\
|
|
|
|
SearchVector('oclc_number', weight='B') +\
|
|
|
|
SearchVector('description', weight='C') +\
|
|
|
|
SearchVector('series', weight='C')
|
|
|
|
).filter(search=query)
|
2020-04-29 18:22:29 +00:00
|
|
|
results = results.filter(default=True) or results
|
2020-04-29 17:57:20 +00:00
|
|
|
|
|
|
|
search_results = []
|
|
|
|
for book in results[:10]:
|
|
|
|
search_results.append(
|
|
|
|
SearchResult(
|
|
|
|
book.title,
|
2020-05-04 00:53:14 +00:00
|
|
|
book.id,
|
2020-04-29 17:57:20 +00:00
|
|
|
book.author_text,
|
|
|
|
book.published_date.year if book.published_date else None,
|
|
|
|
None
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return search_results
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
2020-05-04 00:53:14 +00:00
|
|
|
def get_or_create_book(self, book_id):
|
2020-03-28 19:55:53 +00:00
|
|
|
''' since this is querying its own data source, it can only
|
|
|
|
get a book, not load one from an external source '''
|
|
|
|
try:
|
|
|
|
return models.Book.objects.select_subclasses().get(
|
2020-05-04 00:53:14 +00:00
|
|
|
id=book_id
|
2020-03-28 19:55:53 +00:00
|
|
|
)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-05-04 00:53:14 +00:00
|
|
|
def get_or_create_author(self, author_id):
|
2020-03-28 19:55:53 +00:00
|
|
|
''' load that author '''
|
|
|
|
try:
|
2020-05-04 00:53:14 +00:00
|
|
|
return models.Author.objects.get(id=author_id)
|
2020-03-28 19:55:53 +00:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def update_book(self, book_obj):
|
|
|
|
pass
|
2020-04-29 17:57:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def expand_book_data(self, book):
|
|
|
|
pass
|