mirror of
https://github.com/sissbruecker/linkding.git
synced 2025-08-08 03:08:29 +02:00
Add date and time to HTML export filename (#1101)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import datetime
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
@@ -29,9 +30,6 @@ class SettingsExportViewTestCase(TestCase, BookmarkFactoryMixin):
|
|||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response["content-type"], "text/plain; charset=UTF-8")
|
self.assertEqual(response["content-type"], "text/plain; charset=UTF-8")
|
||||||
self.assertEqual(
|
|
||||||
response["Content-Disposition"], 'attachment; filename="bookmarks.html"'
|
|
||||||
)
|
|
||||||
|
|
||||||
for bookmark in Bookmark.objects.all():
|
for bookmark in Bookmark.objects.all():
|
||||||
self.assertContains(response, bookmark.url)
|
self.assertContains(response, bookmark.url)
|
||||||
@@ -78,3 +76,16 @@ class SettingsExportViewTestCase(TestCase, BookmarkFactoryMixin):
|
|||||||
self.assertFormErrorHint(
|
self.assertFormErrorHint(
|
||||||
response, "An error occurred during bookmark export."
|
response, "An error occurred during bookmark export."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_filename_includes_date_and_time(self):
|
||||||
|
self.setup_bookmark()
|
||||||
|
|
||||||
|
# Mock timezone.now to return a fixed datetime for predictable filename
|
||||||
|
fixed_time = datetime.datetime(2023, 5, 15, 14, 30, 45, tzinfo=datetime.timezone.utc)
|
||||||
|
|
||||||
|
with patch("bookmarks.views.settings.timezone.now", return_value=fixed_time):
|
||||||
|
response = self.client.get(reverse("linkding:settings.export"), follow=True)
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
expected_filename = 'attachment; filename="bookmarks_2023-05-15_14-30-45.html"'
|
||||||
|
self.assertEqual(response["Content-Disposition"], expected_filename)
|
||||||
|
@@ -11,6 +11,7 @@ from django.db.models import prefetch_related_objects
|
|||||||
from django.http import HttpResponseRedirect, HttpResponse
|
from django.http import HttpResponseRedirect, HttpResponse
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.utils import timezone
|
||||||
from rest_framework.authtoken.models import Token
|
from rest_framework.authtoken.models import Token
|
||||||
|
|
||||||
from bookmarks.models import (
|
from bookmarks.models import (
|
||||||
@@ -239,8 +240,12 @@ def bookmark_export(request: HttpRequest):
|
|||||||
prefetch_related_objects(bookmarks, "tags")
|
prefetch_related_objects(bookmarks, "tags")
|
||||||
file_content = exporter.export_netscape_html(list(bookmarks))
|
file_content = exporter.export_netscape_html(list(bookmarks))
|
||||||
|
|
||||||
|
# Generate filename with current date and time
|
||||||
|
current_time = timezone.now()
|
||||||
|
filename = current_time.strftime("bookmarks_%Y-%m-%d_%H-%M-%S.html")
|
||||||
|
|
||||||
response = HttpResponse(content_type="text/plain; charset=UTF-8")
|
response = HttpResponse(content_type="text/plain; charset=UTF-8")
|
||||||
response["Content-Disposition"] = 'attachment; filename="bookmarks.html"'
|
response["Content-Disposition"] = f'attachment; filename="{filename}"'
|
||||||
response.write(file_content)
|
response.write(file_content)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
Reference in New Issue
Block a user