Private
Public Access
Implements the backend-agnostic search interface from the plan, selected at runtime by connection.vendor: - SQLite: an FTS5 virtual table (external-content, mirroring Article.title/content_html) created via a vendor-conditional migration, kept in sync by insert/update/delete triggers. Queries wrap the whole user input as a single FTS5 phrase so punctuation in a search box can't break MATCH syntax. `reindex_search` management command rebuilds it after bulk imports. - Postgres: computes the search vector on the fly per the plan's decision (title weighted above content, no stored/indexed column) rather than diverging the schema from SQLite. Both backends scope results to the requesting user's own subscriptions and share the stream/detail article-row template and read/star/save actions via a new apps.articles.queries.with_user_state() helper, extracted from apps.reader.views to avoid duplicating that annotation logic for search results. Verified live: the SQLite path end-to-end in-browser against the real Hacker News subscription (search UI, phrase matching, no-match case), and the Postgres path against a throwaway Postgres container (search, feed-scoping, and title-vs-content ranking), torn down afterward — not part of this project's own infrastructure. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
137 lines
4.3 KiB
Python
137 lines
4.3 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
from django.http import Http404
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.utils import timezone
|
|
from django.views.decorators.http import require_POST
|
|
|
|
from apps.articles.models import Article, Tag, UserArticleState
|
|
from apps.articles.queries import with_user_state
|
|
|
|
FILTER_CHOICES = [
|
|
("unread", "Unread"),
|
|
("all", "All"),
|
|
("starred", "Starred"),
|
|
("saved", "Saved"),
|
|
]
|
|
VALID_FILTERS = {key for key, _ in FILTER_CHOICES}
|
|
PAGE_SIZE = 30
|
|
|
|
|
|
def _base_queryset(user):
|
|
"""Articles from the user's subscriptions, with rule-hidden articles
|
|
excluded regardless of which filter tab is active."""
|
|
return (
|
|
Article.objects.filter(feed__subscriptions__user=user)
|
|
.exclude(user_states__user=user, user_states__is_hidden=True)
|
|
.select_related("feed")
|
|
)
|
|
|
|
|
|
def _get_or_create_state(user, article):
|
|
state, _ = UserArticleState.objects.get_or_create(user=user, article=article)
|
|
return state
|
|
|
|
|
|
@login_required
|
|
def stream(request):
|
|
active_filter = request.GET.get("filter", "unread")
|
|
if active_filter not in VALID_FILTERS:
|
|
active_filter = "unread"
|
|
|
|
queryset = _base_queryset(request.user)
|
|
if active_filter == "unread":
|
|
queryset = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
|
|
elif active_filter == "starred":
|
|
queryset = queryset.filter(user_states__user=request.user, user_states__is_starred=True)
|
|
elif active_filter == "saved":
|
|
queryset = queryset.filter(
|
|
user_states__user=request.user, user_states__is_saved_for_later=True
|
|
)
|
|
|
|
active_tag = None
|
|
tag_id = request.GET.get("tag")
|
|
if tag_id:
|
|
active_tag = Tag.objects.filter(user=request.user, pk=tag_id).first()
|
|
if active_tag:
|
|
queryset = queryset.filter(
|
|
article_tags__user=request.user, article_tags__tag=active_tag
|
|
)
|
|
|
|
try:
|
|
page = max(int(request.GET.get("page", 1)), 1)
|
|
except ValueError:
|
|
page = 1
|
|
start, end = (page - 1) * PAGE_SIZE, page * PAGE_SIZE
|
|
|
|
total_count = queryset.count()
|
|
articles = with_user_state(queryset[start:end], request.user)
|
|
|
|
context = {
|
|
"articles": articles,
|
|
"active_filter": active_filter,
|
|
"filters": FILTER_CHOICES,
|
|
"active_tag": active_tag,
|
|
"user_tags": Tag.objects.filter(user=request.user),
|
|
"page": page,
|
|
"has_next_page": end < total_count,
|
|
"has_previous_page": page > 1,
|
|
}
|
|
return render(request, "reader/stream.html", context)
|
|
|
|
|
|
@login_required
|
|
def article_detail(request, article_id):
|
|
article = get_object_or_404(_base_queryset(request.user), pk=article_id)
|
|
|
|
state = _get_or_create_state(request.user, article)
|
|
if not state.is_read:
|
|
state.is_read = True
|
|
state.read_at = timezone.now()
|
|
state.save(update_fields=["is_read", "read_at"])
|
|
|
|
article.is_read = state.is_read
|
|
article.is_starred = state.is_starred
|
|
article.is_saved_for_later = state.is_saved_for_later
|
|
|
|
return render(request, "reader/article_detail.html", {"article": article})
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def toggle_read(request, article_id):
|
|
return _toggle(request, article_id, "is_read", "read_at")
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def toggle_star(request, article_id):
|
|
return _toggle(request, article_id, "is_starred", "starred_at")
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def toggle_saved(request, article_id):
|
|
return _toggle(request, article_id, "is_saved_for_later", None)
|
|
|
|
|
|
def _toggle(request, article_id, flag_field, timestamp_field):
|
|
try:
|
|
article = _base_queryset(request.user).get(pk=article_id)
|
|
except Article.DoesNotExist:
|
|
raise Http404
|
|
|
|
state = _get_or_create_state(request.user, article)
|
|
new_value = not getattr(state, flag_field)
|
|
setattr(state, flag_field, new_value)
|
|
update_fields = [flag_field]
|
|
if timestamp_field:
|
|
setattr(state, timestamp_field, timezone.now() if new_value else None)
|
|
update_fields.append(timestamp_field)
|
|
state.save(update_fields=update_fields)
|
|
|
|
article.is_read = state.is_read
|
|
article.is_starred = state.is_starred
|
|
article.is_saved_for_later = state.is_saved_for_later
|
|
|
|
return render(request, "reader/_article_actions.html", {"article": article})
|