Add black code formatter

This commit is contained in:
Sascha Ißbrücker
2024-01-27 11:29:16 +01:00
parent 6775633be5
commit 98b9a9c1a0
128 changed files with 7181 additions and 4264 deletions

View File

@@ -16,34 +16,48 @@ from bookmarks.views.partials import contexts
class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
def assertBookmarksLink(self, html: str, bookmark: Bookmark, link_target: str = '_blank'):
favicon_img = f'<img src="/static/{bookmark.favicon_file}" alt="">' if bookmark.favicon_file else ''
def assertBookmarksLink(
self, html: str, bookmark: Bookmark, link_target: str = "_blank"
):
favicon_img = (
f'<img src="/static/{bookmark.favicon_file}" alt="">'
if bookmark.favicon_file
else ""
)
self.assertInHTML(
f'''
f"""
<a href="{bookmark.url}"
target="{link_target}"
rel="noopener">
{favicon_img}
<span>{bookmark.resolved_title}</span>
</a>
''',
html
""",
html,
)
def assertDateLabel(self, html: str, label_content: str):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<span>{label_content}</span>
<span class="separator">|</span>
''', html)
""",
html,
)
def assertWebArchiveLink(self, html: str, label_content: str, url: str, link_target: str = '_blank'):
self.assertInHTML(f'''
def assertWebArchiveLink(
self, html: str, label_content: str, url: str, link_target: str = "_blank"
):
self.assertInHTML(
f"""
<a href="{url}"
title="Show snapshot on the Internet Archive Wayback Machine" target="{link_target}" rel="noopener">
{label_content}
</a>
<span class="separator">|</span>
''', html)
""",
html,
)
def assertBookmarkActions(self, html: str, bookmark: Bookmark):
self.assertBookmarkActionsCount(html, bookmark, count=1)
@@ -53,20 +67,32 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
def assertBookmarkActionsCount(self, html: str, bookmark: Bookmark, count=1):
# Edit link
edit_url = reverse('bookmarks:edit', args=[bookmark.id])
self.assertInHTML(f'''
edit_url = reverse("bookmarks:edit", args=[bookmark.id])
self.assertInHTML(
f"""
<a href="{edit_url}?return_url=/bookmarks">Edit</a>
''', html, count=count)
""",
html,
count=count,
)
# Archive link
self.assertInHTML(f'''
self.assertInHTML(
f"""
<button type="submit" name="archive" value="{bookmark.id}"
class="btn btn-link btn-sm">Archive</button>
''', html, count=count)
""",
html,
count=count,
)
# Delete link
self.assertInHTML(f'''
self.assertInHTML(
f"""
<button ld-confirm-button type="submit" name="remove" value="{bookmark.id}"
class="btn btn-link btn-sm">Remove</button>
''', html, count=count)
""",
html,
count=count,
)
def assertShareInfo(self, html: str, bookmark: Bookmark):
self.assertShareInfoCount(html, bookmark, 1)
@@ -75,11 +101,15 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.assertShareInfoCount(html, bookmark, 0)
def assertShareInfoCount(self, html: str, bookmark: Bookmark, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<span>Shared by
<a href="?user={bookmark.owner.username}">{bookmark.owner.username}</a>
</span>
''', html, count=count)
""",
html,
count=count,
)
def assertFaviconVisible(self, html: str, bookmark: Bookmark):
self.assertFaviconCount(html, bookmark, 1)
@@ -88,47 +118,68 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.assertFaviconCount(html, bookmark, 0)
def assertFaviconCount(self, html: str, bookmark: Bookmark, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<img src="/static/{bookmark.favicon_file}" alt="">
''', html, count=count)
""",
html,
count=count,
)
def assertBookmarkURLCount(self, html: str, bookmark: Bookmark, link_target: str = '_blank', count=0):
self.assertInHTML(f'''
def assertBookmarkURLCount(
self, html: str, bookmark: Bookmark, link_target: str = "_blank", count=0
):
self.assertInHTML(
f"""
<div class="url-path truncate">
<a href="{bookmark.url}" target="{link_target}" rel="noopener"
class="url-display text-sm">
{bookmark.url}
</a>
</div>
''', html, count)
""",
html,
count,
)
def assertBookmarkURLVisible(self, html: str, bookmark: Bookmark):
self.assertBookmarkURLCount(html, bookmark, count=1)
def assertBookmarkURLHidden(self, html: str, bookmark: Bookmark, link_target: str = '_blank'):
def assertBookmarkURLHidden(
self, html: str, bookmark: Bookmark, link_target: str = "_blank"
):
self.assertBookmarkURLCount(html, bookmark, count=0)
def assertNotes(self, html: str, notes_html: str, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<div class="notes bg-gray text-gray-dark">
<div class="notes-content">
{notes_html}
</div>
</div>
''', html, count=count)
""",
html,
count=count,
)
def assertNotesToggle(self, html: str, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<button type="button" class="btn btn-link btn-sm btn-icon toggle-notes">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16">
<use xlink:href="#ld-icon-note"></use>
</svg>
Notes
</button>
''', html, count=count)
""",
html,
count=count,
)
def assertUnshareButton(self, html: str, bookmark: Bookmark, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<button type="submit" name="unshare" value="{bookmark.id}"
class="btn btn-link btn-sm btn-icon"
ld-confirm-button confirm-icon="ld-icon-unshare" confirm-question="Unshare?">
@@ -137,10 +188,14 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
</svg>
Shared
</button>
''', html, count=count)
""",
html,
count=count,
)
def assertMarkAsReadButton(self, html: str, bookmark: Bookmark, count=1):
self.assertInHTML(f'''
self.assertInHTML(
f"""
<button type="submit" name="mark_as_read" value="{bookmark.id}"
class="btn btn-link btn-sm btn-icon"
ld-confirm-button confirm-icon="ld-icon-read" confirm-question="Mark as read?">
@@ -149,12 +204,19 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
</svg>
Unread
</button>
''', html, count=count)
""",
html,
count=count,
)
def render_template(self,
url='/bookmarks',
context_type: Type[contexts.BookmarkListContext] = contexts.ActiveBookmarkListContext,
user: User | AnonymousUser = None) -> str:
def render_template(
self,
url="/bookmarks",
context_type: Type[
contexts.BookmarkListContext
] = contexts.ActiveBookmarkListContext,
user: User | AnonymousUser = None,
) -> str:
rf = RequestFactory()
request = rf.get(url)
request.user = user or self.get_or_create_test_user()
@@ -162,14 +224,14 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
middleware(request)
bookmark_list_context = context_type(request)
context = RequestContext(request, {'bookmark_list': bookmark_list_context})
context = RequestContext(request, {"bookmark_list": bookmark_list_context})
template = Template(
"{% include 'bookmarks/bookmark_list.html' %}"
)
template = Template("{% include 'bookmarks/bookmark_list.html' %}")
return template.render(context)
def setup_date_format_test(self, date_display_setting: str, web_archive_url: str = ''):
def setup_date_format_test(
self, date_display_setting: str, web_archive_url: str = ""
):
bookmark = self.setup_bookmark()
bookmark.date_added = timezone.now() - relativedelta(days=8)
bookmark.web_archive_snapshot_url = web_archive_url
@@ -180,38 +242,46 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
return bookmark
def test_should_respect_absolute_date_setting(self):
bookmark = self.setup_date_format_test(UserProfile.BOOKMARK_DATE_DISPLAY_ABSOLUTE)
bookmark = self.setup_date_format_test(
UserProfile.BOOKMARK_DATE_DISPLAY_ABSOLUTE
)
html = self.render_template()
formatted_date = formats.date_format(bookmark.date_added, 'SHORT_DATE_FORMAT')
formatted_date = formats.date_format(bookmark.date_added, "SHORT_DATE_FORMAT")
self.assertDateLabel(html, formatted_date)
def test_should_render_web_archive_link_with_absolute_date_setting(self):
bookmark = self.setup_date_format_test(UserProfile.BOOKMARK_DATE_DISPLAY_ABSOLUTE,
'https://web.archive.org/web/20210811214511/https://wanikani.com/')
bookmark = self.setup_date_format_test(
UserProfile.BOOKMARK_DATE_DISPLAY_ABSOLUTE,
"https://web.archive.org/web/20210811214511/https://wanikani.com/",
)
html = self.render_template()
formatted_date = formats.date_format(bookmark.date_added, 'SHORT_DATE_FORMAT')
formatted_date = formats.date_format(bookmark.date_added, "SHORT_DATE_FORMAT")
self.assertWebArchiveLink(html, formatted_date, bookmark.web_archive_snapshot_url)
self.assertWebArchiveLink(
html, formatted_date, bookmark.web_archive_snapshot_url
)
def test_should_respect_relative_date_setting(self):
self.setup_date_format_test(UserProfile.BOOKMARK_DATE_DISPLAY_RELATIVE)
html = self.render_template()
self.assertDateLabel(html, '1 week ago')
self.assertDateLabel(html, "1 week ago")
def test_should_render_web_archive_link_with_relative_date_setting(self):
bookmark = self.setup_date_format_test(UserProfile.BOOKMARK_DATE_DISPLAY_RELATIVE,
'https://web.archive.org/web/20210811214511/https://wanikani.com/')
bookmark = self.setup_date_format_test(
UserProfile.BOOKMARK_DATE_DISPLAY_RELATIVE,
"https://web.archive.org/web/20210811214511/https://wanikani.com/",
)
html = self.render_template()
self.assertWebArchiveLink(html, '1 week ago', bookmark.web_archive_snapshot_url)
self.assertWebArchiveLink(html, "1 week ago", bookmark.web_archive_snapshot_url)
def test_bookmark_link_target_should_be_blank_by_default(self):
bookmark = self.setup_bookmark()
html = self.render_template()
self.assertBookmarksLink(html, bookmark, link_target='_blank')
self.assertBookmarksLink(html, bookmark, link_target="_blank")
def test_bookmark_link_target_should_respect_user_profile(self):
profile = self.get_or_create_test_user().profile
@@ -221,17 +291,19 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
bookmark = self.setup_bookmark()
html = self.render_template()
self.assertBookmarksLink(html, bookmark, link_target='_self')
self.assertBookmarksLink(html, bookmark, link_target="_self")
def test_web_archive_link_target_should_be_blank_by_default(self):
bookmark = self.setup_bookmark()
bookmark.date_added = timezone.now() - relativedelta(days=8)
bookmark.web_archive_snapshot_url = 'https://example.com'
bookmark.web_archive_snapshot_url = "https://example.com"
bookmark.save()
html = self.render_template()
self.assertWebArchiveLink(html, '1 week ago', bookmark.web_archive_snapshot_url, link_target='_blank')
self.assertWebArchiveLink(
html, "1 week ago", bookmark.web_archive_snapshot_url, link_target="_blank"
)
def test_web_archive_link_target_should_respect_user_profile(self):
profile = self.get_or_create_test_user().profile
@@ -240,12 +312,14 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
bookmark = self.setup_bookmark()
bookmark.date_added = timezone.now() - relativedelta(days=8)
bookmark.web_archive_snapshot_url = 'https://example.com'
bookmark.web_archive_snapshot_url = "https://example.com"
bookmark.save()
html = self.render_template()
self.assertWebArchiveLink(html, '1 week ago', bookmark.web_archive_snapshot_url, link_target='_self')
self.assertWebArchiveLink(
html, "1 week ago", bookmark.web_archive_snapshot_url, link_target="_self"
)
def test_should_reflect_unread_state_as_css_class(self):
self.setup_bookmark(unread=True)
@@ -281,7 +355,9 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.assertNoShareInfo(html, bookmark)
def test_show_share_info_for_non_owned_bookmarks(self):
other_user = User.objects.create_user('otheruser', 'otheruser@example.com', 'password123')
other_user = User.objects.create_user(
"otheruser", "otheruser@example.com", "password123"
)
other_user.profile.enable_sharing = True
other_user.profile.save()
@@ -292,25 +368,32 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.assertShareInfo(html, bookmark)
def test_share_info_user_link_keeps_query_params(self):
other_user = User.objects.create_user('otheruser', 'otheruser@example.com', 'password123')
other_user = User.objects.create_user(
"otheruser", "otheruser@example.com", "password123"
)
other_user.profile.enable_sharing = True
other_user.profile.save()
bookmark = self.setup_bookmark(user=other_user, shared=True, title='foo')
html = self.render_template(url='/bookmarks?q=foo', context_type=contexts.SharedBookmarkListContext)
bookmark = self.setup_bookmark(user=other_user, shared=True, title="foo")
html = self.render_template(
url="/bookmarks?q=foo", context_type=contexts.SharedBookmarkListContext
)
self.assertInHTML(f'''
self.assertInHTML(
f"""
<span>Shared by
<a href="?q=foo&user={bookmark.owner.username}">{bookmark.owner.username}</a>
</span>
''', html)
""",
html,
)
def test_favicon_should_be_visible_when_favicons_enabled(self):
profile = self.get_or_create_test_user().profile
profile.enable_favicons = True
profile.save()
bookmark = self.setup_bookmark(favicon_file='https_example_com.png')
bookmark = self.setup_bookmark(favicon_file="https_example_com.png")
html = self.render_template()
self.assertFaviconVisible(html, bookmark)
@@ -320,7 +403,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.enable_favicons = True
profile.save()
bookmark = self.setup_bookmark(favicon_file='')
bookmark = self.setup_bookmark(favicon_file="")
html = self.render_template()
self.assertFaviconHidden(html, bookmark)
@@ -330,7 +413,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.enable_favicons = False
profile.save()
bookmark = self.setup_bookmark(favicon_file='https_example_com.png')
bookmark = self.setup_bookmark(favicon_file="https_example_com.png")
html = self.render_template()
self.assertFaviconHidden(html, bookmark)
@@ -428,21 +511,23 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.setup_bookmark()
html = self.render_template()
self.assertNotes(html, '', 0)
self.assertNotes(html, "", 0)
self.assertNotesToggle(html, 0)
def test_with_notes(self):
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = self.render_template()
note_html = '<p>Test note</p>'
note_html = "<p>Test note</p>"
self.assertNotes(html, note_html, 1)
def test_note_renders_markdown(self):
self.setup_bookmark(notes='**Example:** `print("Hello world!")`')
html = self.render_template()
note_html = '<p><strong>Example:</strong> <code>print("Hello world!")</code></p>'
note_html = (
'<p><strong>Example:</strong> <code>print("Hello world!")</code></p>'
)
self.assertNotes(html, note_html, 1)
def test_note_cleans_html(self):
@@ -453,7 +538,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
self.assertNotes(html, note_html, 1)
def test_notes_are_hidden_initially_by_default(self):
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = collapse_whitespace(self.render_template())
self.assertIn('<ul class="bookmark-list" data-bookmarks-total="1">', html)
@@ -463,7 +548,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.permanent_notes = False
profile.save()
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = collapse_whitespace(self.render_template())
self.assertIn('<ul class="bookmark-list" data-bookmarks-total="1">', html)
@@ -473,13 +558,15 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.permanent_notes = True
profile.save()
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = collapse_whitespace(self.render_template())
self.assertIn('<ul class="bookmark-list show-notes" data-bookmarks-total="1">', html)
self.assertIn(
'<ul class="bookmark-list show-notes" data-bookmarks-total="1">', html
)
def test_toggle_notes_is_visible_by_default(self):
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = self.render_template()
self.assertNotesToggle(html, 1)
@@ -489,7 +576,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.permanent_notes = False
profile.save()
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = self.render_template()
self.assertNotesToggle(html, 1)
@@ -499,7 +586,7 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
profile.permanent_notes = True
profile.save()
self.setup_bookmark(notes='Test note')
self.setup_bookmark(notes="Test note")
html = self.render_template()
self.assertNotesToggle(html, 0)
@@ -512,25 +599,35 @@ class BookmarkListTemplateTest(TestCase, BookmarkFactoryMixin):
bookmark = self.setup_bookmark()
bookmark.date_added = timezone.now() - relativedelta(days=8)
bookmark.web_archive_snapshot_url = 'https://web.archive.org/web/20230531200136/https://example.com'
bookmark.web_archive_snapshot_url = (
"https://web.archive.org/web/20230531200136/https://example.com"
)
bookmark.notes = '**Example:** `print("Hello world!")`'
bookmark.favicon_file = 'https_example_com.png'
bookmark.favicon_file = "https_example_com.png"
bookmark.shared = True
bookmark.unread = True
bookmark.save()
html = self.render_template(context_type=contexts.SharedBookmarkListContext, user=AnonymousUser())
self.assertBookmarksLink(html, bookmark, link_target='_blank')
self.assertWebArchiveLink(html, '1 week ago', bookmark.web_archive_snapshot_url, link_target='_blank')
html = self.render_template(
context_type=contexts.SharedBookmarkListContext, user=AnonymousUser()
)
self.assertBookmarksLink(html, bookmark, link_target="_blank")
self.assertWebArchiveLink(
html, "1 week ago", bookmark.web_archive_snapshot_url, link_target="_blank"
)
self.assertNoBookmarkActions(html, bookmark)
self.assertShareInfo(html, bookmark)
self.assertMarkAsReadButton(html, bookmark, count=0)
self.assertUnshareButton(html, bookmark, count=0)
note_html = '<p><strong>Example:</strong> <code>print("Hello world!")</code></p>'
note_html = (
'<p><strong>Example:</strong> <code>print("Hello world!")</code></p>'
)
self.assertNotes(html, note_html, 1)
self.assertFaviconVisible(html, bookmark)
def test_empty_state(self):
html = self.render_template()
self.assertInHTML('<p class="empty-title h5">You have no bookmarks yet</p>', html)
self.assertInHTML(
'<p class="empty-title h5">You have no bookmarks yet</p>', html
)