Update web/core/views.py
This commit is contained in:
+12
-4
@@ -9,6 +9,7 @@ from django.db.models import Q
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.utils.text import Truncator
|
||||
|
||||
from .forms import ImportForm, EntryForm
|
||||
from .models import Entry
|
||||
@@ -616,10 +617,9 @@ def normalize_subjects(request):
|
||||
def api_get_recent_views(request):
|
||||
"""
|
||||
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 # local import to avoid issues in old migrations
|
||||
from .models import RecentView
|
||||
|
||||
recents = (
|
||||
RecentView.objects
|
||||
@@ -628,12 +628,20 @@ def api_get_recent_views(request):
|
||||
.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 = []
|
||||
for rv in recents:
|
||||
e = rv.entry
|
||||
items.append({
|
||||
"entry_id": rv.entry_id,
|
||||
"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})
|
||||
Reference in New Issue
Block a user