Skip to content

Commit

Permalink
prefer html snapshots when internet archive integration is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
cything committed Nov 12, 2024
1 parent c314940 commit 153ecd9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
31 changes: 31 additions & 0 deletions bookmarks/migrations/0043_bookmark_latest_snapshot_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.1.1 on 2024-11-12 00:16

from django.db import migrations, models


def update_latest_snapshots(apps, schema_editor):
Bookmark = apps.get_model("bookmarks", "bookmark")
BookmarkAsset = apps.get_model("bookmarks", "bookmarkasset")
for bookmark in Bookmark.objects.all():
snapshots = BookmarkAsset.objects.filter(
bookmark=bookmark.id, status="complete"
).order_by("-date_created")
if snapshots:
bookmark.latest_snapshot_id = snapshots[0].id
bookmark.save()


class Migration(migrations.Migration):

dependencies = [
("bookmarks", "0042_userprofile_custom_css_hash"),
]

operations = [
migrations.AddField(
model_name="bookmark",
name="latest_snapshot_id",
field=models.IntegerField(blank=True, null=True),
),
migrations.RunPython(update_latest_snapshots),
]
1 change: 1 addition & 0 deletions bookmarks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Bookmark(models.Model):
# Obsolete field, kept to not remove column when generating migrations
website_description = models.TextField(blank=True, null=True)
web_archive_snapshot_url = models.CharField(max_length=2048, blank=True)
latest_snapshot_id = models.IntegerField(blank=True, null=True)
favicon_file = models.CharField(max_length=512, blank=True)
preview_image_file = models.CharField(max_length=512, blank=True)
unread = models.BooleanField(default=False)
Expand Down
2 changes: 2 additions & 0 deletions bookmarks/services/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ def _create_html_snapshot_task(asset_id: int):
asset.file = filename
asset.gzip = True
asset.save()
asset.bookmark.latest_snapshot_id = asset.id
asset.bookmark.save()
logger.info(
f"Successfully created HTML snapshot for bookmark. url={asset.bookmark.url}"
)
Expand Down
9 changes: 8 additions & 1 deletion bookmarks/templates/bookmarks/bookmark_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@
{% endif %}
<div class="actions">
{% if bookmark_item.display_date %}
{% if bookmark_item.web_archive_snapshot_url %}
{% if bookmark_item.show_html_snapshot %}
<a href="{% url 'bookmarks:assets.view' bookmark_item.latest_snapshot_id %}"
title="Show HTML snapshot"
target="{{ bookmark_list.link_target }}"
rel="noopener">
{{ bookmark_item.display_date }}
</a>
{% elif bookmark_item.web_archive_snapshot_url %}
<a href="{{ bookmark_item.web_archive_snapshot_url }}"
title="Show snapshot on the Internet Archive Wayback Machine"
target="{{ bookmark_list.link_target }}"
Expand Down
6 changes: 6 additions & 0 deletions bookmarks/views/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def __init__(
self.web_archive_snapshot_url = generate_fallback_webarchive_url(
bookmark.url, bookmark.date_added
)
self.latest_snapshot_id = bookmark.latest_snapshot_id
self.favicon_file = bookmark.favicon_file
self.preview_image_file = bookmark.preview_image_file
self.is_archived = bookmark.is_archived
Expand Down Expand Up @@ -164,6 +165,11 @@ def __init__(
self.show_notes_button or self.show_mark_as_read or self.show_unshare
)

self.show_html_snapshot = self.latest_snapshot_id and (
profile.web_archive_integration
== UserProfile.WEB_ARCHIVE_INTEGRATION_DISABLED
)


class BookmarkListContext:
request_context = RequestContext
Expand Down

0 comments on commit 153ecd9

Please sign in to comment.