Add bulk and single bookmark metadata refresh (#999)

* Add url create/edit query paramter to clear cache

* Add refresh bookmark metadata button in create/edit bookmark page

* Fix refresh bookmark metadata when editing existing bookmark

* Add bulk refresh metadata functionality

* Fix test cases for bulk view dropdown selection list

* Allow bulk metadata refresh when background tasks are disabled

* Move load preview image call on refresh metadata

* Update bookmark modified time on metadata refresh

* Rename function to align with convention

* Add tests for refresh task

* Add tests for bookmarks service refresh metadata

* Add tests for bookmarks api disable cache on check

* Remove bulk refresh metadata when background tasks disabled

* Refactor refresh metadata task

* Remove unnecessary call

* Fix testing mock name

* Abstract clearing metadata cache

* Add test to check if load page is called twice when cache disabled

* Remove refresh button for new bookmarks

* Remove strict disable cache is true check

* Refactor refresh metadata form logic into its own function

* move button and highlight changes

* polish and update tests

---------

Co-authored-by: Sascha Ißbrücker <sascha.issbruecker@gmail.com>
This commit is contained in:
Josh S
2025-03-22 06:34:10 -04:00
committed by GitHub
parent f1acb4f7c9
commit a23c357f2f
14 changed files with 300 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ from waybackpy.exceptions import WaybackError
from bookmarks.models import BookmarkAsset, UserProfile
from bookmarks.services import tasks
from bookmarks.services.website_loader import WebsiteMetadata
from bookmarks.tests.helpers import BookmarkFactoryMixin
@@ -615,3 +616,52 @@ class BookmarkTasksTestCase(TestCase, BookmarkFactoryMixin):
self.assertEqual(count, 3)
self.assertEqual(BookmarkAsset.objects.count(), count)
@override_settings(LD_DISABLE_BACKGROUND_TASKS=True)
def test_refresh_metadata_task_not_called_when_background_tasks_disabled(self):
bookmark = self.setup_bookmark()
with mock.patch(
"bookmarks.services.tasks._refresh_metadata_task"
) as mock_refresh_metadata_task:
tasks.refresh_metadata(bookmark)
mock_refresh_metadata_task.assert_not_called()
@override_settings(LD_DISABLE_BACKGROUND_TASKS=False)
def test_refresh_metadata_task_called_when_background_tasks_enabled(self):
bookmark = self.setup_bookmark()
with mock.patch(
"bookmarks.services.tasks._refresh_metadata_task"
) as mock_refresh_metadata_task:
tasks.refresh_metadata(bookmark)
mock_refresh_metadata_task.assert_called_once()
def test_refresh_metadata_task_should_handle_missing_bookmark(self):
with mock.patch(
"bookmarks.services.website_loader.load_website_metadata"
) as mock_load_website_metadata:
tasks._refresh_metadata_task(123)
mock_load_website_metadata.assert_not_called()
def test_refresh_metadata_updates_title_description(self):
bookmark = self.setup_bookmark(
title="Initial title",
description="Initial description",
)
mock_website_metadata = WebsiteMetadata(
url=bookmark.url,
title="New title",
description="New description",
preview_image=None,
)
with mock.patch(
"bookmarks.services.tasks.load_website_metadata"
) as mock_load_website_metadata:
mock_load_website_metadata.return_value = mock_website_metadata
tasks.refresh_metadata(bookmark)
bookmark.refresh_from_db()
self.assertEqual(bookmark.title, "New title")
self.assertEqual(bookmark.description, "New description")