Skip to content

Commit

Permalink
Add permission to view/delete all comments by a user
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Sep 20, 2023
1 parent 9ff5b5b commit 82ec68d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
17 changes: 17 additions & 0 deletions judge/migrations/0196_view_all_user_comment_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.21 on 2023-09-20 13:41

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('judge', '0195_post_pin_global_permissions'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={'permissions': (('view_all_user_comment', 'View all comments by a user'),), 'verbose_name': 'comment', 'verbose_name_plural': 'comments'},
),
]
3 changes: 3 additions & 0 deletions judge/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Comment(MPTTModel):
revisions = models.IntegerField(verbose_name=_('revisions'), default=0)

class Meta:
permissions = (
('view_all_user_comment', _('View all comments by a user')),
)
verbose_name = _('comment')
verbose_name_plural = _('comments')

Expand Down
4 changes: 2 additions & 2 deletions judge/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def get_context_data(self, **kwargs):

@method_decorator(require_POST)
def delete_comments(self, request, *args, **kwargs):
if not request.user.is_superuser:
if not request.user.has_perm('judge.change_comment'):
raise PermissionDenied()

user_id = User.objects.get(username=kwargs['user']).id
Expand All @@ -320,7 +320,7 @@ def delete_comments(self, request, *args, **kwargs):
return HttpResponseRedirect(reverse('user_comment', args=(user.user.username,)))

def dispatch(self, request, *args, **kwargs):
if not self.request.user.is_superuser:
if not request.user.has_perm('judge.view_all_user_comment'):
raise PermissionDenied()
if request.method == 'POST':
return self.delete_comments(request, *args, **kwargs)
Expand Down
2 changes: 2 additions & 0 deletions templates/user/comment.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

{% block body %}
{% block before_comments %}{% endblock %}
{% if request.user.has_perm('judge.change_comment') %}
<form action="{{url('user_comment', user.username)}}" method="post">
{% csrf_token %}
<button type="submit">Delete all comments</button>
</form>
{% endif %}
<ul class="comments top-level-comments new-comments">
{% set logged_in = request.user.is_authenticated %}
{% for comment in comments %}
Expand Down
2 changes: 1 addition & 1 deletion templates/user/user-tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% if not request.official_contest_mode %}
{{ make_tab('blogs', 'fa-rss', url('user_blog', user.user.username), _('Blogs')) }}
{% endif %}
{% if request.user.is_superuser %}
{% if request.user.has_perm('judge.view_all_user_comment') %}
{{ make_tab('comments', 'fa-comments', url('user_comment', user.user.username), _('Comments')) }}
{% endif %}
{% if request.user.is_superuser and user.user != request.user and not user.user.is_superuser %}
Expand Down

0 comments on commit 82ec68d

Please sign in to comment.