prefer html snapshots when internet archive integration is disabled

This commit is contained in:
Cy Pokhrel 2024-11-11 16:32:32 -05:00
parent c3149409b0
commit d932b6257b
No known key found for this signature in database
GPG key ID: 1200FBE36C2ADE2E
5 changed files with 47 additions and 1 deletions

View file

@ -0,0 +1,30 @@
# 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")
for bookmark in Bookmark.objects.all():
snapshots = bookmark.bookmarkasset_set.filter(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),
]

View file

@ -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)

View file

@ -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}"
)

View file

@ -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 }}"

View file

@ -134,6 +134,7 @@ class BookmarkItem:
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
@ -164,6 +165,11 @@ class BookmarkItem:
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