Revert part of "Hyphenate ISBN numbers and add copy button" related to hyphenation

This partially reverts commit d2c4785af1.
This commit is contained in:
axiomizer 2023-07-22 13:57:51 -04:00
parent d2c4785af1
commit 1bda8a5d9d
6 changed files with 4 additions and 7981 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,71 +0,0 @@
import os
import requests
from xml.etree import ElementTree
from bookwyrm import settings
class IsbnHyphenator:
__range_message_url = "https://www.isbn-international.org/export_rangemessage.xml"
__range_file_path = os.path.join(
settings.BASE_DIR, "bookwyrm", "isbn", "RangeMessage.xml"
)
__element_tree = None
def update_range_message(self):
response = requests.get(self.__range_message_url)
with open(self.__range_file_path, "w", encoding="utf-8") as file:
file.write(response.text)
self.__element_tree = None
def hyphenate(self, isbn_13):
if self.__element_tree is None:
self.__element_tree = ElementTree.parse(self.__range_file_path)
gs1_prefix = isbn_13[:3]
reg_group = self.__find_reg_group(isbn_13, gs1_prefix)
if reg_group is None:
return isbn_13 # failed to hyphenate
registrant = self.__find_registrant(isbn_13, gs1_prefix, reg_group)
if registrant is None:
return isbn_13 # failed to hyphenate
publication = isbn_13[len(gs1_prefix) + len(reg_group) + len(registrant) : -1]
check_digit = isbn_13[-1:]
return "-".join((gs1_prefix, reg_group, registrant, publication, check_digit))
def __find_reg_group(self, isbn_13, gs1_prefix):
for ean_ucc_el in self.__element_tree.find("EAN.UCCPrefixes").findall(
"EAN.UCC"
):
if ean_ucc_el.find("Prefix").text == gs1_prefix:
for rule_el in ean_ucc_el.find("Rules").findall("Rule"):
length = int(rule_el.find("Length").text)
if length == 0:
continue
range = [
int(x[:length]) for x in rule_el.find("Range").text.split("-")
]
reg_group = isbn_13[len(gs1_prefix) : len(gs1_prefix) + length]
if range[0] <= int(reg_group) <= range[1]:
return reg_group
return None
return None
def __find_registrant(self, isbn_13, gs1_prefix, reg_group):
from_ind = len(gs1_prefix) + len(reg_group)
for group_el in self.__element_tree.find("RegistrationGroups").findall("Group"):
if group_el.find("Prefix").text == "-".join((gs1_prefix, reg_group)):
for rule_el in group_el.find("Rules").findall("Rule"):
length = int(rule_el.find("Length").text)
if length == 0:
continue
range = [
int(x[:length]) for x in rule_el.find("Range").text.split("-")
]
registrant = isbn_13[from_ind : from_ind + length]
if range[0] <= int(registrant) <= range[1]:
return registrant
return None
return None
hyphenator_singleton = IsbnHyphenator()

View file

@ -537,8 +537,8 @@ let BookWyrm = new (class {
setTimeout(function () { setTimeout(function () {
tooltipEl.style.visibility = "hidden"; tooltipEl.style.visibility = "hidden";
tooltipEl.style.opacity = 0; tooltipEl.style.opacity = 0;
}, 3000) }, 3000);
}) });
} }
/** /**

View file

@ -6,7 +6,7 @@
{% if book.isbn_13 %} {% if book.isbn_13 %}
<div class="is-flex is-flex-wrap-wrap"> <div class="is-flex is-flex-wrap-wrap">
<dt class="mr-1">{% trans "ISBN:" %}</dt> <dt class="mr-1">{% trans "ISBN:" %}</dt>
<dd itemprop="isbn" class="mr-1" id="isbn_content">{{ hyphenated_isbn13 }}</dd> <dd itemprop="isbn" class="mr-1" id="isbn_content">{{ book.isbn_13 }}</dd>
<div> <div>
<button class="button is-small" data-copywithtooltip data-content-id="isbn_content" data-tooltip-id="isbn_tooltip"> <button class="button is-small" data-copywithtooltip data-content-id="isbn_content" data-tooltip-id="isbn_tooltip">
<span class="icon icon-copy" title="{% trans "Copy ISBN" %}"></span> <span class="icon icon-copy" title="{% trans "Copy ISBN" %}"></span>

View file

@ -14,7 +14,6 @@ from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager, ConnectorException from bookwyrm.connectors import connector_manager, ConnectorException
from bookwyrm.connectors.abstract_connector import get_image from bookwyrm.connectors.abstract_connector import get_image
from bookwyrm.isbn.isbn import hyphenator_singleton as hyphenator
from bookwyrm.settings import PAGE_LENGTH from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path from bookwyrm.views.helpers import is_api_request, maybe_redirect_local_path
@ -91,7 +90,6 @@ class Book(View):
"rating": reviews.aggregate(Avg("rating"))["rating__avg"], "rating": reviews.aggregate(Avg("rating"))["rating__avg"],
"lists": lists, "lists": lists,
"update_error": kwargs.get("update_error", False), "update_error": kwargs.get("update_error", False),
"hyphenated_isbn13": hyphenator.hyphenate(book.isbn_13),
} }
if request.user.is_authenticated: if request.user.is_authenticated: