remove need for get_or_create_book

This commit is contained in:
Mouse Reeve 2020-11-28 18:46:50 -08:00
parent 34e8fb3e5c
commit 9d84346d3c
2 changed files with 23 additions and 67 deletions

View file

@ -1,83 +1,37 @@
''' using another bookwyrm instance as a source of book data '''
from django.db import transaction
from bookwyrm import activitypub, models
from .abstract_connector import AbstractConnector, SearchResult
from .abstract_connector import get_data
class Connector(AbstractConnector):
''' interact with other instances '''
def update_from_mappings(self, obj, data, mappings):
''' serialize book data into a model '''
if self.is_work_data(data):
work_data = activitypub.Work(**data)
return work_data.to_model(models.Work, instance=obj)
edition_data = activitypub.Edition(**data)
return edition_data.to_model(models.Edition, instance=obj)
def get_remote_id_from_data(self, data):
return data.get('id')
def is_work_data(self, data):
return data.get('type') == 'Work'
def get_edition_from_work_data(self, data):
''' we're served a list of edition urls '''
path = data['editions'][0]
return get_data(path)
def get_work_from_edition_date(self, data):
return get_data(data['work'])
def get_authors_from_data(self, data):
''' load author data '''
for author_id in data.get('authors', []):
try:
yield models.Author.objects.get(origin_id=author_id)
except models.Author.DoesNotExist:
pass
data = get_data(author_id)
author_data = activitypub.Author(**data)
author = author_data.to_model(models.Author)
yield author
def get_cover_from_data(self, data):
pass
''' this is basically just for search '''
def get_or_create_book(self, remote_id):
return activitypub.resolve_remote_id(models.Edition, remote_id)
def parse_search_data(self, data):
return data
def format_search_result(self, search_result):
return SearchResult(**search_result)
def get_remote_id_from_data(self, data):
pass
def is_work_data(self, data):
pass
def get_edition_from_work_data(self, data):
pass
def get_work_from_edition_date(self, data):
pass
def get_cover_from_data(self, data):
pass
def expand_book_data(self, book):
work = book
# go from the edition to the work, if necessary
if isinstance(book, models.Edition):
work = book.parent_work
pass
# it may be that we actually want to request this url
editions_url = '%s/editions?page=true' % work.remote_id
edition_options = get_data(editions_url)
for edition_data in edition_options['orderedItems']:
with transaction.atomic():
edition = self.create_book(
edition_data['id'],
edition_data,
models.Edition
)
edition.parent_work = work
edition.save()
if not edition.authors.exists() and work.authors.exists():
edition.authors.set(work.authors.all())
def get_authors_from_data(self, data):
pass

View file

@ -219,7 +219,9 @@ def edit_profile(request):
def resolve_book(request):
''' figure out the local path to a book from a remote_id '''
remote_id = request.POST.get('remote_id')
book = books_manager.get_or_create_book(remote_id)
connector = books_manager.get_or_create_connector(remote_id)
book = connector.get_or_create_book(remote_id)
return redirect('/book/%d' % book.id)