Skip to content

Commit

Permalink
index and search user journal
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Dec 30, 2024
1 parent bea87f2 commit 6d377c4
Show file tree
Hide file tree
Showing 57 changed files with 2,487 additions and 1,197 deletions.
3 changes: 3 additions & 0 deletions boofilsic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
NEODB_SENTRY_DSN=(str, ""),
NEODB_SENTRY_SAMPLE_RATE=(float, 0),
NEODB_FANOUT_LIMIT_DAYS=(int, 9),
INDEX_ALIASES=(dict, {}),
)

# ====== End of user configuration variables ======
Expand Down Expand Up @@ -561,6 +562,8 @@ def _init_language_settings(preferred_lanugages_env):

SEARCH_INDEX_NEW_ONLY = False

INDEX_ALIASES = env("INDEX_ALIASES")

DOWNLOADER_SAVEDIR = env("NEODB_DOWNLOADER_SAVE_DIR", default="/tmp") # type: ignore

DISABLE_MODEL_SIGNAL = False # disable index and social feeds during importing/etc
Expand Down
6 changes: 6 additions & 0 deletions catalog/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def get_localized_subtitle(self) -> str | None:
def display_subtitle(self) -> str | None:
return self.get_localized_subtitle()

def to_indexable_titles(self) -> list[str]:
titles = [t["text"] for t in self.localized_title if t]
titles += [t["text"] for t in self.localized_subtitle if t]
titles += [self.orig_title] if self.orig_title else []
return list(set(titles))

@property
def isbn10(self):
return isbn_13_to_10(self.isbn)
Expand Down
4 changes: 4 additions & 0 deletions catalog/collection/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Collection(Item):
journal_item: "JournalCollection"
category = ItemCategory.Collection

@property
def url(self):
return self.journal_item.url if self.journal_item else super().url

@property
def owner_id(self):
return self.journal_item.owner_id if self.journal_item else None
14 changes: 13 additions & 1 deletion catalog/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def clear(self):
res.save()

def __str__(self):
return f"{self.__class__.__name__}|{self.pk}|{self.uuid} {self.primary_lookup_id_type}:{self.primary_lookup_id_value if self.primary_lookup_id_value else ''} ({self.title})"
return f"{self.__class__.__name__}|{self.pk}|{self.uuid} {self.primary_lookup_id_type}:{self.primary_lookup_id_value if self.primary_lookup_id_value else ''} ({self.display_title})"

@classmethod
def lookup_id_type_choices(cls):
Expand Down Expand Up @@ -567,6 +567,12 @@ def merge_to(self, to_item: "Item | None"):
res.item = to_item
res.save()

@property
def final_item(self) -> Self:
if self.merged_to_item:
return self.merged_to_item.final_item
return self

def recast_to(self, model: "type[Any]") -> "Item":
logger.warning(f"recast item {self} to {model}")
if isinstance(self, model):
Expand Down Expand Up @@ -657,6 +663,12 @@ def display_description(self) -> str:
def brief_description(self):
return (str(self.display_description) or "")[:155]

def to_indexable_titles(self) -> list[str]:
titles = [t["text"] for t in self.localized_title if t]
if self.parent_item:
titles += self.parent_item.to_indexable_titles()
return list(set(titles))

@classmethod
def get_by_url(cls, url_or_b62: str, resolve_merge=False) -> "Self | None":
b62 = url_or_b62.strip().split("/")[-1]
Expand Down
5 changes: 5 additions & 0 deletions catalog/movie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,8 @@ def lookup_id_cleanup(cls, lookup_id_type, lookup_id_value):
else:
return None, None
return super().lookup_id_cleanup(lookup_id_type, lookup_id_value)

def to_indexable_titles(self) -> list[str]:
titles = [t["text"] for t in self.localized_title if t]
titles += [self.orig_title] if self.orig_title else []
return list(set(titles))
12 changes: 7 additions & 5 deletions catalog/search/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import re
from urllib.parse import quote

import django_rq
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.cache import cache
from django.core.exceptions import BadRequest
from django.shortcuts import redirect, render
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views.decorators.http import require_http_methods
from rq.job import Job

