mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-24 09:00:33 +00:00
Path to update books
This commit is contained in:
parent
de4f783891
commit
3a4a194160
9 changed files with 68 additions and 33 deletions
|
@ -57,10 +57,10 @@ def first_search_result(query):
|
|||
return None
|
||||
|
||||
|
||||
def update_book(book):
|
||||
def update_book(book, data=None):
|
||||
''' re-sync with the original data source '''
|
||||
connector = load_connector(book.connector)
|
||||
connector.update_book(book)
|
||||
connector.update_book(book, data=data)
|
||||
|
||||
|
||||
def get_connectors():
|
||||
|
|
|
@ -57,7 +57,7 @@ class AbstractConnector(ABC):
|
|||
|
||||
|
||||
@abstractmethod
|
||||
def update_book(self, book_obj):
|
||||
def update_book(self, book_obj, data=None):
|
||||
''' sync a book with the canonical remote copy '''
|
||||
# return book model obj
|
||||
|
||||
|
|
|
@ -140,7 +140,7 @@ class Connector(AbstractConnector):
|
|||
return book
|
||||
|
||||
|
||||
def update_book(self, book):
|
||||
def update_book(self, book, data=None):
|
||||
''' load new data '''
|
||||
if not book.sync and not book.sync_cover:
|
||||
return
|
||||
|
|
|
@ -23,7 +23,6 @@ class Connector(AbstractConnector):
|
|||
SearchVector('isbn_10', weight='A') +\
|
||||
SearchVector('openlibrary_key', weight='B') +\
|
||||
SearchVector('goodreads_key', weight='B') +\
|
||||
SearchVector('source_url', weight='B') +\
|
||||
SearchVector('asin', weight='B') +\
|
||||
SearchVector('oclc_number', weight='B') +\
|
||||
SearchVector('description', weight='C') +\
|
||||
|
|
|
@ -10,7 +10,7 @@ from django.http import HttpResponseBadRequest, HttpResponseNotFound
|
|||
from django.views.decorators.csrf import csrf_exempt
|
||||
import requests
|
||||
|
||||
from fedireads import models, outgoing
|
||||
from fedireads import books_manager, models, outgoing
|
||||
from fedireads import status as status_builder
|
||||
from fedireads.remote_user import get_or_create_remote_user
|
||||
from fedireads.tasks import app
|
||||
|
@ -63,7 +63,7 @@ def shared_inbox(request):
|
|||
},
|
||||
'Update': {
|
||||
'Person': None,# TODO: handle_update_user
|
||||
'Document': None# TODO: handle_update_book
|
||||
'Document': handle_update_book,
|
||||
},
|
||||
}
|
||||
activity_type = activity['type']
|
||||
|
@ -320,3 +320,18 @@ def handle_tag(activity):
|
|||
if not user.local:
|
||||
book = activity['target']['id'].split('/')[-1]
|
||||
status_builder.create_tag(user, book, activity['object']['name'])
|
||||
|
||||
|
||||
@app.task
|
||||
def handle_update_book(activity):
|
||||
''' a remote instance changed a book (Document) '''
|
||||
document = activity['object']
|
||||
# check if we have their copy and care about their updates
|
||||
book = models.Book.objects.select_subclasses().filter(
|
||||
remote_id=document['url'],
|
||||
sync=True,
|
||||
).first()
|
||||
if not book:
|
||||
return
|
||||
|
||||
books_manager.update_book(book, data=document)
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
# Generated by Django 3.0.3 on 2020-05-04 00:49
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('fedireads', '0036_auto_20200503_2007'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='author',
|
||||
name='fedireads_key',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='book',
|
||||
name='fedireads_key',
|
||||
),
|
||||
]
|
41
fedireads/migrations/0037_auto_20200504_0154.py
Normal file
41
fedireads/migrations/0037_auto_20200504_0154.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
# Generated by Django 3.0.3 on 2020-05-04 01:54
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('fedireads', '0036_auto_20200503_2007'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='author',
|
||||
name='fedireads_key',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='book',
|
||||
name='fedireads_key',
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name='book',
|
||||
name='source_url',
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='author',
|
||||
name='last_sync_date',
|
||||
field=models.DateTimeField(default=django.utils.timezone.now),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='author',
|
||||
name='sync',
|
||||
field=models.BooleanField(default=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='remote_id',
|
||||
field=models.CharField(max_length=255, null=True),
|
||||
),
|
||||
]
|
|
@ -1,6 +1,4 @@
|
|||
''' database schema for books and shelves '''
|
||||
from uuid import uuid4
|
||||
|
||||
from django.utils import timezone
|
||||
from django.db import models
|
||||
from model_utils.managers import InheritanceManager
|
||||
|
@ -50,6 +48,7 @@ class Connector(FedireadsModel):
|
|||
|
||||
class Book(FedireadsModel):
|
||||
''' a generic book, which can mean either an edition or a work '''
|
||||
remote_id = models.CharField(max_length=255, null=True)
|
||||
# these identifiers apply to both works and editions
|
||||
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
|
||||
librarything_key = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
@ -57,7 +56,6 @@ class Book(FedireadsModel):
|
|||
misc_identifiers = JSONField(null=True)
|
||||
|
||||
# info about where the data comes from and where/if to sync
|
||||
source_url = models.CharField(max_length=255, unique=True, null=True)
|
||||
sync = models.BooleanField(default=True)
|
||||
sync_cover = models.BooleanField(default=True)
|
||||
last_sync_date = models.DateTimeField(default=timezone.now)
|
||||
|
@ -130,6 +128,7 @@ class Edition(Book):
|
|||
''' an edition of a book '''
|
||||
# default -> this is what gets displayed for a work
|
||||
default = models.BooleanField(default=False)
|
||||
|
||||
# these identifiers only apply to editions, not works
|
||||
isbn_10 = models.CharField(max_length=255, blank=True, null=True)
|
||||
isbn_13 = models.CharField(max_length=255, blank=True, null=True)
|
||||
|
@ -152,6 +151,8 @@ class Edition(Book):
|
|||
class Author(FedireadsModel):
|
||||
''' copy of an author from OL '''
|
||||
openlibrary_key = models.CharField(max_length=255, blank=True, null=True)
|
||||
sync = models.BooleanField(default=True)
|
||||
last_sync_date = models.DateTimeField(default=timezone.now)
|
||||
wikipedia_link = models.CharField(max_length=255, blank=True, null=True)
|
||||
# idk probably other keys would be useful here?
|
||||
born = models.DateTimeField(blank=True, null=True)
|
||||
|
|
|
@ -36,5 +36,5 @@ Connector.objects.create(
|
|||
)
|
||||
|
||||
|
||||
get_or_create_book('OL1715344W')
|
||||
get_or_create_book('OL102749W')
|
||||
get_or_create_book('OL1715344W', key='openlibrary_key', connector_id=1)
|
||||
get_or_create_book('OL102749W', key='openlibrary_key', connector_id=1)
|
||||
|
|
Loading…
Reference in a new issue