Skip to content

Commit

Permalink
Show web archive fallback link in details modal
Browse files Browse the repository at this point in the history
  • Loading branch information
sissbruecker committed Aug 29, 2024
1 parent 0fe6304 commit 1122d18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions bookmarks/templates/bookmarks/details/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<span>Reader mode</span>
</a>
{% endif %}
{% if details.bookmark.web_archive_snapshot_url %}
<a class="weblink" href="{{ details.bookmark.web_archive_snapshot_url }}"
{% if details.web_archive_snapshot_url %}
<a class="weblink" href="{{ details.web_archive_snapshot_url }}"
target="{{ details.profile.bookmark_link_target }}">
{% if details.show_link_icons %}
<svg class="favicon" viewBox="0 0 76 86" xmlns="http://www.w3.org/2000/svg">
Expand Down
35 changes: 22 additions & 13 deletions bookmarks/tests/test_bookmark_details_modal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import datetime
import re
from unittest.mock import patch

from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import formats
from django.utils import formats, timezone

from bookmarks.models import BookmarkAsset, UserProfile
from bookmarks.services import bookmarks, tasks
Expand Down Expand Up @@ -180,7 +181,7 @@ def test_reader_mode_link(self):
# no latest snapshot
bookmark = self.setup_bookmark()
soup = self.get_details(bookmark)
self.assertEqual(self.count_weblinks(soup), 1)
self.assertEqual(self.count_weblinks(soup), 2)

# snapshot is not complete
self.setup_asset(
Expand All @@ -194,7 +195,7 @@ def test_reader_mode_link(self):
status=BookmarkAsset.STATUS_FAILURE,
)
soup = self.get_details(bookmark)
self.assertEqual(self.count_weblinks(soup), 1)
self.assertEqual(self.count_weblinks(soup), 2)

# not a snapshot
self.setup_asset(
Expand All @@ -203,7 +204,7 @@ def test_reader_mode_link(self):
status=BookmarkAsset.STATUS_COMPLETE,
)
soup = self.get_details(bookmark)
self.assertEqual(self.count_weblinks(soup), 1)
self.assertEqual(self.count_weblinks(soup), 2)

# snapshot is complete
asset = self.setup_asset(
Expand All @@ -212,20 +213,13 @@ def test_reader_mode_link(self):
status=BookmarkAsset.STATUS_COMPLETE,
)
soup = self.get_details(bookmark)
self.assertEqual(self.count_weblinks(soup), 2)
self.assertEqual(self.count_weblinks(soup), 3)

reader_mode_url = reverse("bookmarks:assets.read", args=[asset.id])
link = self.find_weblink(soup, reader_mode_url)
self.assertIsNotNone(link)

def test_internet_archive_link(self):
# without snapshot url
bookmark = self.setup_bookmark()
soup = self.get_details(bookmark)
link = self.find_weblink(soup, bookmark.web_archive_snapshot_url)
self.assertIsNone(link)

# with snapshot url
def test_internet_archive_link_with_snapshot_url(self):
bookmark = self.setup_bookmark(web_archive_snapshot_url="https://example.com/")
soup = self.get_details(bookmark)
link = self.find_weblink(soup, bookmark.web_archive_snapshot_url)
Expand Down Expand Up @@ -264,6 +258,21 @@ def test_internet_archive_link(self):
image = link.select_one("svg")
self.assertIsNotNone(image)

def test_internet_archive_link_with_fallback_url(self):
date_added = timezone.datetime(
2023, 8, 11, 21, 45, 11, tzinfo=datetime.timezone.utc
)
bookmark = self.setup_bookmark(url="https://example.com/", added=date_added)
fallback_web_archive_url = (
"https://web.archive.org/web/20230811214511/https://example.com/"
)

soup = self.get_details(bookmark)
link = self.find_weblink(soup, fallback_web_archive_url)
self.assertIsNotNone(link)
self.assertEqual(link["href"], fallback_web_archive_url)
self.assertEqual(link.text.strip(), "Internet Archive")

def test_weblinks_respect_target_setting(self):
bookmark = self.setup_bookmark(web_archive_snapshot_url="https://example.com/")

Expand Down
6 changes: 6 additions & 0 deletions bookmarks/views/partials/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ def __init__(self, request: WSGIRequest, bookmark: Bookmark):
# For now hide files section if snapshots are not supported
self.show_files = settings.LD_ENABLE_SNAPSHOTS

self.web_archive_snapshot_url = bookmark.web_archive_snapshot_url
if not self.web_archive_snapshot_url:
self.web_archive_snapshot_url = generate_fallback_webarchive_url(
bookmark.url, bookmark.date_added
)

self.assets = [
BookmarkAssetItem(asset) for asset in bookmark.bookmarkasset_set.all()
]
Expand Down

0 comments on commit 1122d18

Please sign in to comment.