Make Internet Archive integration opt-in (#250)

* Make web archive integration opt-in

* Add toast message about web archive integration opt-in

* Improve wording for web archive setting

* Add toast admin

* Fix toast clear button visited styles

* Add test for redirect

* Improve wording

* Ensure redirects to same domain

* Improve wording

* Fix snapshot test

Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@gmail.com>
This commit is contained in:
Sascha Ißbrücker 2022-05-14 09:46:51 +02:00 committed by GitHub
parent 56173aea3f
commit f92c3dd403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 376 additions and 67 deletions

View file

@ -7,7 +7,7 @@ from django.utils.translation import ngettext, gettext
from rest_framework.authtoken.admin import TokenAdmin from rest_framework.authtoken.admin import TokenAdmin
from rest_framework.authtoken.models import TokenProxy from rest_framework.authtoken.models import TokenProxy
from bookmarks.models import Bookmark, Tag, UserProfile from bookmarks.models import Bookmark, Tag, UserProfile, Toast
from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark
@ -95,8 +95,15 @@ class AdminCustomUser(UserAdmin):
return super(AdminCustomUser, self).get_inline_instances(request, obj) return super(AdminCustomUser, self).get_inline_instances(request, obj)
class AdminToast(admin.ModelAdmin):
list_display = ('key', 'message', 'owner', 'acknowledged')
search_fields = ('key', 'message')
list_filter = ('owner__username',)
linkding_admin_site = LinkdingAdminSite() linkding_admin_site = LinkdingAdminSite()
linkding_admin_site.register(Bookmark, AdminBookmark) linkding_admin_site.register(Bookmark, AdminBookmark)
linkding_admin_site.register(Tag, AdminTag) linkding_admin_site.register(Tag, AdminTag)
linkding_admin_site.register(User, AdminCustomUser) linkding_admin_site.register(User, AdminCustomUser)
linkding_admin_site.register(TokenProxy, TokenAdmin) linkding_admin_site.register(TokenProxy, TokenAdmin)
linkding_admin_site.register(Toast, AdminToast)

View file

@ -0,0 +1,12 @@
from bookmarks.models import Toast
def toasts(request):
user = request.user if hasattr(request, 'user') else None
toast_messages = Toast.objects.filter(owner=user, acknowledged=False) if user and user.is_authenticated else []
has_toasts = len(toast_messages) > 0
return {
'has_toasts': has_toasts,
'toast_messages': toast_messages,
}

View file

@ -0,0 +1,18 @@
# Generated by Django 3.2.6 on 2022-01-08 12:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bookmarks', '0010_userprofile_bookmark_link_target'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='web_archive_integration',
field=models.CharField(choices=[('disabled', 'Disabled'), ('enabled', 'Enabled')], default='disabled', max_length=10),
),
]

View file

