Skip to content

Commit

Permalink
Also display sound comments in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Nov 8, 2023
1 parent e6504bb commit a29d1fd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
25 changes: 25 additions & 0 deletions comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 change: 1 addition & 0 deletions freesound/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
path('people/<username>/sounds/<int:sound_id>/geotag/', geotags.views.for_sound, name="sound-geotag"),
path('people/<username>/sounds/<int:sound_id>/similar/', sounds.views.similar, name="sound-similar"),
path('people/<username>/sounds/<int:sound_id>/downloaders/', sounds.views.downloaders, name="sound-downloaders"),
path('people/<username>/sounds/<int:sound_id>/comments/', comments.views.for_sound, name="sound-comments"),
path('people/<username>/packs/', sounds.views.packs_for_user, name="packs-for-user"),
path('people/<username>/packs/<int:pack_id>/', sounds.views.pack, name="pack"),
path('people/<username>/packs/<int:pack_id>/edit/', sounds.views.pack_edit, name="pack-edit"),
Expand Down
37 changes: 29 additions & 8 deletions templates/accounts/modal_comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<div class="col-12">
<div class="text-center">
<h4 class="v-spacing-5">{% if mode == "for_user" %}Comments on <a href="{% url "account" user.username %}">{{ user.username }}</a>'s sounds{% else %}Sound comments by <a href="{% url "account" user.username %}">{{ user.username }}</a>{% endif %} ({{paginator.count}})</h4>
<h4 class="v-spacing-5">{% if mode == "for_user" %}Comments on <a href="{% url "account" user.username %}">{{ user.username }}</a>'s sounds{% elif mode == "by_user" %}Sound comments by <a href="{% url "account" user.username %}">{{ user.username }}</a>{% else %}Comments for sound <a href="{% url "sound" sound.user.username sound.id %}">{{ sound.original_filename }}</a>{% endif %} ({{paginator.count}})</h4>
</div>
<div class="v-spacing-4">
{% if paginator.count > 0 %}
{% if mode == "for_sound" %}
{% for comment in page.object_list %}
<div>
<div style="display: flex; justify-content: space-between;">
{% display_user_comment comment.user comment.created %}
<div class="text-right v-spacing-top-1">
{% 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 %}
<a class="cursor-pointer" data-toggle="confirmation-modal" data-modal-confirmation-title="Are you sure you want to delete this comment?" data-modal-confirmation-url="{% url 'comment-delete' comment.id %}?next={{ delete_next_url }}" title="Delete comment">{% bw_icon 'trash' %} Delete</a>
{% endif %}
</div>
</div>
<p class="v-spacing-top-1">{{ comment.comment|replace_img|safe|linebreaks|strip_unnecessary_br }}</p>
{% if not forloop.last %}
<div class="divider-light v-spacing-top-2 v-spacing-2"></div>
{% endif %}
</div>
{% endfor %}

{% else %}
{% regroup page.object_list by sound as comments_per_sound %}
{% for group in comments_per_sound %}
<div class="row">
Expand All @@ -29,7 +49,7 @@ <h4 class="v-spacing-5">{% if mode == "for_user" %}Comments on <a href="{% url "
<div class="text-right v-spacing-top-1">
{% 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 %}
<a class="cursor-pointer" data-toggle="confirmation-modal" data-modal-confirmation-title="Are you sure you want to delete this comment?" data-modal-confirmation-url="{% url 'comment-delete' comment.id %}?next={{ delete_next_url }}" title="Delete comment">{% bw_icon 'trash' %} Delete comment</a>
<a class="cursor-pointer" data-toggle="confirmation-modal" data-modal-confirmation-title="Are you sure you want to delete this comment?" data-modal-confirmation-url="{% url 'comment-delete' comment.id %}?next={{ delete_next_url }}" title="Delete comment">{% bw_icon 'trash' %} Delete</a>
{% endif %}
</div>
</div>
Expand All @@ -45,12 +65,13 @@ <h4 class="v-spacing-5">{% if mode == "for_user" %}Comments on <a href="{% url "
<div class="divider-light v-spacing-3"></div>
{% endif %}
{% endfor %}
<div class="v-spacing-top-5">
{% bw_paginator paginator page current_page request "comments" %}
</div>
{% endif %}
<div class="v-spacing-top-5">
{% bw_paginator paginator page current_page request "comments" %}
</div>
{% else %}
<div class="text-center">
<div class="text-grey v-spacing-top-1">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... &#128543</div>
<div class="text-grey v-spacing-top-1">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... &#128543</div>
</div>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/sounds/display_sound.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ <h5 class="v-spacing-1"><a class="bw-link--black" href="{% url 'sound' sound.use
</a>
</div>
<div class="h-spacing-left-2" title="{{ sound.num_comments }} comments">
<a href="{% url 'sound' sound.username sound.id %}#comments" class="bw-link--grey-light">
<a href="javascript:void(0)" data-toggle="modal-default" data-modal-content-url="{% url 'sound-comments' sound.username sound.id %}?ajax=1" class="bw-link--grey-light">
<span class="bw-icon-comments"></span> {{ sound.num_comments|formatnumber }}
</a>
</div>
Expand Down

0 comments on commit a29d1fd

Please sign in to comment.