function moved

This commit is contained in:
Willi Hohenstein 2022-02-13 19:07:25 +01:00
parent a4b08d7213
commit 3c05cecb50

View file

@ -24,6 +24,8 @@ class Search(View):
def get(self, request):
"""that search bar up top"""
query = request.GET.get("q")
# check if query is isbn
query = isbn_check(query)
min_confidence = request.GET.get("min_confidence", 0)
search_type = request.GET.get("type")
search_remote = (
@ -123,3 +125,29 @@ def list_search(query, viewer, *_):
)
.order_by("-similarity")
), None
def isbn_check(query):
n = query.replace('-','').replace(' ', '')
if len(n) == 13:
# Multiply every other digit by 3
# Add these numbers and the other digits
product = (sum(int(ch) for ch in n[::2])
+ sum(int(ch) * 3 for ch in n[1::2]))
if product % 10 == 0:
return n
elif len(n) == 10:
if n[0:8].isdigit() and (n[9].isdigit() or n[9].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(n[i]) * (10 - i)
# Handle last character
if n[9].lower() == "x":
product += 10
else:
product += int(n[9])
if product % 11 == 0:
return n
return query