@ -0,0 +1,26 @@
# Generated by Django 3.2.6 on 2022-01-08 19:24
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('bookmarks', '0011_userprofile_web_archive_integration'),
]
operations = [
migrations.CreateModel(
name='Toast',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(max_length=50)),
('message', models.TextField()),
('acknowledged', models.BooleanField(default=False)),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

View file

@ -0,0 +1,30 @@
# Generated by Django 3.2.6 on 2022-01-08 19:27
from django.db import migrations
from django.contrib.auth import get_user_model
from bookmarks.models import Toast
User = get_user_model()
def forwards(apps, schema_editor):
for user in User.objects.all():
toast = Toast(key='web_archive_opt_in_hint',
message='The Internet Archive Wayback Machine integration has been disabled by default. Check the Settings to re-enable it.',
owner=user)
toast.save()
def reverse(apps, schema_editor):
Toast.objects.filter(key='web_archive_opt_in_hint').delete()
class Migration(migrations.Migration):
dependencies = [
('bookmarks', '0012_toast'),
]
operations = [
migrations.RunPython(forwards, reverse),
]

View file

@ -128,18 +128,26 @@ class UserProfile(models.Model):
(BOOKMARK_LINK_TARGET_BLANK, 'New page'), (BOOKMARK_LINK_TARGET_BLANK, 'New page'),
(BOOKMARK_LINK_TARGET_SELF, 'Same page'), (BOOKMARK_LINK_TARGET_SELF, 'Same page'),
] ]
WEB_ARCHIVE_INTEGRATION_DISABLED = 'disabled'
WEB_ARCHIVE_INTEGRATION_ENABLED = 'enabled'
WEB_ARCHIVE_INTEGRATION_CHOICES = [
(WEB_ARCHIVE_INTEGRATION_DISABLED, 'Disabled'),
(WEB_ARCHIVE_INTEGRATION_ENABLED, 'Enabled'),
]
user = models.OneToOneField(get_user_model(), related_name='profile', on_delete=models.CASCADE) user = models.OneToOneField(get_user_model(), related_name='profile', on_delete=models.CASCADE)
theme = models.CharField(max_length=10, choices=THEME_CHOICES, blank=False, default=THEME_AUTO) theme = models.CharField(max_length=10, choices=THEME_CHOICES, blank=False, default=THEME_AUTO)
bookmark_date_display = models.CharField(max_length=10, choices=BOOKMARK_DATE_DISPLAY_CHOICES, blank=False, bookmark_date_display = models.CharField(max_length=10, choices=BOOKMARK_DATE_DISPLAY_CHOICES, blank=False,
default=BOOKMARK_DATE_DISPLAY_RELATIVE) default=BOOKMARK_DATE_DISPLAY_RELATIVE)
bookmark_link_target = models.CharField(max_length=10, choices=BOOKMARK_LINK_TARGET_CHOICES, blank=False, bookmark_link_target = models.CharField(max_length=10, choices=BOOKMARK_LINK_TARGET_CHOICES, blank=False,
default=BOOKMARK_LINK_TARGET_BLANK) default=BOOKMARK_LINK_TARGET_BLANK)
web_archive_integration = models.CharField(max_length=10, choices=WEB_ARCHIVE_INTEGRATION_CHOICES, blank=False,
default=WEB_ARCHIVE_INTEGRATION_DISABLED)
class UserProfileForm(forms.ModelForm): class UserProfileForm(forms.ModelForm):
class Meta: class Meta:
model = UserProfile model = UserProfile
fields = ['theme', 'bookmark_date_display', 'bookmark_link_target'] fields = ['theme', 'bookmark_date_display', 'bookmark_link_target', 'web_archive_integration']
@receiver(post_save, sender=get_user_model()) @receiver(post_save, sender=get_user_model())
@ -151,3 +159,10 @@ def create_user_profile(sender, instance, created, **kwargs):
@receiver(post_save, sender=get_user_model()) @receiver(post_save, sender=get_user_model())
def save_user_profile(sender, instance, **kwargs): def save_user_profile(sender, instance, **kwargs):
instance.profile.save() instance.profile.save()
class Toast(models.Model):
key = models.CharField(max_length=50)
message = models.TextField()
acknowledged = models.BooleanField(default=False)
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

View file

@ -29,7 +29,7 @@ def create_bookmark(bookmark: Bookmark, tag_string: str, current_user: User):
_update_bookmark_tags(bookmark, tag_string, current_user) _update_bookmark_tags(bookmark, tag_string, current_user)
bookmark.save() bookmark.save()
# Create snapshot on web archive # Create snapshot on web archive
tasks.create_web_archive_snapshot(bookmark.id, False) tasks.create_web_archive_snapshot(current_user, bookmark, False)
return bookmark return bookmark
@ -47,7 +47,7 @@ def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
bookmark.save() bookmark.save()
# Update web archive snapshot, if URL changed # Update web archive snapshot, if URL changed
if has_url_changed: if has_url_changed:
tasks.create_web_archive_snapshot(bookmark.id, True) tasks.create_web_archive_snapshot(current_user, bookmark, True)
return bookmark return bookmark

View file

@ -40,7 +40,7 @@ def import_netscape_html(html: str, user: User):
result.failed = result.failed + 1 result.failed = result.failed + 1
# Create snapshots for newly imported bookmarks # Create snapshots for newly imported bookmarks
tasks.schedule_bookmarks_without_snapshots(user.id) tasks.schedule_bookmarks_without_snapshots(user)
return result return result

View file

