From 534d66eccdc421d38ffa3b731865569a4f904033 Mon Sep 17 00:00:00 2001 From: ffont Date: Tue, 10 Dec 2024 16:41:34 +0100 Subject: [PATCH] Fix bookmark category download filename --- apiv2/views.py | 2 +- bookmarks/models.py | 7 +++++++ bookmarks/views.py | 2 +- freesound/urls.py | 2 +- sounds/views.py | 2 +- templates/sounds/pack.html | 4 ++-- utils/downloads.py | 4 +++- utils/tests/tests.py | 2 +- 8 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apiv2/views.py b/apiv2/views.py index 540ce069f..a682061d0 100755 --- a/apiv2/views.py +++ b/apiv2/views.py @@ -704,7 +704,7 @@ def get(self, request, *args, **kwargs): sounds_list = pack.sounds.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license') licenses_url = (reverse('pack-licenses', args=[pack.user.username, pack.id])) licenses_content = pack.get_attribution(sound_qs=sounds_list) - return download_sounds(licenses_url, licenses_content, sounds_list) + return download_sounds(licenses_url, licenses_content, sounds_list, pack.friendly_filename()) ################## diff --git a/bookmarks/models.py b/bookmarks/models.py index bda360449..f5ad64aa8 100755 --- a/bookmarks/models.py +++ b/bookmarks/models.py @@ -20,6 +20,7 @@ from django.contrib.auth.models import User from django.db import models +from django.utils.text import slugify from sounds.models import Sound, License from django.template.loader import render_to_string @@ -48,6 +49,12 @@ def get_attribution(self, sound_qs=None): licenses=licenses, sound_list=sound_qs)) return attribution + + @property + def download_filename(self): + name_slug = slugify(self.name) + username_slug = slugify(self.user.username) + return "%d__%s__%s.zip" % (self.id, username_slug, name_slug) class Bookmark(models.Model): diff --git a/bookmarks/views.py b/bookmarks/views.py index 997241070..e1a8a7a94 100755 --- a/bookmarks/views.py +++ b/bookmarks/views.py @@ -97,7 +97,7 @@ def download_bookmark_category(request, category_id): licenses_url = (reverse('category-licenses', args=[category_id])) licenses_content = category.get_attribution(sound_qs=sounds_list) # NOTE: unlike pack downloads, here we are not doing any cache check to avoid consecutive downloads - return download_sounds(licenses_file_url=licenses_url, licenses_file_content=licenses_content, sounds_list=sounds_list) + return download_sounds(licenses_url, licenses_content, sounds_list, category.download_filename) def bookmark_category_licenses(request, category_id): diff --git a/freesound/urls.py b/freesound/urls.py index 11152e57b..48d6ed94e 100644 --- a/freesound/urls.py +++ b/freesound/urls.py @@ -67,7 +67,7 @@ path('people//packs//', sounds.views.pack, name="pack"), path('people//packs//section/stats/', sounds.views.pack_stats_section, name="pack-stats-section"), path('people//packs//edit/', sounds.views.pack_edit, name="pack-edit"), - re_path(r'^people/(?P[^//]+)/packs/(?P\d+)/download/.*$', sounds.views.pack_download, name="pack-download"), + path('people//packs//download/', sounds.views.pack_download, name="pack-download"), path('people//packs//downloaders/', sounds.views.pack_downloaders, name="pack-downloaders"), path('people//packs//licenses/', sounds.views.pack_licenses, name="pack-licenses"), path('people//packs//geotags/', geotags.views.for_pack, name="pack-geotags"), diff --git a/sounds/views.py b/sounds/views.py index db569cd55..cf5125486 100644 --- a/sounds/views.py +++ b/sounds/views.py @@ -371,7 +371,7 @@ def pack_download(request, username, pack_id): sounds_list = pack.sounds.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license') licenses_url = (reverse('pack-licenses', args=[username, pack_id])) licenses_content = pack.get_attribution(sound_qs=sounds_list) - return download_sounds(licenses_url, licenses_content, sounds_list) + return download_sounds(licenses_url, licenses_content, sounds_list, pack.friendly_filename()) def pack_licenses(request, username, pack_id): diff --git a/templates/sounds/pack.html b/templates/sounds/pack.html index 2b34e61bc..165ff7b76 100644 --- a/templates/sounds/pack.html +++ b/templates/sounds/pack.html @@ -90,9 +90,9 @@
No sounds... 😟
{% if pack.num_sounds %}
{% if request.user.is_authenticated %} - Download pack + Download pack {% else %} - Login to download + Login to download {% endif %}
{% endif %} diff --git a/utils/downloads.py b/utils/downloads.py index da02628c5..99eb68b46 100644 --- a/utils/downloads.py +++ b/utils/downloads.py @@ -29,7 +29,7 @@ from utils.nginxsendfile import prepare_sendfile_arguments_for_sound_download -def download_sounds(licenses_file_url, licenses_file_content, sounds_list): +def download_sounds(licenses_file_url, licenses_file_content, sounds_list, download_filename): """From a list of sounds generates the HttpResponse with the information of the wav files of the sounds and a text file with the license. This response is handled by mod_zipfile of nginx to generate a zip file with the content. @@ -38,6 +38,7 @@ def download_sounds(licenses_file_url, licenses_file_content, sounds_list): licenses_file_url (str): url to the sound Pack or BookmarkCategory licenses licenses_file_content (str): attributions for the different sounds in the Pack or BookmarkCategory sounds_list (django.db.models.query.QuerySet): list of sounds forming the Pack or BookmarkCategory + download_filename (str): name of the zip file to be downloaded Returns: HttpResponse: information of the wav files of the sounds and a text file with the license @@ -54,6 +55,7 @@ def download_sounds(licenses_file_url, licenses_file_content, sounds_list): filelist += "%s %i %s %s\r\n" % (sound.crc, sound.filesize, url, name) response = HttpResponse(filelist, content_type="text/plain") response['X-Archive-Files'] = 'zip' + response['Content-Disposition'] = f"attachment; filename=\"{download_filename}\"" return response diff --git a/utils/tests/tests.py b/utils/tests/tests.py index 28dc237cc..864e54c6d 100644 --- a/utils/tests/tests.py +++ b/utils/tests/tests.py @@ -60,7 +60,7 @@ def test_download_sounds(self): sounds_list = pack.sounds.filter(processing_state="OK", moderation_state="OK").select_related('user', 'license') licenses_url = (reverse('pack-licenses', args=["testuser", pack.id])) licenses_content = pack.get_attribution(sound_qs=sounds_list) - ret = utils.downloads.download_sounds(licenses_url, licenses_content, sounds_list) + ret = utils.downloads.download_sounds(licenses_url, licenses_content, sounds_list, pack.friendly_filename()) self.assertEqual(ret.status_code, 200) @override_uploads_path_with_temp_directory