code cleanup

This commit is contained in:
Hugh Rundle 2021-11-22 12:52:59 +11:00
parent 09c5a3861b
commit a9d921cc06
No known key found for this signature in database
GPG key ID: CD23D6039184286B
3 changed files with 33 additions and 35 deletions

View file

@ -92,7 +92,7 @@ def get_isni(existing, author, autoescape=True):
if len(existing) == 0:
return ""
for value in existing:
if hasattr(value, "isni")and auth_isni == re.sub(r"\D", "", str(value.isni)):
if hasattr(value, "isni") 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>'

View file

@ -4,6 +4,7 @@ import requests
from bookwyrm import activitypub, models
def request_isni_data(search_index, search_term, max_records=5):
"""Request data from the ISNI API"""
@ -18,11 +19,7 @@ def request_isni_data(search_index, search_term, max_records=5):
"recordPacking": "xml",
"sortKeys": "RLV,pica,0,,",
}
result = requests.get(
"http://isni.oclc.org/sru/",
params=query_params,
timeout=10
)
result = requests.get("http://isni.oclc.org/sru/", params=query_params, timeout=10)
# the OCLC ISNI server asserts the payload is encoded
# in latin1, but we know better
result.encoding = "utf-8"
@ -59,8 +56,7 @@ def get_other_identifier(element, code):
code_of_source = source.find(".//codeOfSource")
if (
code_of_source is not None
and code_of_source.text == code.upper()
or code_of_source.text == code.lower()
and code_of_source.text.lower() == code.lower()
):
return source.find(".//sourceIdentifier").text
@ -115,9 +111,11 @@ def find_authors_by_name(name_string, description=False):
# some of the "titles" in ISNI are a little ...iffy
# '@' is used by ISNI/OCLC to index the starting point ignoring stop words
# (e.g. "The @Government of no one")
title_elements = [e for e in titles if not e.text.replace('@', '').isnumeric()]
title_elements = [
e for e in titles if not e.text.replace("@", "").isnumeric()
]
if len(title_elements):
author.bio = title_elements[0].text.replace('@', '')
author.bio = title_elements[0].text.replace("@", "")
else:
author.bio = None
@ -155,18 +153,19 @@ def get_author_from_isni(isni):
viafId=viaf,
aliases=aliases,
bio=bio,
wikipediaLink=wikipedia
wikipediaLink=wikipedia,
)
return author
def build_author_from_isni(match_value):
"""Build dict with basic author details from ISNI or author name"""
"""Build basic author class object from ISNI URL"""
# if it is an isni value get the data
if match_value.startswith("https://isni.org/isni/"):
isni = match_value.replace("https://isni.org/isni/", "")
return { "author": get_author_from_isni(isni) }
return {"author": get_author_from_isni(isni)}
# otherwise it's a name string
return {}

View file

@ -12,6 +12,7 @@ from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import book_search, forms, models
# from bookwyrm.activitypub.base_activity import ActivityObject
from bookwyrm.utils.isni import (
find_authors_by_name,
@ -72,8 +73,7 @@ class EditBook(View):
)
isni_authors = find_authors_by_name(
author,
description=True
author, description=True
) # find matches from ISNI API
# dedupe isni authors we already have in the DB
@ -187,12 +187,11 @@ class ConfirmEditBook(View):
author_object = build_author_from_isni(isni_match)
# with author data class from isni id
if "author" in author_object:
# TESTING
skeleton = models.Author.objects.create(name=author_object["author"].name)
skeleton = models.Author.objects.create(
name=author_object["author"].name
)
author = author_object["author"].to_model(
model=models.Author,
overwrite=True,
instance=skeleton
model=models.Author, overwrite=True, instance=skeleton
)
else:
# or it's just a name