@ -4,30 +4,29 @@ import waybackpy
from background_task import background from background_task import background
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from waybackpy.exceptions import WaybackError from waybackpy.exceptions import WaybackError
from bookmarks.models import Bookmark from bookmarks.models import Bookmark, UserProfile
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def when_background_tasks_enabled(fn): def is_web_archive_integration_active(user: User) -> bool:
def wrapper(*args, **kwargs): background_tasks_enabled = not settings.LD_DISABLE_BACKGROUND_TASKS
if settings.LD_DISABLE_BACKGROUND_TASKS: web_archive_integration_enabled = \
return user.profile.web_archive_integration == UserProfile.WEB_ARCHIVE_INTEGRATION_ENABLED
return fn(*args, **kwargs)
# Expose attributes from wrapped TaskProxy function return background_tasks_enabled and web_archive_integration_enabled
attrs = vars(fn)
for key, value in attrs.items():
setattr(wrapper, key, value) def create_web_archive_snapshot(user: User, bookmark: Bookmark, force_update: bool):
if is_web_archive_integration_active(user):
return wrapper _create_web_archive_snapshot_task(bookmark.id, force_update)
@when_background_tasks_enabled
@background() @background()
def create_web_archive_snapshot(bookmark_id: int, force_update: bool): def _create_web_archive_snapshot_task(bookmark_id: int, force_update: bool):
try: try:
bookmark = Bookmark.objects.get(id=bookmark_id) bookmark = Bookmark.objects.get(id=bookmark_id)
except Bookmark.DoesNotExist: except Bookmark.DoesNotExist:
@ -52,11 +51,15 @@ def create_web_archive_snapshot(bookmark_id: int, force_update: bool):
logger.debug(f'Successfully created web archive link for bookmark: {bookmark}...') logger.debug(f'Successfully created web archive link for bookmark: {bookmark}...')
@when_background_tasks_enabled def schedule_bookmarks_without_snapshots(user: User):
if is_web_archive_integration_active(user):
_schedule_bookmarks_without_snapshots_task(user.id)
@background() @background()
def schedule_bookmarks_without_snapshots(user_id: int): def _schedule_bookmarks_without_snapshots_task(user_id: int):
user = get_user_model().objects.get(id=user_id) user = get_user_model().objects.get(id=user_id)
bookmarks_without_snapshots = Bookmark.objects.filter(web_archive_snapshot_url__exact='', owner=user) bookmarks_without_snapshots = Bookmark.objects.filter(web_archive_snapshot_url__exact='', owner=user)
for bookmark in bookmarks_without_snapshots: for bookmark in bookmarks_without_snapshots:
create_web_archive_snapshot(bookmark.id, False) _create_web_archive_snapshot_task(bookmark.id, False)

View file

@ -5,4 +5,4 @@ from bookmarks.services import tasks
@receiver(user_logged_in) @receiver(user_logged_in)
def user_logged_in(sender, request, user, **kwargs): def user_logged_in(sender, request, user, **kwargs):
tasks.schedule_bookmarks_without_snapshots(user.id) tasks.schedule_bookmarks_without_snapshots(user)

View file

