Skip to content

Commit

Permalink
Fix bookmark category download filename
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Dec 10, 2024
1 parent f447d47 commit 534d66e
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apiv2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())


##################
Expand Down
7 changes: 7 additions & 0 deletions bookmarks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion bookmarks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion freesound/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
path('people/<username>/packs/<int:pack_id>/', sounds.views.pack, name="pack"),
path('people/<username>/packs/<int:pack_id>/section/stats/', sounds.views.pack_stats_section, name="pack-stats-section"),
path('people/<username>/packs/<int:pack_id>/edit/', sounds.views.pack_edit, name="pack-edit"),
re_path(r'^people/(?P<username>[^//]+)/packs/(?P<pack_id>\d+)/download/.*$', sounds.views.pack_download, name="pack-download"),
path('people/<username>/packs/<int:pack_id>/download/', sounds.views.pack_download, name="pack-download"),
path('people/<username>/packs/<int:pack_id>/downloaders/', sounds.views.pack_downloaders, name="pack-downloaders"),
path('people/<username>/packs/<int:pack_id>/licenses/', sounds.views.pack_licenses, name="pack-licenses"),
path('people/<username>/packs/<int:pack_id>/geotags/', geotags.views.for_pack, name="pack-geotags"),
Expand Down
2 changes: 1 addition & 1 deletion sounds/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions templates/sounds/pack.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ <h5>No sounds... &#128543</h5>
{% if pack.num_sounds %}
<div class="v-spacing-top-6">
{% if request.user.is_authenticated %}
<a class="no-hover btn-primary display-inline-block w-100 text-center" href="{% url 'pack-download' pack.user.username pack.id %}{{ pack.friendly_filename }}" title="Download pack">Download pack</a>
<a class="no-hover btn-primary display-inline-block w-100 text-center" href="{% url 'pack-download' pack.user.username pack.id %}" title="Download pack">Download pack</a>
{% else %}
<a class="no-hover btn-primary display-inline-block w-100 text-center" href="{% url 'pack-download' pack.user.username pack.id %}{{ pack.friendly_filename }}" title="Login to download">Login to download</a>
<a class="no-hover btn-primary display-inline-block w-100 text-center" href="{% url 'pack-download' pack.user.username pack.id %}" title="Login to download">Login to download</a>
{% endif %}
</div>
{% endif %}
Expand Down
4 changes: 3 additions & 1 deletion utils/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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


Expand Down
2 changes: 1 addition & 1 deletion utils/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 534d66e

Please sign in to comment.