Update openlibrary author with ISNI

This commit is contained in:
Mouse Reeve 2021-12-05 13:24:40 -08:00
parent d7e4e6aa1e
commit 4085714764
8 changed files with 43 additions and 15 deletions

View file

@ -177,9 +177,10 @@ class AbstractConnector(AbstractMinimalConnector):
def get_or_create_author(self, remote_id, instance=None):
"""load that author"""
existing = models.Author.find_existing_by_remote_id(remote_id)
if existing:
return existing
if not instance:
existing = models.Author.find_existing_by_remote_id(remote_id)
if existing:
return existing
data = self.get_book_data(remote_id)

View file

@ -68,6 +68,7 @@ class Connector(AbstractConnector):
Mapping("born", remote_field="birth_date"),
Mapping("died", remote_field="death_date"),
Mapping("bio", formatter=get_description),
Mapping("isni", remote_field="remote_ids", formatter=get_isni),
]
def get_book_data(self, remote_id):
@ -226,6 +227,13 @@ def get_languages(language_blob):
return langs
def get_isni(remote_ids_blob):
"""extract the isni from the remote id data for the author"""
if not remote_ids_blob or not isinstance(remote_ids_blob, dict):
return None
return remote_ids_blob.get("isni")
def pick_default_edition(options):
"""favor physical copies with covers in english"""
if not options:

View file

@ -38,7 +38,7 @@ class Author(BookDataModel):
def isni_link(self):
"""generate the url from the isni id"""
clean_isni = re.sub(r"\s", "", self.isni)
return f"https://insi.org/isni/{clean_isni}"
return f"https://isni.org/isni/{clean_isni}"
@property
def openlibrary_link(self):

View file

@ -14,7 +14,7 @@
</div>
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
<div class="column is-narrow">
<a href="{{ author.local_path }}/edit">
<a href="{% url 'edit-author' author.id %}">
<span class="icon icon-pencil" title="{% trans 'Edit Author' %}" aria-hidden="True"></span>
<span class="is-hidden-mobile">{% trans "Edit Author" %}</span>
</a>
@ -73,23 +73,29 @@
{% endif %}
{% if author.openlibrary_key %}
<p class="my-1 is-flex is-justify-content-space-between">
<p class="my-1">
<a itemprop="sameAs" href="{{ author.openlibrary_link }}" target="_blank" rel="noopener">
{% trans "View on OpenLibrary" %}
</a>
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
<button class="button is-small" type="button">{% trans "Update from OpenLibrary" %}</button>
<form name="ol-update" method="POST" action="{% url 'author-update-remote' author.id 'openlibrary.org' %}">
{% csrf_token %}
<button class="button is-small" type="submit">{% trans "Update from OpenLibrary" %}</button>
</form>
{% endif %}
</p>
{% endif %}
{% if author.inventaire_id %}
<p class="my-1 is-flex is-justify-content-space-between">
<p class="my-1">
<a itemprop="sameAs" href="{{ author.inventaire_link }}" target="_blank" rel="noopener">
{% trans "View on Inventaire" %}
</a>
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
<button class="button is-small" type="button">{% trans "Update from Inventaire" %}</button>
<form name="ol-update" method="POST" action="{% url 'author-update-remote' author.id 'inventaire' %}">
{% csrf_token %}
<button class="button is-small" type="submit">{% trans "Update from Inventaire" %}</button>
</form>
{% endif %}
</p>
{% endif %}

View file

@ -93,10 +93,12 @@
{% if book.openlibrary_key %}
<p>
<a href="{{ book.openlibrary_link }}" target="_blank" rel="noopener">{% trans "View on OpenLibrary" %}</a>
{% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
<form name="ol-update" method="POST" action="{% url 'book-update-remote' book.id 'openlibrary.org' %}">
{% csrf_token %}
<button class="button is-small" type="submit">{% trans "Update from OpenLibrary" %}</button>
</form>
{% endif %}
</p>
{% endif %}
{% if book.inventaire_id %}

View file

@ -425,15 +425,26 @@ urlpatterns = [
re_path(r"^resolve-book/?$", views.resolve_book, name="resolve-book"),
re_path(r"^switch-edition/?$", views.switch_edition, name="switch-edition"),
re_path(
rf"{BOOK_PATH}/update/(?P<connector_identifier>[\w\.]+)?$",
rf"^{BOOK_PATH}/update/(?P<connector_identifier>[\w\.]+)?$",
views.update_book_from_remote,
name="book-update-remote"
name="book-update-remote",
),
re_path(
r"^author/(?P<author_id>\d+)/update/(?P<connector_identifier>[\w\.]+)?$",
views.update_author_from_remote,
name="author-update-remote",
),
# isbn
re_path(r"^isbn/(?P<isbn>\d+)(.json)?/?$", views.Isbn.as_view()),
# author
re_path(r"^author/(?P<author_id>\d+)(.json)?/?$", views.Author.as_view()),
re_path(r"^author/(?P<author_id>\d+)/edit/?$", views.EditAuthor.as_view()),
re_path(
r"^author/(?P<author_id>\d+)(.json)?/?$", views.Author.as_view(), name="author"
),
re_path(
r"^author/(?P<author_id>\d+)/edit/?$",
views.EditAuthor.as_view(),
name="edit-author",
),
# reading progress
re_path(r"^edit-readthrough/?$", views.edit_readthrough, name="edit-readthrough"),
re_path(r"^delete-readthrough/?$", views.delete_readthrough),

View file

@ -55,7 +55,7 @@ from .imports.manually_review import (
)
# misc views
from .author import Author, EditAuthor
from .author import Author, EditAuthor, update_author_from_remote
from .directory import Directory
from .discover import Discover
from .feed import DirectMessage, Feed, Replies, Status

View file

@ -86,7 +86,7 @@ def update_author_from_remote(request, author_id, connector_identifier):
connector = connector_manager.load_connector(
get_object_or_404(models.Connector, identifier=connector_identifier)
)
author = get_object_or_404(models.Book.objects.select_subclasses(), id=author_id)
author = get_object_or_404(models.Author, id=author_id)
connector.update_author_from_remote(author)