from catalog.common.models import ItemCategory, SiteName
from catalog.common.sites import AbstractSite, SiteManager
from common.models import int_
from common.utils import (
HTTPResponseHXRedirect,
PageLinksGenerator,
Expand All @@ -37,7 +40,7 @@ def fetch_refresh(request, job_id):
else:
return HTTPResponseHXRedirect(item_url)
else:
retry = int(request.GET.get("retry", 0)) + 1
retry = int_(request.GET.get("retry", 0)) + 1
if retry > 10:
return render(request, "_fetch_failed.html")
else:
Expand Down Expand Up @@ -97,10 +100,10 @@ def visible_categories(request):

@user_identity_required
def search(request):
category = request.GET.get("c", default="all").strip().lower()
keywords = request.GET.get("q", default="").strip()
if re.match(r"^[@@]", keywords):
return query_identity(request, keywords.replace("@", "@"))
category = request.GET.get("c", default="all").strip().lower()
hide_category = False
if category == "all" or not category:
category = None
Expand All @@ -115,8 +118,7 @@ def search(request):
categories = visible_categories(request)
tag = request.GET.get("tag", default="").strip()
tag = Tag.deep_cleanup_title(tag, default="")
p = request.GET.get("page", default="1")
p = int(p) if p.isdigit() else 1
p = int_(request.GET.get("page", default="1"), 1)
if not (keywords or tag):
return render(
request,
Expand Down Expand Up @@ -158,7 +160,7 @@ def external_search(request):
if category == "all":
category = None
keywords = request.GET.get("q", default="").strip()
page_number = int(request.GET.get("page", default=1))
page_number = int_(request.GET.get("page"), 1)
items = ExternalSources.search(category, keywords, page_number) if keywords else []
cache_key = f"search_{category if category!='movietv' else 'movie,tv'}_{keywords}"
dedupe_urls = cache.get(cache_key, [])
Expand Down
2 changes: 1 addition & 1 deletion catalog/templates/_item_card_metadata_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h5>
{% for tag in item.tags %}
{% if forloop.counter <= 5 %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ tag }}">{{ tag }}</a>
<a href="{% url 'common:search' %}?tag={{ tag }}">{{ tag }}</a>
</span>
{% endif %}
{% endfor %}
Expand Down
16 changes: 16 additions & 0 deletions catalog/templates/_item_card_metadata_collection.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "_item_card_metadata_base.html" %}
{% load humanize %}
{% load i18n %}
{% block brief %}
<div class="multi-fields">
{% if item.rating %}
<span class="solo-hidden">{{ item.rating | floatformat:1 }} <small>({{ item.rating_count }} {% trans "ratings" %})</small></span>
{% endif %}
{% include '_people.html' with people=item.host role='host' max=5 %}
</div>
{% endblock brief %}
{% block full %}
<div>
{% if not hide_brief %}{{ item.display_description | linebreaksbr }}{% endif %}
</div>
{% endblock full %}
2 changes: 1 addition & 1 deletion catalog/templates/discover.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ <h5>{% trans "Collections" %}</h5>
<div class="tag-list">
{% for t in popular_tags %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ t|urlencode }}">{{ t }}</a>
<a href="{% url 'common:search' %}?tag={{ t|urlencode }}">{{ t }}</a>
</span>
{% empty %}
<div class="empty">{% trans "nothing so far." %}</div>
Expand Down
2 changes: 1 addition & 1 deletion catalog/templates/item_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ <h3>
<div class="tag-list solo-hidden">
{% for tag in item.tags %}
<span>
<a href="{% url 'catalog:search' %}?tag={{ tag }}">{{ tag }}</a>
<a href="{% url 'common:search' %}?tag={{ tag }}">{{ tag }}</a>
</span>
{% endfor %}
</div>
Expand Down
80 changes: 80 additions & 0 deletions catalog/templates/search_header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{% load static %}
{% load i18n %}
{% load l10n %}
{% load humanize %}
{% load mastodon %}
{% load duration %}
{% load thumb %}
<hgroup>
<h5>“{{ request.GET.q }}”</h5>
<div class="search-category-picker">
{% visible_categories as cats %}
{% if request.GET.c and request.GET.c != 'all' %}
<a href="?q={{ request.GET.q }}&amp;c=all">{% trans "all" %}</a>
{% else %}
{% trans "all" %}
{% endif %}
{% if 'book' in cats %}
|
{% if request.GET.c != 'book' %}
<a href="?q={{ request.GET.q }}&amp;c=book">{% trans "books" %}</a>
{% else %}
{% trans "books" %}
{% endif %}
{% endif %}
{% if 'movie' in cats or 'tv' in cats %}
|
{% if request.GET.c != 'movietv' %}
<a href="?q={{ request.GET.q }}&amp;c=movietv">{% trans "movie & tv" %}</a>
{% else %}
{% trans "movie & tv" %}
{% endif %}
{% endif %}
{% if 'podcast' in cats %}
|
{% if request.GET.c != 'podcast' %}
<a href="?q={{ request.GET.q }}&amp;c=podcast">{% trans "podcasts" %}</a>
{% else %}
{% trans "podcasts" %}
{% endif %}
{% endif %}
{% if 'music' in cats %}
|
{% if request.GET.c != 'music' %}
<a href="?q={{ request.GET.q }}&amp;c=music">{% trans "music" %}</a>
{% else %}
{% trans "music" %}
{% endif %}
{% endif %}
{% if 'game' in cats %}
|
{% if request.GET.c != 'game' %}
<a href="?q={{ request.GET.q }}&amp;c=game">{% trans "games" %}</a>
{% else %}
{% trans "games" %}
{% endif %}
{% endif %}
{% if 'performance' in cats %}
|
{% if request.GET.c != 'performance' %}
<a href="?q={{ request.GET.q }}&amp;c=performance">{% trans "performances" %}</a>
{% else %}
{% trans "performances" %}
{% endif %}
{% endif %}
{% if user.is_authenticated %}
|
{% if request.GET.c != 'journal' %}
<a href="?q={{ request.GET.q }}&amp;c=journal">{% trans "your journal" %}</a>
{% else %}
{% trans "your journal" %}
{% endif %}
|
{% if request.GET.c != 'timeline' %}
<a href="?q={{ request.GET.q }}&amp;c=timeline">{% trans "your timeline" %}</a>
{% else %}
{% trans "your timeline" %}
{% endif %}
{% endif %}
</div>
</hgroup>
60 changes: 1 addition & 59 deletions catalog/templates/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,7 @@
<div>
<div>
{% if request.GET.q %}
<hgroup>
<h5>“{{ request.GET.q }}”</h5>
<div>
{% visible_categories as cats %}
{% if request.GET.c and request.GET.c != 'all' %}
<a href="?q={{ request.GET.q }}&amp;c=all">{% trans "all" %}</a>
{% else %}
{% trans "all" %}
{% endif %}
{% if 'book' in cats %}
|
{% if request.GET.c != 'book' %}
<a href="?q={{ request.GET.q }}&amp;c=book">{% trans "books" %}</a>
{% else %}
{% trans "books" %}
{% endif %}
{% endif %}
{% if 'movie' in cats or 'tv' in cats %}
|
{% if request.GET.c != 'movietv' %}
<a href="?q={{ request.GET.q }}&amp;c=movietv">{% trans "movie & tv" %}</a>
{% else %}
{% trans "movie & tv" %}
{% endif %}
{% endif %}
{% if 'podcast' in cats %}
|
{% if request.GET.c != 'podcast' %}
<a href="?q={{ request.GET.q }}&amp;c=podcast">{% trans "podcasts" %}</a>
{% else %}
{% trans "podcasts" %}
{% endif %}
{% endif %}
{% if 'music' in cats %}
|
{% if request.GET.c != 'music' %}
<a href="?q={{ request.GET.q }}&amp;c=music">{% trans "music" %}</a>
{% else %}
{% trans "music" %}
{% endif %}
{% endif %}
{% if 'game' in cats %}
|
{% if request.GET.c != 'game' %}
<a href="?q={{ request.GET.q }}&amp;c=game">{% trans "games" %}</a>
{% else %}
{% trans "games" %}
{% endif %}
{% endif %}
{% if 'performance' in cats %}
|
{% if request.GET.c != 'performance' %}
<a href="?q={{ request.GET.q }}&amp;c=performance">{% trans "performances" %}</a>
{% else %}
{% trans "performances" %}
{% endif %}
{% endif %}
</div>
</hgroup>
{% include "search_header.html" %}
{% endif %}
{% if request.GET.tag %}
<h5>{% trans 'tag' %}: “{{ request.GET.tag }}”</h5>
Expand Down
11 changes: 11 additions & 0 deletions catalog/tv/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def child_items(self):
def get_season_count(self):
return self.season_count or self.seasons.all().count()

