From 59d54234839062d20d1edd58f0aa767f89555078 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Thu, 1 Apr 2021 02:22:15 -0400 Subject: [PATCH] fix snapshot icon caching and ordering --- archivebox/core/models.py | 4 ++++ archivebox/core/views.py | 6 +----- archivebox/index/html.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/archivebox/core/models.py b/archivebox/core/models.py index e7741920..fcfcf97d 100644 --- a/archivebox/core/models.py +++ b/archivebox/core/models.py @@ -12,6 +12,7 @@ from ..config import ARCHIVE_DIR, ARCHIVE_DIR_NAME from ..system import get_dir_size from ..util import parse_date, base_url, hashurl from ..index.schema import Link +from ..index.html import snapshot_icons from ..extractors import get_default_archive_methods, ARCHIVE_METHODS_INDEXING_PRECEDENCE EXTRACTORS = [(extractor[0], extractor[0]) for extractor in get_default_archive_methods()] @@ -125,6 +126,9 @@ class Snapshot(models.Model): return tags_str return cache.get_or_set(cache_key, calc_tags_str) + def icons(self) -> str: + return snapshot_icons(self) + @cached_property def bookmarked(self): return parse_date(self.timestamp) diff --git a/archivebox/core/views.py b/archivebox/core/views.py index 36794a8d..eb9d76b2 100644 --- a/archivebox/core/views.py +++ b/archivebox/core/views.py @@ -209,7 +209,7 @@ class PublicIndexView(ListView): template_name = 'public_index.html' model = Snapshot paginate_by = SNAPSHOTS_PER_PAGE - ordering = ['title'] + ordering = ['-added'] def get_context_data(self, **kwargs): return { @@ -223,10 +223,6 @@ class PublicIndexView(ListView): query = self.request.GET.get('q') if query: qs = qs.filter(Q(title__icontains=query) | Q(url__icontains=query) | Q(timestamp__icontains=query) | Q(tags__name__icontains=query)) - - for snapshot in qs: - # lazy load snapshot icons, otherwise it will load icons for entire index at once - snapshot.icons = lambda: snapshot_icons(snapshot) return qs def get(self, *args, **kwargs): diff --git a/archivebox/index/html.py b/archivebox/index/html.py index c4f66f55..b584b876 100644 --- a/archivebox/index/html.py +++ b/archivebox/index/html.py @@ -116,7 +116,7 @@ def render_django_template(template: str, context: Mapping[str, str]) -> str: def snapshot_icons(snapshot) -> str: - cache_key = f'{str(snapshot.id)[:12]}-{(snapshot.updated or snapshot.added).timestamp()}-snapshot-icons' + cache_key = f'{snapshot.id}-{(snapshot.updated or snapshot.added).timestamp()}-snapshot-icons' def calc_snapshot_icons(): from core.models import EXTRACTORS