@ -11,6 +11,18 @@ header {
margin-bottom: 40px; margin-bottom: 40px;
} }
header .toasts {
margin-bottom: 20px;
.toast {
margin-bottom: 0.4rem;
}
.toast a.btn-clear:visited {
color: currentColor;
}
}
.navbar { .navbar {
.navbar-brand { .navbar-brand {

View file

@ -30,7 +30,7 @@
<span class="date-label text-gray text-sm"> <span class="date-label text-gray text-sm">
{% if bookmark.web_archive_snapshot_url %} {% if bookmark.web_archive_snapshot_url %}
<a href="{{ bookmark.web_archive_snapshot_url }}" <a href="{{ bookmark.web_archive_snapshot_url }}"
title="Show snapshot on web archive" target="{{ link_target }}" rel="noopener"> title="Show snapshot on the Internet Archive Wayback Machine" target="{{ link_target }}" rel="noopener">
{% endif %} {% endif %}
<span>{{ bookmark.date_added|humanize_relative_date }}</span> <span>{{ bookmark.date_added|humanize_relative_date }}</span>
{% if bookmark.web_archive_snapshot_url %} {% if bookmark.web_archive_snapshot_url %}
@ -44,7 +44,7 @@
<span class="date-label text-gray text-sm"> <span class="date-label text-gray text-sm">
{% if bookmark.web_archive_snapshot_url %} {% if bookmark.web_archive_snapshot_url %}
<a href="{{ bookmark.web_archive_snapshot_url }}" <a href="{{ bookmark.web_archive_snapshot_url }}"
title="Show snapshot on web archive" target="{{ link_target }}" rel="noopener"> title="Show snapshot on the Internet Archive Wayback Machine" target="{{ link_target }}" rel="noopener">
{% endif %} {% endif %}
<span>{{ bookmark.date_added|humanize_absolute_date }}</span> <span>{{ bookmark.date_added|humanize_absolute_date }}</span>
{% if bookmark.web_archive_snapshot_url %} {% if bookmark.web_archive_snapshot_url %}

View file

@ -27,19 +27,31 @@
{% endif %} {% endif %}
</head> </head>
<body> <body>
<header class="navbar container grid-lg"> <header>
<section class="navbar-section"> {% if has_toasts %}
<a href="/" class="navbar-brand text-bold"> <div class="toasts container grid-lg">
<img class="logo" src="{% static 'logo.png' %}" alt="Application logo"> {% for toast in toast_messages %}
<h1>linkding</h1> <div class="toast">
</a> {{ toast.message }}
</section> <a href="{% url 'bookmarks:toasts.acknowledge' toast.id %}?return_url={{ request.path | urlencode }}" class="btn btn-clear float-right"></a>
{# Only show nav items menu when logged in #} </div>
{% if request.user.is_authenticated %} {% endfor %}
<section class="navbar-section"> </div>
{% include 'bookmarks/nav_menu.html' %}
</section>
{% endif %} {% endif %}
<div class="navbar container grid-lg">
<section class="navbar-section">
<a href="/" class="navbar-brand text-bold">
<img class="logo" src="{% static 'logo.png' %}" alt="Application logo">
<h1>linkding</h1>
</a>
</section>
{# Only show nav items menu when logged in #}
{% if request.user.is_authenticated %}
<section class="navbar-section">
{% include 'bookmarks/nav_menu.html' %}
</section>
{% endif %}
</div>
</header> </header>
<div class="content container grid-lg"> <div class="content container grid-lg">
{% block content %} {% block content %}

View file

@ -17,14 +17,35 @@
<div class="form-group"> <div class="form-group">
<label for="{{ form.theme.id_for_label }}" class="form-label">Theme</label> <label for="{{ form.theme.id_for_label }}" class="form-label">Theme</label>
{{ form.theme|add_class:"form-select col-2 col-sm-12" }} {{ form.theme|add_class:"form-select col-2 col-sm-12" }}
<div class="form-input-hint">
Whether to use a light or dark theme, or automatically adjust the theme based on your system's settings.
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="{{ form.bookmark_date_display.id_for_label }}" class="form-label">Bookmark date format</label> <label for="{{ form.bookmark_date_display.id_for_label }}" class="form-label">Bookmark date format</label>
{{ form.bookmark_date_display|add_class:"form-select col-2 col-sm-12" }} {{ form.bookmark_date_display|add_class:"form-select col-2 col-sm-12" }}
<div class="form-input-hint">
Whether to show bookmark dates as relative (how long ago), or as absolute dates. Alternatively the date can be hidden.
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="{{ form.bookmark_link_target.id_for_label }}" class="form-label">Open bookmarks in</label> <label for="{{ form.bookmark_link_target.id_for_label }}" class="form-label">Open bookmarks in</label>
{{ form.bookmark_link_target|add_class:"form-select col-2 col-sm-12" }} {{ form.bookmark_link_target|add_class:"form-select col-2 col-sm-12" }}
<div class="form-input-hint">
Whether to open bookmarks a new page or in the same page.
</div>
</div>
<div class="form-group">
<label for="{{ form.web_archive_integration.id_for_label }}" class="form-label">Internet Archive
integration</label>
{{ form.web_archive_integration|add_class:"form-select col-2 col-sm-12" }}
<div class="form-input-hint">
Enabling this feature will automatically create snapshots of bookmarked websites on the <a
href="https://web.archive.org/" target="_blank" rel="noopener">Internet Archive Wayback
Machine</a>. This allows
to preserve, and later access, the website as it was at the point in time it was bookmarked, in
case it goes offline or its content is modified.
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="submit" value="Save" class="btn btn-primary mt-2"> <input type="submit" value="Save" class="btn btn-primary mt-2">

View file

@ -28,7 +28,7 @@ class BookmarkListTagTest(TestCase, BookmarkFactoryMixin):
self.assertInHTML(f''' self.assertInHTML(f'''
<span class="date-label text-gray text-sm"> <span class="date-label text-gray text-sm">
<a href="{url}" <a href="{url}"
title="Show snapshot on web archive" target="{link_target}" rel="noopener"> title="Show snapshot on the Internet Archive Wayback Machine" target="{link_target}" rel="noopener">
<span>{label_content}</span> <span>{label_content}</span>
<span></span> <span></span>
</a> </a>

View file

@ -23,7 +23,7 @@ class BookmarkServiceTestCase(TestCase, BookmarkFactoryMixin):
bookmark_data = Bookmark(url='https://example.com') bookmark_data = Bookmark(url='https://example.com')
bookmark = create_bookmark(bookmark_data, 'tag1,tag2', self.user) bookmark = create_bookmark(bookmark_data, 'tag1,tag2', self.user)
mock_create_web_archive_snapshot.assert_called_once_with(bookmark.id, False) mock_create_web_archive_snapshot.assert_called_once_with(self.user, bookmark, False)
def test_update_should_create_web_archive_snapshot_if_url_did_change(self): def test_update_should_create_web_archive_snapshot_if_url_did_change(self):
with patch.object(tasks, 'create_web_archive_snapshot') as mock_create_web_archive_snapshot: with patch.object(tasks, 'create_web_archive_snapshot') as mock_create_web_archive_snapshot:
@ -31,7 +31,7 @@ class BookmarkServiceTestCase(TestCase, BookmarkFactoryMixin):
bookmark.url = 'https://example.com/updated' bookmark.url = 'https://example.com/updated'
update_bookmark(bookmark, 'tag1,tag2', self.user) update_bookmark(bookmark, 'tag1,tag2', self.user)
mock_create_web_archive_snapshot.assert_called_once_with(bookmark.id, True) mock_create_web_archive_snapshot.assert_called_once_with(self.user, bookmark, True)
def test_update_should_not_create_web_archive_snapshot_if_url_did_not_change(self): def test_update_should_not_create_web_archive_snapshot_if_url_did_not_change(self):
with patch.object(tasks, 'create_web_archive_snapshot') as mock_create_web_archive_snapshot: with patch.object(tasks, 'create_web_archive_snapshot') as mock_create_web_archive_snapshot:

View file

@ -5,8 +5,8 @@ from background_task.models import Task
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from bookmarks.models import Bookmark from bookmarks.models import Bookmark, UserProfile
from bookmarks.services.tasks import create_web_archive_snapshot, schedule_bookmarks_without_snapshots from bookmarks.services import tasks
from bookmarks.tests.helpers import BookmarkFactoryMixin, disable_logging from bookmarks.tests.helpers import BookmarkFactoryMixin, disable_logging
@ -26,6 +26,11 @@ class MockWaybackUrlWithSaveError:
class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin): class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self):
user = self.get_or_create_test_user()
user.profile.web_archive_integration = UserProfile.WEB_ARCHIVE_INTEGRATION_ENABLED
user.profile.save()
@disable_logging @disable_logging
def run_pending_task(self, task_function): def run_pending_task(self, task_function):
func = getattr(task_function, 'task_function', None) func = getattr(task_function, 'task_function', None)
@ -48,16 +53,16 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
bookmark = self.setup_bookmark() bookmark = self.setup_bookmark()
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')):
create_web_archive_snapshot(bookmark.id, False) tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, False)
self.run_pending_task(create_web_archive_snapshot) self.run_pending_task(tasks._create_web_archive_snapshot_task)
bookmark.refresh_from_db() bookmark.refresh_from_db()
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com')
def test_create_web_archive_snapshot_should_handle_missing_bookmark_id(self): def test_create_web_archive_snapshot_should_handle_missing_bookmark_id(self):
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')) as mock_wayback_url: with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')) as mock_wayback_url:
create_web_archive_snapshot(123, False) tasks._create_web_archive_snapshot_task(123, False)
self.run_pending_task(create_web_archive_snapshot) self.run_pending_task(tasks._create_web_archive_snapshot_task)
mock_wayback_url.assert_not_called() mock_wayback_url.assert_not_called()
@ -67,15 +72,15 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
with patch.object(waybackpy, 'Url', with patch.object(waybackpy, 'Url',
return_value=MockWaybackUrlWithSaveError()): return_value=MockWaybackUrlWithSaveError()):
with self.assertRaises(NotImplementedError): with self.assertRaises(NotImplementedError):
create_web_archive_snapshot(bookmark.id, False) tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, False)
self.run_pending_task(create_web_archive_snapshot) self.run_pending_task(tasks._create_web_archive_snapshot_task)
def test_create_web_archive_snapshot_should_skip_if_snapshot_exists(self): def test_create_web_archive_snapshot_should_skip_if_snapshot_exists(self):
bookmark = self.setup_bookmark(web_archive_snapshot_url='https://example.com') bookmark = self.setup_bookmark(web_archive_snapshot_url='https://example.com')
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')):
create_web_archive_snapshot(bookmark.id, False) tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, False)
self.run_pending_task(create_web_archive_snapshot) self.run_pending_task(tasks._create_web_archive_snapshot_task)
bookmark.refresh_from_db() bookmark.refresh_from_db()
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com')
@ -84,8 +89,8 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
bookmark = self.setup_bookmark(web_archive_snapshot_url='https://example.com') bookmark = self.setup_bookmark(web_archive_snapshot_url='https://example.com')
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')):
create_web_archive_snapshot(bookmark.id, True) tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, True)
self.run_pending_task(create_web_archive_snapshot) self.run_pending_task(tasks._create_web_archive_snapshot_task)
bookmark.refresh_from_db() bookmark.refresh_from_db()
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://other.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://other.com')
@ -93,7 +98,16 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
@override_settings(LD_DISABLE_BACKGROUND_TASKS=True) @override_settings(LD_DISABLE_BACKGROUND_TASKS=True)
def test_create_web_archive_snapshot_should_not_run_when_background_tasks_are_disabled(self): def test_create_web_archive_snapshot_should_not_run_when_background_tasks_are_disabled(self):
bookmark = self.setup_bookmark() bookmark = self.setup_bookmark()
create_web_archive_snapshot(bookmark.id, False) tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, False)
self.assertEqual(Task.objects.count(), 0)
def test_create_web_archive_snapshot_should_not_run_when_web_archive_integration_is_disabled(self):
self.user.profile.web_archive_integration = UserProfile.WEB_ARCHIVE_INTEGRATION_DISABLED
self.user.profile.save()
bookmark = self.setup_bookmark()
tasks.create_web_archive_snapshot(self.get_or_create_test_user(), bookmark, False)
self.assertEqual(Task.objects.count(), 0) self.assertEqual(Task.objects.count(), 0)
@ -104,9 +118,9 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
self.setup_bookmark() self.setup_bookmark()
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')):
schedule_bookmarks_without_snapshots(user.id) tasks.schedule_bookmarks_without_snapshots(user)
self.run_pending_task(schedule_bookmarks_without_snapshots) self.run_pending_task(tasks._schedule_bookmarks_without_snapshots_task)
self.run_all_pending_tasks(create_web_archive_snapshot) self.run_all_pending_tasks(tasks._create_web_archive_snapshot_task)
for bookmark in Bookmark.objects.all(): for bookmark in Bookmark.objects.all():
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com')
@ -118,9 +132,9 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
self.setup_bookmark(web_archive_snapshot_url='https://example.com') self.setup_bookmark(web_archive_snapshot_url='https://example.com')
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://other.com')):
schedule_bookmarks_without_snapshots(user.id) tasks.schedule_bookmarks_without_snapshots(user)
self.run_pending_task(schedule_bookmarks_without_snapshots) self.run_pending_task(tasks._schedule_bookmarks_without_snapshots_task)
self.run_all_pending_tasks(create_web_archive_snapshot) self.run_all_pending_tasks(tasks._create_web_archive_snapshot_task)
for bookmark in Bookmark.objects.all(): for bookmark in Bookmark.objects.all():
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com')
@ -136,9 +150,9 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
self.setup_bookmark(user=other_user) self.setup_bookmark(user=other_user)
with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')): with patch.object(waybackpy, 'Url', return_value=MockWaybackUrl('https://example.com')):
schedule_bookmarks_without_snapshots(user.id) tasks.schedule_bookmarks_without_snapshots(user)
self.run_pending_task(schedule_bookmarks_without_snapshots) self.run_pending_task(tasks._schedule_bookmarks_without_snapshots_task)
self.run_all_pending_tasks(create_web_archive_snapshot) self.run_all_pending_tasks(tasks._create_web_archive_snapshot_task)
for bookmark in Bookmark.objects.all().filter(owner=user): for bookmark in Bookmark.objects.all().filter(owner=user):
self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com') self.assertEqual(bookmark.web_archive_snapshot_url, 'https://example.com')
@ -148,7 +162,13 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
@override_settings(LD_DISABLE_BACKGROUND_TASKS=True) @override_settings(LD_DISABLE_BACKGROUND_TASKS=True)
def test_schedule_bookmarks_without_snapshots_should_not_run_when_background_tasks_are_disabled(self): def test_schedule_bookmarks_without_snapshots_should_not_run_when_background_tasks_are_disabled(self):
user = self.get_or_create_test_user() tasks.schedule_bookmarks_without_snapshots(self.user)
schedule_bookmarks_without_snapshots(user.id)
self.assertEqual(Task.objects.count(), 0)
def test_schedule_bookmarks_without_snapshots_should_not_run_when_web_archive_integration_is_disabled(self):
self.user.profile.web_archive_integration = UserProfile.WEB_ARCHIVE_INTEGRATION_DISABLED
self.user.profile.save()
tasks.schedule_bookmarks_without_snapshots(self.user)
self.assertEqual(Task.objects.count(), 0) self.assertEqual(Task.objects.count(), 0)

