Skip to content

Commit

Permalink
Reduce num queries bookmarks page
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Dec 10, 2024
1 parent d587ecc commit eff0984
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 1 addition & 3 deletions accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@
path('bookmarks/category/<int:category_id>/delete/', bookmarks.delete_bookmark_category, name="delete-bookmark-category"),
path('bookmarks/<int:bookmark_id>/delete/', bookmarks.delete_bookmark, name="delete-bookmark"),
path('bookmarks/category/<int:category_id>/edit_modal/', bookmarks.edit_bookmark_category, name="edit-bookmark-category"),
re_path(r'^bookmarks/(?P<category_id>\d+)/download/.*$', bookmarks.download_bookmark_category, name="download-bookmark-category"),
path('bookmarks/<int:category_id>/download/', bookmarks.download_bookmark_category, name="download-bookmark-category"),
path('bookmarks/<int:category_id>/licenses/', bookmarks.bookmark_category_licenses, name="category-licenses"),



path('messages/', messages.inbox, name='messages'),
path('messages/sent/', messages.sent_messages, name='messages-sent'),
path('messages/archived/', messages.archived_messages, name='messages-archived'),
Expand Down
17 changes: 10 additions & 7 deletions bookmarks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ def bookmarks(request, category_id=None):
n_uncat = Bookmark.objects.select_related("sound").filter(user=user, category=None).count()
if not category_id:
category = None
bookmarked_sounds = Bookmark.objects.select_related("sound", "sound__user").filter(user=user, category=None)
bookmarked_sounds = Bookmark.objects.filter(user=user, category=None)
else:
category = get_object_or_404(BookmarkCategory, id=category_id, user=user)
bookmarked_sounds = category.bookmarks.select_related("sound", "sound__user").all()
bookmarked_sounds = category.bookmarks.all()
bookmark_categories = BookmarkCategory.objects.filter(user=user).annotate(num_bookmarks=Count('bookmarks'))
tvars = {'user': user,
'is_owner': is_owner,
'n_uncat': n_uncat,
'category': category,
'bookmark_categories': bookmark_categories}
tvars.update(paginate(request, bookmarked_sounds, settings.BOOKMARKS_PER_PAGE))

paginator = paginate(request, bookmarked_sounds, settings.BOOKMARKS_PER_PAGE)
page_sounds = Sound.objects.ordered_ids([bookmark.sound_id for bookmark in paginator['page'].object_list])
tvars.update(paginator)
tvars['page_bookmarks_and_sound_objects'] = zip(paginator['page'].object_list, page_sounds)
return render(request, 'bookmarks/bookmarks.html', tvars)


@redirect_if_old_username_or_404
@raise_404_if_user_is_deleted
def bookmarks_for_user(request, username, category_id=None):
Expand Down Expand Up @@ -88,21 +91,21 @@ def delete_bookmark_category(request, category_id):
@login_required
@transaction.atomic()
def download_bookmark_category(request, category_id):
category = get_object_or_404(BookmarkCategory, id=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)
# 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)


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


@login_required
@transaction.atomic()
def edit_bookmark_category(request, category_id):
Expand Down
6 changes: 3 additions & 3 deletions templates/bookmarks/bookmarks.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ <h4>Bookmark categories</h4>
<div class="col-lg-8">
<h4><span class="text-light-grey">Category:</span> {% if category %}{{category.name}}{% else %}Uncategorized{% endif %} </h4>
<div class="v-spacing-top-3">
{% if page.object_list %}
{% if page_bookmarks_and_sound_objects %}
<div class="row">
{% for bookmark in page.object_list %}
{% for bookmark, sound in page_bookmarks_and_sound_objects %}
<div class="col-6 col-md-4">
{% display_sound_small bookmark.sound %}
{% display_sound_small sound %}
{% if is_owner %}
<div class="right v-spacing-4 v-spacing-top-negative-2"><a class="cursor-pointer bw-link--grey" data-toggle="confirmation-modal" data-modal-confirmation-title="Are you sure you want to remove this bookmark from '{{bookmark.category_name_or_uncategorized}}'?" data-modal-confirmation-url="{% url "delete-bookmark" bookmark.id %}?next={{request.path}}&page={{current_page}}" title="Remove from bookmarks" aria-label="Remove from bookmarks">{% bw_icon 'trash' %} Remove bookmark</a></div>
{% endif %}
Expand Down

0 comments on commit eff0984

Please sign in to comment.