From a29d1fd891daba7e2e297b431680bc8d47c67d36 Mon Sep 17 00:00:00 2001 From: ffont Date: Wed, 8 Nov 2023 10:12:53 +0100 Subject: [PATCH] Also display sound comments in modal --- comments/views.py | 25 +++++++++++++++++ freesound/urls.py | 1 + templates/accounts/modal_comments.html | 37 ++++++++++++++++++++------ templates/sounds/display_sound.html | 2 +- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/comments/views.py b/comments/views.py index 52fdc8a94..10bebee19 100644 --- a/comments/views.py +++ b/comments/views.py @@ -106,3 +106,28 @@ def by_user(request, username): tvars.update(paginator) return render(request, 'accounts/modal_comments.html', tvars) + +@redirect_if_old_username_or_404 +@raise_404_if_user_is_deleted +def for_sound(request, username, sound_id): + if not request.GET.get('ajax'): + # If not loaded as a modal, redirect to account page with parameter to open modal + return HttpResponseRedirect(reverse('sound', args=[username, sound_id]) + '#comments') + + sound = get_object_or_404(Sound, id=sound_id) + if sound.user.username.lower() != username.lower(): + raise Http404 + + qs = Comment.objects.filter(sound=sound).select_related("user", "user__profile", + "sound__user", "sound__user__profile") + num_items_per_page = settings.SOUND_COMMENTS_PER_PAGE + paginator = paginate(request, qs, num_items_per_page) + tvars = { + "sound": sound, + "user": request.parameter_user, + "mode": "for_sound", + "delete_next_url": reverse('sound', args=[username, sound_id]) + f'?page={paginator["current_page"]}#comments' + } + tvars.update(paginator) + return render(request, 'accounts/modal_comments.html', tvars) + diff --git a/freesound/urls.py b/freesound/urls.py index 4cd318bd6..0633b0e41 100644 --- a/freesound/urls.py +++ b/freesound/urls.py @@ -61,6 +61,7 @@ path('people//sounds//geotag/', geotags.views.for_sound, name="sound-geotag"), path('people//sounds//similar/', sounds.views.similar, name="sound-similar"), path('people//sounds//downloaders/', sounds.views.downloaders, name="sound-downloaders"), + path('people//sounds//comments/', comments.views.for_sound, name="sound-comments"), path('people//packs/', sounds.views.packs_for_user, name="packs-for-user"), path('people//packs//', sounds.views.pack, name="pack"), path('people//packs//edit/', sounds.views.pack_edit, name="pack-edit"), diff --git a/templates/accounts/modal_comments.html b/templates/accounts/modal_comments.html index 51e8f789b..34c9f143e 100644 --- a/templates/accounts/modal_comments.html +++ b/templates/accounts/modal_comments.html @@ -7,16 +7,36 @@ {% load filter_img %} {% block id %}commentsModal{% endblock %} -{% block extra-class %}modal-width-80{% endblock %} -{% block aria-label %}{% if mode == "for_user" %}Comments on {{ user.username }}'s sounds{% else %}Sound comments by {{ user.username }}{% endif %}{% endblock %} +{% block extra-class %}modal-width-{% if mode == "for_sound" %}60{% else %}80{% endif %}{% endblock %} +{% block aria-label %}{% if mode == "for_user" %}Comments on {{ user.username }}'s sounds{% elif mode == "by_user" %}Sound comments by {{ user.username }}{% else %}Comments for sound {{ sound.original_filename }}{% endif %}{% endblock %} {% block body %}
-

{% if mode == "for_user" %}Comments on {{ user.username }}'s sounds{% else %}Sound comments by {{ user.username }}{% endif %} ({{paginator.count}})

+

{% if mode == "for_user" %}Comments on {{ user.username }}'s sounds{% elif mode == "by_user" %}Sound comments by {{ user.username }}{% else %}Comments for sound {{ sound.original_filename }}{% endif %} ({{paginator.count}})

{% if paginator.count > 0 %} + {% if mode == "for_sound" %} + {% for comment in page.object_list %} +
+
+ {% display_user_comment comment.user comment.created %} +
+ {% flag_user "SC" comment.user.username comment.id "" comment.user.profile.num_sounds %} + {% if perms.comments.delete_comment or sound.user == request.user or comment.user == request.user %} + {% bw_icon 'trash' %} Delete + {% endif %} +
+
+

{{ comment.comment|replace_img|safe|linebreaks|strip_unnecessary_br }}

+ {% if not forloop.last %} +
+ {% endif %} +
+ {% endfor %} + + {% else %} {% regroup page.object_list by sound as comments_per_sound %} {% for group in comments_per_sound %}
@@ -45,12 +65,13 @@

{% if mode == "for_user" %}Comments on

{% endif %} {% endfor %} -
- {% bw_paginator paginator page current_page request "comments" %} -
+ {% endif %} +
+ {% bw_paginator paginator page current_page request "comments" %} +
{% else %}
-
Looks like {% if mode == "for_user" %}there are no comments for {{ user.username }}'s sounds{% else %}{{ user.username }} hasn't made any comments{% endif %} yet... 😟
+
Looks like {% if mode == "for_user" %}there are no comments for {{ user.username }}'s sounds{% elif mode == "by_user" %}{{ user.username }} hasn't made any comments{% else %}there are no comments for this sound{% endif %} yet... 😟
{% endif %} diff --git a/templates/sounds/display_sound.html b/templates/sounds/display_sound.html index 6f0dc43e2..d9eb33807 100644 --- a/templates/sounds/display_sound.html +++ b/templates/sounds/display_sound.html @@ -124,7 +124,7 @@
- + {{ sound.num_comments|formatnumber }}