Update existing authors when isni data available

When an existing author is selected as a new author when editing a book,
if they have an ISNI ID recorded we check the record and augment the local
database record from the ISNI data.

Also dedupes author aliases for this feature and when adding a completely
new author.
This commit is contained in:
Hugh Rundle 2021-11-01 19:50:49 +11:00
parent c845b7a5d0
commit 6556090524
4 changed files with 47 additions and 2 deletions

View file

@ -78,6 +78,7 @@
<p class="help ml-5">
{{ author.existing_isnis|get_isni_bio:match }}
</p>
{{ author.existing_isnis|get_isni:match }}
{% endfor %}
<label class="label mt-2">
<input type="radio" name="author_match-{{ counter }}" value="{{ author.name }}" required> {% trans "This is a new author" %}

View file

@ -3,6 +3,7 @@ import os
import re
from uuid import uuid4
from django import template
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django.template.defaultfilters import stringfilter
from django.templatetags.static import static
@ -83,6 +84,21 @@ def get_isni_bio(existing, author):
return ""
@register.filter(name="get_isni", needs_autoescape=True)
def get_isni(existing, author, autoescape=True):
"""Returns the isni ID if an existing author has an ISNI listing"""
auth_isni = re.sub(r"\D", "", str(author.isni))
if len(existing) == 0:
return ""
for value in existing:
if "isni" in value and auth_isni == re.sub(r"\D", "", str(value["isni"])):
isni = value["isni"]
return mark_safe(
f'<input type="text" name="isni-for-{author.id}" value="{isni}" hidden>'
)
return ""
@register.filter(name="remove_spaces")
@stringfilter
def remove_spaces(arg):

View file

@ -117,7 +117,8 @@ def get_author_isni_data(isni):
aliases = element.findall(".//personalNameVariant")
for entry in aliases:
author["aliases"].append(make_name_string(entry))
# dedupe aliases
author["aliases"] = list(set(author["aliases"]))
return author
@ -130,3 +131,22 @@ def build_author_dict(match_value):
return get_author_isni_data(isni)
# otherwise it's a name string
return {"name": match_value}
def augment_author_metadata(author, isni):
"""Update any missing author fields from ISNI data"""
isni_data = get_author_isni_data(isni)
author.viaf_id = (
isni_data["viaf_id"] if len(author.viaf_id) == 0 else author.viaf_id
)
author.wikipedia_link = (
isni_data["wikipedia_link"]
if len(author.wikipedia_link) == 0
else author.wikipedia_link
)
author.bio = isni_data["bio"] if len(author.bio) == 0 else author.bio
aliases = set(isni_data["aliases"])
for x in author.aliases:
aliases.add(x)
author.aliases = list(aliases)
author.save()

View file

@ -12,7 +12,11 @@ from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import book_search, forms, models
from bookwyrm.utils.isni import find_authors_by_name, build_author_dict
from bookwyrm.utils.isni import (
find_authors_by_name,
build_author_dict,
augment_author_metadata,
)
from bookwyrm.views.helpers import get_edition
from .books import set_cover_from_url
@ -169,6 +173,10 @@ class ConfirmEditBook(View):
author = get_object_or_404(
models.Author, id=request.POST[f"author_match-{i}"]
)
# update author metadata if the ISNI record is more complete
isni = request.POST.get(f"isni-for-{match}", None)
if isni is not None:
augment_author_metadata(author, isni)
except ValueError:
# otherwise it's a name with or without isni id
author_data = build_author_dict(match)