mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 03:51:08 +00:00
Merge pull request #1942 from willhoh/isbn_search
Isbn check befor search
This commit is contained in:
commit
23eb1c1b10
1 changed files with 34 additions and 0 deletions
|
@ -24,6 +24,8 @@ class Search(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
"""that search bar up top"""
|
"""that search bar up top"""
|
||||||
query = request.GET.get("q")
|
query = request.GET.get("q")
|
||||||
|
# check if query is isbn
|
||||||
|
query = isbn_check(query)
|
||||||
min_confidence = request.GET.get("min_confidence", 0)
|
min_confidence = request.GET.get("min_confidence", 0)
|
||||||
search_type = request.GET.get("type")
|
search_type = request.GET.get("type")
|
||||||
search_remote = (
|
search_remote = (
|
||||||
|
@ -123,3 +125,35 @@ def list_search(query, viewer, *_):
|
||||||
)
|
)
|
||||||
.order_by("-similarity")
|
.order_by("-similarity")
|
||||||
), None
|
), None
|
||||||
|
|
||||||
|
|
||||||
|
def isbn_check(query):
|
||||||
|
"""isbn10 or isbn13 check, if so remove separators"""
|
||||||
|
if query:
|
||||||
|
su_num = re.sub(r"(?<=\d)\D(?=\d|[xX])", "", query)
|
||||||
|
if len(su_num) == 13 and su_num.isdecimal():
|
||||||
|
# Multiply every other digit by 3
|
||||||
|
# Add these numbers and the other digits
|
||||||
|
product = sum(int(ch) for ch in su_num[::2]) + sum(
|
||||||
|
int(ch) * 3 for ch in su_num[1::2]
|
||||||
|
)
|
||||||
|
if product % 10 == 0:
|
||||||
|
return su_num
|
||||||
|
elif (
|
||||||
|
len(su_num) == 10
|
||||||
|
and su_num[:-1].isdecimal()
|
||||||
|
and (su_num[-1].isdecimal() or su_num[-1].lower() == "x")
|
||||||
|
):
|
||||||
|
product = 0
|
||||||
|
# Iterate through code_string
|
||||||
|
for i in range(9):
|
||||||
|
# for each character, multiply by a different decreasing number: 10 - x
|
||||||
|
product = product + int(su_num[i]) * (10 - i)
|
||||||
|
# Handle last character
|
||||||
|
if su_num[9].lower() == "x":
|
||||||
|
product += 10
|
||||||
|
else:
|
||||||
|
product += int(su_num[9])
|
||||||
|
if product % 11 == 0:
|
||||||
|
return su_num
|
||||||
|
return query
|
||||||
|
|
Loading…
Reference in a new issue