moviewyrm/bookwyrm/views/search.py

154 lines
5.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" search views"""
2021-01-13 20:03:27 +00:00
import re
from django.contrib.postgres.search import TrigramSimilarity
2021-05-01 17:47:01 +00:00
from django.core.paginator import Paginator
2021-01-13 20:03:27 +00:00
from django.db.models.functions import Greatest
from django.http import JsonResponse
from django.template.response import TemplateResponse
from django.views import View
from bookwyrm import models
from bookwyrm.connectors import connector_manager
from bookwyrm.book_search import search, format_search_result
2021-05-01 17:47:01 +00:00
from bookwyrm.settings import PAGE_LENGTH
2021-01-13 20:03:27 +00:00
from bookwyrm.utils import regex
2021-10-06 17:37:09 +00:00
from .helpers import is_api_request
2021-01-13 20:03:27 +00:00
from .helpers import handle_remote_webfinger
# pylint: disable= no-self-use
class Search(View):
2021-04-26 16:15:42 +00:00
"""search users or books"""
2021-03-08 16:49:10 +00:00
2021-05-01 01:59:02 +00:00
def get(self, request):
2021-04-26 16:15:42 +00:00
"""that search bar up top"""
2021-03-08 16:49:10 +00:00
query = request.GET.get("q")
2022-02-13 18:07:25 +00:00
# check if query is isbn
2022-02-13 19:30:11 +00:00
query = isbn_check(query)
min_confidence = request.GET.get("min_confidence", 0)
2021-05-01 01:59:02 +00:00
search_type = request.GET.get("type")
search_remote = (
request.GET.get("remote", False) and request.user.is_authenticated
)
2021-01-13 20:03:27 +00:00
if is_api_request(request):
# only return local book results via json so we don't cascade
2021-09-14 22:26:18 +00:00
book_results = search(query, min_confidence=min_confidence)
return JsonResponse(
[format_search_result(r) for r in book_results], safe=False
)
2021-01-13 20:03:27 +00:00
2021-05-20 23:34:32 +00:00
if query and not search_type:
2021-05-01 01:59:02 +00:00
search_type = "user" if "@" in query else "book"
endpoints = {
"book": book_search,
"user": user_search,
"list": list_search,
}
if not search_type in endpoints:
search_type = "book"
2021-05-01 02:19:10 +00:00
data = {
"query": query or "",
"type": search_type,
2021-05-01 02:56:29 +00:00
"remote": search_remote,
2021-05-01 02:19:10 +00:00
}
2021-05-01 02:56:29 +00:00
if query:
2021-07-28 20:52:16 +00:00
results, search_remote = endpoints[search_type](
2021-05-01 02:56:29 +00:00
query, request.user, min_confidence, search_remote
)
2021-05-02 05:20:23 +00:00
if results:
paginated = Paginator(results, PAGE_LENGTH).get_page(
request.GET.get("page")
)
data["results"] = paginated
2021-07-28 20:52:16 +00:00
data["remote"] = search_remote
2021-05-01 01:35:09 +00:00
2021-09-18 18:32:00 +00:00
return TemplateResponse(request, f"search/{search_type}.html", data)
2021-05-01 01:06:30 +00:00
def book_search(query, user, min_confidence, search_remote=False):
2021-05-01 02:19:10 +00:00
"""the real business is elsewhere"""
# try a local-only search
2021-09-14 22:26:18 +00:00
results = [{"results": search(query, min_confidence=min_confidence)}]
2021-10-03 16:44:27 +00:00
if not user.is_authenticated or (results[0]["results"] and not search_remote):
2021-09-14 22:26:18 +00:00
return results, False
# if there were no local results, or the request was for remote, search all sources
results += connector_manager.search(query, min_confidence=min_confidence)
return results, True
2021-05-01 01:06:30 +00:00
2021-05-01 02:56:29 +00:00
def user_search(query, viewer, *_):
2021-05-01 02:19:10 +00:00
"""cool kids members only user search"""
2021-05-01 01:06:30 +00:00
# logged out viewers can't search users
if not viewer.is_authenticated:
2021-07-28 23:43:49 +00:00
return models.User.objects.none(), None
2021-05-01 01:06:30 +00:00
# use webfinger for mastodon style account@domain.com username to load the user if
# they don't exist locally (handle_remote_webfinger will check the db)
2021-06-18 21:12:56 +00:00
if re.match(regex.FULL_USERNAME, query):
2021-05-01 01:06:30 +00:00
handle_remote_webfinger(query)
2021-05-01 02:19:10 +00:00
return (
models.User.viewer_aware_objects(viewer)
.annotate(
similarity=Greatest(
TrigramSimilarity("username", query),
TrigramSimilarity("localname", query),
2021-03-08 16:49:10 +00:00
)
2021-05-01 02:19:10 +00:00
)
.filter(
similarity__gt=0.5,
)
2021-10-03 16:38:41 +00:00
.order_by("-similarity")
2021-07-28 20:52:16 +00:00
), None
2021-05-01 01:06:30 +00:00
2021-01-13 20:03:27 +00:00
2021-05-01 02:56:29 +00:00
def list_search(query, viewer, *_):
2021-05-01 01:06:30 +00:00
"""any relevent lists?"""
2021-05-01 02:19:10 +00:00
return (
2021-10-06 17:37:09 +00:00
models.List.privacy_filter(
2021-05-01 02:19:10 +00:00
viewer,
privacy_levels=["public", "followers"],
)
.annotate(
similarity=Greatest(
TrigramSimilarity("name", query),
TrigramSimilarity("description", query),
2021-02-01 19:50:47 +00:00
)
2021-05-01 02:19:10 +00:00
)
.filter(
similarity__gt=0.1,
)
2021-10-03 16:38:41 +00:00
.order_by("-similarity")
2021-07-28 20:52:16 +00:00
), None
2022-02-13 18:07:25 +00:00
def isbn_check(query):
2022-02-13 19:30:11 +00:00
if query:
su_num = query.replace("-", "").replace(" ", "")
if len(su_num) == 13:
# 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:
if su_num[0:8].isdigit() and (su_num[9].isdigit() or su_num[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(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
2022-02-13 18:07:25 +00:00
return query