Update web/core/views.py

This commit is contained in:
2025-08-16 22:36:53 +00:00
parent 6c35a63f33
commit 556936021f
+12 -4
View File
@@ -9,6 +9,7 @@ from django.db.models import Q
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from django.utils.text import Truncator
from .forms import ImportForm, EntryForm from .forms import ImportForm, EntryForm
from .models import Entry from .models import Entry
@@ -616,10 +617,9 @@ def normalize_subjects(request):
def api_get_recent_views(request): def api_get_recent_views(request):
""" """
Return the current user's recently viewed entries (up to 50), Return the current user's recently viewed entries (up to 50),
including the illustration text so the UI can show a 20-word snippet. including a precomputed 20-word snippet from illustration (or a sensible fallback).
""" """
# Model: RecentView with fields: user (FK), entry (FK Entry), viewed_at (DateTime) from .models import RecentView
from .models import RecentView # local import to avoid issues in old migrations
recents = ( recents = (
RecentView.objects RecentView.objects
@@ -628,12 +628,20 @@ def api_get_recent_views(request):
.order_by("-viewed_at")[:50] .order_by("-viewed_at")[:50]
) )
def make_snippet(e):
base = (e.illustration or "").strip() or (e.application or "").strip() or (e.subject or "").strip()
if not base:
return ""
return Truncator(" ".join(base.split())).words(20, truncate="")
items = [] items = []
for rv in recents: for rv in recents:
e = rv.entry
items.append({ items.append({
"entry_id": rv.entry_id, "entry_id": rv.entry_id,
"viewed_at": rv.viewed_at.isoformat(), "viewed_at": rv.viewed_at.isoformat(),
"illustration": rv.entry.illustration or "", "illustration": e.illustration or "",
"snippet": make_snippet(e),
}) })
return JsonResponse({"ok": True, "items": items}) return JsonResponse({"ok": True, "items": items})