From 4085714764af94157e740848f1c90472f0b16e5c Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 5 Dec 2021 13:24:40 -0800 Subject: [PATCH] Update openlibrary author with ISNI --- bookwyrm/connectors/abstract_connector.py | 7 ++++--- bookwyrm/connectors/openlibrary.py | 8 ++++++++ bookwyrm/models/author.py | 2 +- bookwyrm/templates/author/author.html | 16 +++++++++++----- bookwyrm/templates/book/book.html | 2 ++ bookwyrm/urls.py | 19 +++++++++++++++---- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/author.py | 2 +- 8 files changed, 43 insertions(+), 15 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 48d2b4cb..4b1f6e58 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -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) diff --git a/bookwyrm/connectors/openlibrary.py b/bookwyrm/connectors/openlibrary.py index 225174c8..c15277f8 100644 --- a/bookwyrm/connectors/openlibrary.py +++ b/bookwyrm/connectors/openlibrary.py @@ -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: diff --git a/bookwyrm/models/author.py b/bookwyrm/models/author.py index ce0d027c..5cc11afd 100644 --- a/bookwyrm/models/author.py +++ b/bookwyrm/models/author.py @@ -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): diff --git a/bookwyrm/templates/author/author.html b/bookwyrm/templates/author/author.html index c2355955..6e93d313 100644 --- a/bookwyrm/templates/author/author.html +++ b/bookwyrm/templates/author/author.html @@ -14,7 +14,7 @@ {% if request.user.is_authenticated and perms.bookwyrm.edit_book %}
- + {% trans "Edit Author" %} @@ -73,23 +73,29 @@ {% endif %} {% if author.openlibrary_key %} -

+

{% trans "View on OpenLibrary" %} {% if request.user.is_authenticated and perms.bookwyrm.edit_book %} - +

+ {% csrf_token %} + +
{% endif %}

{% endif %} {% if author.inventaire_id %} -

+

{% trans "View on Inventaire" %} {% if request.user.is_authenticated and perms.bookwyrm.edit_book %} - +

+ {% csrf_token %} + +
{% endif %}

{% endif %} diff --git a/bookwyrm/templates/book/book.html b/bookwyrm/templates/book/book.html index 3a7b1cbc..cdfbdf98 100644 --- a/bookwyrm/templates/book/book.html +++ b/bookwyrm/templates/book/book.html @@ -93,10 +93,12 @@ {% if book.openlibrary_key %}

{% trans "View on OpenLibrary" %} + {% if request.user.is_authenticated and perms.bookwyrm.edit_book %}

{% csrf_token %}
+ {% endif %}

{% endif %} {% if book.inventaire_id %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 697028b7..6e53ef2a 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -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[\w\.]+)?$", + rf"^{BOOK_PATH}/update/(?P[\w\.]+)?$", views.update_book_from_remote, - name="book-update-remote" + name="book-update-remote", + ), + re_path( + r"^author/(?P\d+)/update/(?P[\w\.]+)?$", + views.update_author_from_remote, + name="author-update-remote", ), # isbn re_path(r"^isbn/(?P\d+)(.json)?/?$", views.Isbn.as_view()), # author - re_path(r"^author/(?P\d+)(.json)?/?$", views.Author.as_view()), - re_path(r"^author/(?P\d+)/edit/?$", views.EditAuthor.as_view()), + re_path( + r"^author/(?P\d+)(.json)?/?$", views.Author.as_view(), name="author" + ), + re_path( + r"^author/(?P\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), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index b399b90a..837b1257 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -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 diff --git a/bookwyrm/views/author.py b/bookwyrm/views/author.py index 838d1c2b..2acb3b19 100644 --- a/bookwyrm/views/author.py +++ b/bookwyrm/views/author.py @@ -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)