def to_indexable_titles(self) -> list[str]:
titles = [t["text"] for t in self.localized_title if t]
titles += [self.orig_title] if self.orig_title else []
return list(set(titles))


class TVSeason(Item):
if TYPE_CHECKING:
Expand Down Expand Up @@ -434,6 +439,12 @@ def additional_title(self) -> list[str]:
and RE_LOCALIZED_SEASON_NUMBERS.sub("", t["text"]) != ""
]

def to_indexable_titles(self) -> list[str]:
titles = [t["text"] for t in self.localized_title if t]
titles += [self.orig_title] if self.orig_title else []
titles += self.parent_item.to_indexable_titles() if self.parent_item else []
return list(set(titles))

def update_linked_items_from_external_resource(self, resource):
for w in resource.required_resources:
if w["model"] == "TVShow":
Expand Down
1 change: 0 additions & 1 deletion catalog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ def _get_all_url_paths():
mark_list,
name="mark_list",
),
path("search", search, name="search"),
path("search/", search, name="search_legacy"),
path("search/external", external_search, name="external_search"),
path("fetch_refresh/<str:job_id>", fetch_refresh, name="fetch_refresh"),
Expand Down
20 changes: 19 additions & 1 deletion common/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .cron import BaseJob, JobManager
from .index import Index, SearchResult
from .lang import (
LANGUAGE_CHOICES,
LOCALE_CHOICES,
Expand All @@ -9,4 +10,21 @@
detect_language,
get_current_locales,
)
from .misc import uniq
from .misc import int_, uniq

__all__ = [
"BaseJob",
"JobManager",
"LANGUAGE_CHOICES",
"LOCALE_CHOICES",
"SCRIPT_CHOICES",
"SITE_DEFAULT_LANGUAGE",
"SITE_PREFERRED_LANGUAGES",
"SITE_PREFERRED_LOCALES",
"detect_language",
"get_current_locales",
"uniq",
"int_",
"Index",
"SearchResult",
]
Loading

0 comments on commit 6d377c4

Please sign in to comment.