View file

@ -55,4 +55,4 @@ class ImporterTestCase(TestCase, BookmarkFactoryMixin):
with patch.object(tasks, 'schedule_bookmarks_without_snapshots') as mock_schedule_bookmarks_without_snapshots: with patch.object(tasks, 'schedule_bookmarks_without_snapshots') as mock_schedule_bookmarks_without_snapshots:
import_netscape_html(test_html, user) import_netscape_html(test_html, user)
mock_schedule_bookmarks_without_snapshots.assert_called_once_with(user.id) mock_schedule_bookmarks_without_snapshots.assert_called_once_with(user)

View file

@ -27,6 +27,7 @@ class SettingsGeneralViewTestCase(TestCase, BookmarkFactoryMixin):
'theme': UserProfile.THEME_DARK, 'theme': UserProfile.THEME_DARK,
'bookmark_date_display': UserProfile.BOOKMARK_DATE_DISPLAY_HIDDEN, 'bookmark_date_display': UserProfile.BOOKMARK_DATE_DISPLAY_HIDDEN,
'bookmark_link_target': UserProfile.BOOKMARK_LINK_TARGET_SELF, 'bookmark_link_target': UserProfile.BOOKMARK_LINK_TARGET_SELF,
'web_archive_integration': UserProfile.WEB_ARCHIVE_INTEGRATION_ENABLED,
} }
response = self.client.post(reverse('bookmarks:settings.general'), form_data) response = self.client.post(reverse('bookmarks:settings.general'), form_data)
@ -36,3 +37,4 @@ class SettingsGeneralViewTestCase(TestCase, BookmarkFactoryMixin):
self.assertEqual(self.user.profile.theme, form_data['theme']) self.assertEqual(self.user.profile.theme, form_data['theme'])
self.assertEqual(self.user.profile.bookmark_date_display, form_data['bookmark_date_display']) self.assertEqual(self.user.profile.bookmark_date_display, form_data['bookmark_date_display'])
self.assertEqual(self.user.profile.bookmark_link_target, form_data['bookmark_link_target']) self.assertEqual(self.user.profile.bookmark_link_target, form_data['bookmark_link_target'])
self.assertEqual(self.user.profile.web_archive_integration, form_data['web_archive_integration'])

