Skip to content

Commit

Permalink
uncategorized bookmarked sounds download
Browse files Browse the repository at this point in the history
  • Loading branch information
quimmrc committed Dec 11, 2024
1 parent 534d66e commit 31d2779
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
58 changes: 49 additions & 9 deletions bookmarks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.contrib.auth.models import User
from django.template.loader import render_to_string
from django.utils.text import slugify

from bookmarks.forms import BookmarkForm, BookmarkCategoryForm
from bookmarks.models import Bookmark, BookmarkCategory
from sounds.models import Sound
from sounds.models import Sound, License
from utils.downloads import download_sounds
from utils.pagination import paginate
from utils.username import redirect_if_old_username_or_404, raise_404_if_user_is_deleted
Expand Down Expand Up @@ -88,21 +91,58 @@ def delete_bookmark_category(request, category_id):
else:
return HttpResponseRedirect(reverse("bookmarks-for-user", args=[request.user.username]))


@login_required
@transaction.atomic()
def download_bookmark_category(request, category_id):
category = get_object_or_404(BookmarkCategory, id=category_id, user=request.user)
bookmarked_sounds = Bookmark.objects.filter(category_id=category.id).values("sound_id")
sounds_list = Sound.objects.filter(id__in=bookmarked_sounds, processing_state="OK", moderation_state="OK").select_related('user','license')
licenses_url = (reverse('category-licenses', args=[category_id]))
licenses_content = category.get_attribution(sound_qs=sounds_list)
if category_id==0:
licenses_content, sounds_list, download_filename = uncat_bookmark_data(request=request)
licenses_url = (reverse('category-licenses', args=[category_id]))
else:
category = get_object_or_404(BookmarkCategory, id=category_id, user=request.user)
bookmarked_sounds = Bookmark.objects.filter(category_id=category.id).values("sound_id")
sounds_list = Sound.objects.filter(id__in=bookmarked_sounds, processing_state="OK", moderation_state="OK").select_related('user','license')
licenses_url = (reverse('category-licenses', args=[category_id]))
licenses_content = category.get_attribution(sound_qs=sounds_list)
download_filename = category.download_filename
# NOTE: unlike pack downloads, here we are not doing any cache check to avoid consecutive downloads
return download_sounds(licenses_url, licenses_content, sounds_list, category.download_filename)
return download_sounds(licenses_url, licenses_content, sounds_list, download_filename)


def uncat_bookmark_data(request):
"""Get all the necessary information to download the uncategorized bookmarked sounds of a user.
These sounds do not correspond to a BookmarkCategory object so they need special handling.
Args:
request (django.core.handlers.wsgi.WSGIRequest): the server request. Used to extract user's information regarding its uncategorized bookmarked sounds.
Returns:
attribution (str): attributions for the different sounds
sounds_qs (django.db.models.query.QuerySet): list of uncategorized bookmarked sounds
download_filename (str): name of the zip file to be downloaded
"""
bookmarked_sounds = Bookmark.objects.filter(category=None, user=request.user).values("sound_id")
sound_qs = Sound.objects.filter(id__in=bookmarked_sounds, processing_state="OK", moderation_state="OK").select_related('user','license')
users = User.objects.filter(sounds__in=sound_qs).distinct()
# Generate text file with license info
licenses = License.objects.filter(sound__id__in=sound_qs).distinct()
attribution = render_to_string(("sounds/multiple_sounds_attribution.txt"),
dict(type="Bookmark Category",
users=users,
object=None,
licenses=licenses,
sound_list=sound_qs))
username_slug = slugify(request.user.username)
download_filename = "%d__%s__%s.zip" % (0, username_slug, "uncategorized")
return attribution, sound_qs, download_filename


def bookmark_category_licenses(request, category_id):
category = get_object_or_404(BookmarkCategory, id=category_id)
attribution = category.get_attribution()
if category_id==0:
attribution = uncat_bookmark_data(request)
else:
category = get_object_or_404(BookmarkCategory, id=category_id)
attribution = category.get_attribution()
return HttpResponse(attribution, content_type="text/plain")


Expand Down
3 changes: 2 additions & 1 deletion templates/bookmarks/bookmarks.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ <h4>Bookmark categories</h4>
<div class="v-spacing-top-3">
{% if bookmark_categories %}
<ul class="list-style-type-none">
<li><a href="{% url "bookmarks-for-user" user.username %}" {% if not category %}style="font-weight:bold"{% endif %} aria-label="Category: Uncategorized">Uncategorized</a> <span class="text-grey"> · {{n_uncat|bw_intcomma}} bookmark{{ n_uncat|pluralize }}</span></li>
<li><a href="{% url "bookmarks-for-user" user.username %}" {% if not category %}style="font-weight:bold"{% endif %} aria-label="Category: Uncategorized">Uncategorized</a> <span class="text-grey"> · {{n_uncat|bw_intcomma}} bookmark{{ n_uncat|pluralize }}</span>
<a class="cursor-pointer h-spacing-left-1" href="{% url 'download-bookmark-category' 0 %}" title="Download bookmark category" aria-label="Download bookmark category">{% bw_icon 'download' %}</a></li>
{% for cat in bookmark_categories %}
<li><a href="{% url "bookmarks-for-user-for-category" user.username cat.id %}" {% if category.id == cat.id %}style="font-weight:bold"{% endif %} aria-label="Category: {{cat.name}}">{{cat.name}}</a> <span class="text-grey"> · {{cat.num_bookmarks|bw_intcomma}} bookmark{{ cat.num_bookmarks|pluralize }}</span>
{% if is_owner %}
Expand Down
6 changes: 4 additions & 2 deletions templates/sounds/multiple_sounds_attribution.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{% load absurl %}{{type}} downloaded from Freesound
----------------------------------------
{# NOTE: an object can either be a pack of sounds or a bookmark category#}
"{{ object.name }}"
{%if object.name%}"{{ object.name }}"
{%else%}"Uncategorized bookmarks category"
{%endif%}

This {{ type }} of sounds contains sounds by the following user{{ users|pluralize }}:
{% for user in users %} - {{user.username}} ( {% absurl 'account' user.username %} ){% endfor %}

{% if type == "Pack" %}You can find this pack online at: {% absurl 'pack' object.user.username object.id %}{% endif %}

{%if object.description %}
{%if object and object.description %}
{{ type }} description
----------------

Expand Down

0 comments on commit 31d2779

Please sign in to comment.