Only use get_or_create_book with remote_id

This commit is contained in:
Mouse Reeve 2020-05-10 17:40:22 -07:00
parent 0f579e7d8d
commit 35ca61d7f6
7 changed files with 15 additions and 39 deletions

View file

@ -8,28 +8,15 @@ from fedireads import models, settings
from fedireads.tasks import app from fedireads.tasks import app
def get_or_create_book(value, key='id', connector_id=None): def get_or_create_book(remote_id):
''' pull up a book record by whatever means possible ''' ''' pull up a book record by whatever means possible '''
book = models.Book.objects.select_subclasses().filter( book = get_by_absolute_id(remote_id, models.Book)
**{key: value}
).first()
if book:
if not isinstance(book, models.Edition):
return book.default_edition
return book
if key == 'remote_id':
book = get_by_absolute_id(value, models.Book)
if book: if book:
return book return book
if connector_id: connector = get_or_create_connector(remote_id)
connector_info = models.Connector.objects.get(id=connector_id)
connector = load_connector(connector_info)
else:
connector = get_or_create_connector(value)
book = connector.get_or_create_book(value) book = connector.get_or_create_book(remote_id)
load_more_data.delay(book.id) load_more_data.delay(book.id)
return book return book
@ -51,7 +38,6 @@ def get_or_create_connector(remote_id):
books_url='https://%s/book' % identifier, books_url='https://%s/book' % identifier,
covers_url='https://%s/images/covers' % identifier, covers_url='https://%s/images/covers' % identifier,
search_url='https://%s/search?q=' % identifier, search_url='https://%s/search?q=' % identifier,
key_name='remote_id',
priority=3 priority=3
) )

View file

@ -1,6 +1,5 @@
''' using a fedireads instance as a source of book data ''' ''' using a fedireads instance as a source of book data '''
from django.contrib.postgres.search import SearchVector from django.contrib.postgres.search import SearchVector
from django.core.exceptions import ObjectDoesNotExist
from fedireads import models from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult from .abstract_connector import AbstractConnector, SearchResult
@ -48,15 +47,9 @@ class Connector(AbstractConnector):
) )
def get_or_create_book(self, book_id): def get_or_create_book(self, remote_id):
''' since this is querying its own data source, it can only ''' this COULD be semi-implemented but I think it shouldn't be used '''
get a book, not load one from an external source ''' pass
try:
return models.Book.objects.select_subclasses().get(
id=book_id
)
except ObjectDoesNotExist:
return None
def is_work_data(self, data): def is_work_data(self, data):

View file

@ -7,7 +7,7 @@ from django.http import HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
import requests import requests
from fedireads import activitypub, books_manager from fedireads import activitypub
from fedireads import models from fedireads import models
from fedireads.broadcast import broadcast from fedireads.broadcast import broadcast
from fedireads.status import create_review, create_status from fedireads.status import create_review, create_status
@ -263,7 +263,7 @@ def handle_comment(user, book, content):
def handle_status(user, book_id, \ def handle_status(user, book_id, \
builder, fr_serializer, ap_serializer, *args): builder, fr_serializer, ap_serializer, *args):
''' generic handler for statuses ''' ''' generic handler for statuses '''
book = books_manager.get_or_create_book(book_id) book = models.Book.objects.get(id=book_id)
status = builder(user, book, *args) status = builder(user, book, *args)
activity = fr_serializer(status) activity = fr_serializer(status)

View file

@ -9,7 +9,7 @@ from fedireads.sanitize_html import InputHtmlParser
def create_review_from_activity(author, activity): def create_review_from_activity(author, activity):
''' parse an activity json blob into a status ''' ''' parse an activity json blob into a status '''
book_id = activity['inReplyToBook'] book_id = activity['inReplyToBook']
book = get_or_create_book(book_id, key='remote_id') book = get_or_create_book(book_id)
name = activity.get('name') name = activity.get('name')
rating = activity.get('rating') rating = activity.get('rating')
content = activity.get('content') content = activity.get('content')
@ -57,7 +57,7 @@ def create_review(user, book, name, content, rating):
def create_quotation_from_activity(author, activity): def create_quotation_from_activity(author, activity):
''' parse an activity json blob into a status ''' ''' parse an activity json blob into a status '''
book_id = activity['inReplyToBook'] book_id = activity['inReplyToBook']
book = get_or_create_book(book_id, key='remote_id') book = get_or_create_book(book_id)
quote = activity.get('quote') quote = activity.get('quote')
content = activity.get('content') content = activity.get('content')
published = activity.get('published') published = activity.get('published')
@ -87,7 +87,7 @@ def create_quotation(user, book, content, quote):
def create_comment_from_activity(author, activity): def create_comment_from_activity(author, activity):
''' parse an activity json blob into a status ''' ''' parse an activity json blob into a status '''
book_id = activity['inReplyToBook'] book_id = activity['inReplyToBook']
book = get_or_create_book(book_id, key='remote_id') book = get_or_create_book(book_id)
content = activity.get('content') content = activity.get('content')
published = activity.get('published') published = activity.get('published')
remote_id = activity['id'] remote_id = activity['id']
@ -191,7 +191,7 @@ def create_status(user, content, reply_parent=None, mention_books=None,
def create_tag(user, possible_book, name): def create_tag(user, possible_book, name):
''' add a tag to a book ''' ''' add a tag to a book '''
book = get_or_create_book(possible_book, key='remote_id') book = get_or_create_book(possible_book)
try: try:
tag = models.Tag.objects.create(name=name, book=book, user=user) tag = models.Tag.objects.create(name=name, book=book, user=user)

View file

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

View file

@ -12,7 +12,6 @@ from django.views.decorators.csrf import csrf_exempt
from fedireads import activitypub, outgoing from fedireads import activitypub, outgoing
from fedireads import forms, models, books_manager from fedireads import forms, models, books_manager
from fedireads import goodreads_import from fedireads import goodreads_import
from fedireads.books_manager import get_or_create_book
from fedireads.tasks import app from fedireads.tasks import app
@ -470,7 +469,7 @@ def book_page(request, book_id, tab='friends'):
@login_required @login_required
def edit_book_page(request, book_id): def edit_book_page(request, book_id):
''' info about a book ''' ''' info about a book '''
book = get_or_create_book(book_id) book = models.Book.objects.get(id=book_id)
if not book.description: if not book.description:
book.description = book.parent_work.description book.description = book.parent_work.description
data = { data = {

View file

@ -18,7 +18,6 @@ Connector.objects.create(
books_url='https://openlibrary.org', books_url='https://openlibrary.org',
covers_url='https://covers.openlibrary.org', covers_url='https://covers.openlibrary.org',
search_url='https://openlibrary.org/search?q=', search_url='https://openlibrary.org/search?q=',
key_name='openlibrary_key',
) )
Connector.objects.create( Connector.objects.create(
@ -30,6 +29,5 @@ Connector.objects.create(
books_url='https://%s/book' % DOMAIN, books_url='https://%s/book' % DOMAIN,
covers_url='https://%s/images/covers' % DOMAIN, covers_url='https://%s/images/covers' % DOMAIN,
search_url='https://%s/search?q=' % DOMAIN, search_url='https://%s/search?q=' % DOMAIN,
key_name='id',
priority=1, priority=1,
) )