View file

@ -12,4 +12,4 @@ class SignalsTestCase(TestCase, BookmarkFactoryMixin):
with patch.object(tasks, 'schedule_bookmarks_without_snapshots') as mock_schedule_bookmarks_without_snapshots: with patch.object(tasks, 'schedule_bookmarks_without_snapshots') as mock_schedule_bookmarks_without_snapshots:
self.client.force_login(user) self.client.force_login(user)
mock_schedule_bookmarks_without_snapshots.assert_called_once_with(user.id) mock_schedule_bookmarks_without_snapshots.assert_called_once_with(user)

View file

@ -0,0 +1,108 @@
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from bookmarks.models import Toast
from bookmarks.tests.helpers import BookmarkFactoryMixin, random_sentence, disable_logging
class ToastsViewTestCase(TestCase, BookmarkFactoryMixin):
def setUp(self) -> None:
user = self.get_or_create_test_user()
self.client.force_login(user)
def create_toast(self, user: User = None, message: str = None, acknowledged: bool = False):
if not user:
user = self.user
if not message:
message = random_sentence()
toast = Toast(owner=user, key='test', message=message, acknowledged=acknowledged)
toast.save()
return toast
def test_should_render_unacknowledged_toasts(self):
self.create_toast()
self.create_toast()
self.create_toast(acknowledged=True)
response = self.client.get(reverse('bookmarks:index'))
# Should render toasts container
self.assertContains(response, '<div class="toasts container grid-lg">')
# Should render two toasts
self.assertContains(response, '<div class="toast">', count=2)
def test_should_not_render_acknowledged_toasts(self):
self.create_toast(acknowledged=True)
self.create_toast(acknowledged=True)
self.create_toast(acknowledged=True)
response = self.client.get(reverse('bookmarks:index'))
# Should not render toasts container
self.assertContains(response, '<div class="toasts container grid-lg">', count=0)
# Should not render toasts
self.assertContains(response, '<div class="toast">', count=0)
def test_should_not_render_toasts_of_other_users(self):
other_user = User.objects.create_user('otheruser', 'otheruser@example.com', 'password123')
self.create_toast(user=other_user)
self.create_toast(user=other_user)
self.create_toast(user=other_user)
response = self.client.get(reverse('bookmarks:index'))
# Should not render toasts container
self.assertContains(response, '<div class="toasts container grid-lg">', count=0)
# Should not render toasts
self.assertContains(response, '<div class="toast">', count=0)
def test_toast_content(self):
toast = self.create_toast()
expected_toast = f'''
<div class="toast">
{toast.message}
<a href="{reverse('bookmarks:toasts.acknowledge', args=[toast.id])}?return_url={reverse('bookmarks:index')}" class="btn btn-clear float-right"></a>
</div>
'''
response = self.client.get(reverse('bookmarks:index'))
html = response.content.decode()
self.assertInHTML(expected_toast, html)
def test_acknowledge_toast(self):
toast = self.create_toast()
self.client.get(reverse('bookmarks:toasts.acknowledge', args=[toast.id]))
toast.refresh_from_db()
self.assertTrue(toast.acknowledged)
def test_acknowledge_toast_should_redirect_to_return_url(self):
toast = self.create_toast()
return_url = reverse('bookmarks:settings.general')
acknowledge_url = reverse('bookmarks:toasts.acknowledge', args=[toast.id])
acknowledge_url = acknowledge_url + '?return_url=' + return_url
response = self.client.get(acknowledge_url)
self.assertRedirects(response, return_url)
def test_acknowledge_toast_should_redirect_to_index_by_default(self):
toast = self.create_toast()
response = self.client.get(reverse('bookmarks:toasts.acknowledge', args=[toast.id]))
self.assertRedirects(response, reverse('bookmarks:index'))
@disable_logging
def test_acknowledge_toast_should_not_acknowledge_other_users_toast(self):
other_user = User.objects.create_user('otheruser', 'otheruser@example.com', 'password123')
toast = self.create_toast(user=other_user)
response = self.client.get(reverse('bookmarks:toasts.acknowledge', args=[toast.id]))
self.assertEqual(response.status_code, 404)

