2020-09-21 15:10:37 +00:00
|
|
|
''' using a bookwyrm instance as a source of book data '''
|
2020-05-12 20:03:46 +00:00
|
|
|
from django.contrib.postgres.search import SearchRank, SearchVector
|
2020-03-28 19:55:53 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm 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 search(self, query):
|
2020-09-21 15:10:37 +00:00
|
|
|
''' right now you can't search bookwyrm sorry, but when
|
2020-03-28 19:55:53 +00:00
|
|
|
that gets implemented it will totally rule '''
|
2020-05-12 20:03:46 +00:00
|
|
|
vector = 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('remote_id', weight='B') +\
|
|
|
|
SearchVector('description', weight='C') +\
|
|
|
|
SearchVector('series', weight='C')
|
|
|
|
|
2020-04-29 17:57:20 +00:00
|
|
|
results = models.Edition.objects.annotate(
|
2020-05-12 20:03:46 +00:00
|
|
|
search=vector
|
|
|
|
).annotate(
|
|
|
|
rank=SearchRank(vector, query)
|
|
|
|
).filter(
|
|
|
|
rank__gt=0
|
|
|
|
).order_by('-rank')
|
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(
|
2020-05-04 04:00:25 +00:00
|
|
|
self.format_search_result(book)
|
2020-04-29 17:57:20 +00:00
|
|
|
)
|
|
|
|
return search_results
|
2020-03-28 19:55:53 +00:00
|
|
|
|
|
|
|
|
2020-09-21 17:25:26 +00:00
|
|
|
def format_search_result(self, search_result):
|
2020-05-04 04:00:25 +00:00
|
|
|
return SearchResult(
|
2020-09-21 17:25:26 +00:00
|
|
|
search_result.title,
|
|
|
|
search_result.local_id,
|
|
|
|
search_result.author_text,
|
|
|
|
search_result.published_date.year if \
|
|
|
|
search_result.published_date else None,
|
2020-05-04 04:00:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def is_work_data(self, data):
|
|
|
|
pass
|
2020-03-28 19:55:53 +00:00
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def get_edition_from_work_data(self, data):
|
|
|
|
pass
|
2020-03-28 19:55:53 +00:00
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def get_work_from_edition_date(self, data):
|
|
|
|
pass
|
2020-05-09 20:36:10 +00:00
|
|
|
|
|
|
|
def get_authors_from_data(self, data):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_cover_from_data(self, data):
|
|
|
|
return None
|
|
|
|
|
2020-05-10 19:56:59 +00:00
|
|
|
def parse_search_data(self, data):
|
|
|
|
''' it's already in the right format, don't even worry about it '''
|
|
|
|
return data
|
2020-04-29 17:57:20 +00:00
|
|
|
|
|
|
|
def expand_book_data(self, book):
|
|
|
|
pass
|