View file

@ -22,6 +22,8 @@ urlpatterns = [
path('settings/integrations', views.settings.integrations, name='settings.integrations'), path('settings/integrations', views.settings.integrations, name='settings.integrations'),
path('settings/import', views.settings.bookmark_import, name='settings.import'), path('settings/import', views.settings.bookmark_import, name='settings.import'),
path('settings/export', views.settings.bookmark_export, name='settings.export'), path('settings/export', views.settings.bookmark_export, name='settings.export'),
# Toasts
path('toasts/<int:toast_id>/acknowledge', views.toasts.acknowledge, name='toasts.acknowledge'),
# API # API
path('api/', include(router.urls), name='api') path('api/', include(router.urls), name='api')
] ]

View file

@ -1,2 +1,3 @@
from .bookmarks import * from .bookmarks import *
from .settings import * from .settings import *
from .toasts import *

19
bookmarks/views/toasts.py Normal file
View file

@ -0,0 +1,19 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, Http404
from django.urls import reverse
from bookmarks.models import Toast
from bookmarks.utils import get_safe_return_url
@login_required
def acknowledge(request, toast_id: int):
try:
toast = Toast.objects.get(pk=toast_id, owner=request.user)
except Toast.DoesNotExist:
raise Http404('Toast does not exist')
toast.acknowledged = True
toast.save()
return_url = get_safe_return_url(request.GET.get('return_url'), reverse('bookmarks:index'))
return HttpResponseRedirect(return_url)

View file

@ -69,6 +69,7 @@ TEMPLATES = [
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'bookmarks.context_processors.toasts',
], ],
}, },
}, },