Add moderation tool to Trombi

This commit is contained in:
Skia
2017-05-12 18:30:06 +02:00
parent adeda41b52
commit 231cb236dc
9 changed files with 234 additions and 46 deletions

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('trombi', '0002_trombi_show_profiles'),
]
operations = [
migrations.AddField(
model_name='trombicomment',
name='is_moderated',
field=models.BooleanField(default=False, verbose_name='is the comment moderated'),
),
]

View File

@ -106,6 +106,7 @@ class TrombiComment(models.Model):
author = models.ForeignKey(TrombiUser, verbose_name=_("author"), related_name='given_comments')
target = models.ForeignKey(TrombiUser, verbose_name=_("target"), related_name='received_comments')
content = models.TextField(_("content"), default="")
is_moderated = models.BooleanField(_("is the comment moderated"), default=False)
def can_be_viewed_by(self, user):
if user.id == self.target.user.id:

View File

@ -0,0 +1,34 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Moderate Trombi comments{% endtrans %}
{% endblock %}
{% block content %}
<h3>{% trans %}Moderate Trombi comments{% endtrans %}</h3>
<h4>{{ trombi }}</h4>
<a href="{{ url('trombi:detail', trombi_id=object.id) }}">{% trans %}Back{% endtrans %}</a>
<hr>
<dl>
{% for c in comments %}
<dt>{% trans author=c.author.user.get_display_name(),
target=c.target.user.get_display_name() %}Author: {{ author }} - Target: {{ target }}{% endtrans %}</dt>
<dd>
<p>
{{ c.content }}
</p>
<form action="{{ url('trombi:moderate_comment', comment_id=c.id )}}" method="post">
{% csrf_token %}
<input type="hidden" name="action" id="action" value="accept" />
<p><input type="submit" value="{% trans %}Accept{% endtrans %}" /></p>
</form>
<form action="{{ url('trombi:moderate_comment', comment_id=c.id )}}" method="post">
{% csrf_token %}
<input type="hidden" name="action" id="action" value="reject" />
<p><input type="submit" value="{% trans %}Reject{% endtrans %}" /></p>
</form>
</dd>
{% endfor %}
</dl>
{% endblock %}

View File

@ -7,6 +7,7 @@
{% block content %}
<h2>{% trans club=object.club %}{{ club }}'s Trombi{% endtrans %}</h2>
<a href="{{ url('trombi:edit', trombi_id=object.id) }}">{% trans %}Edit{% endtrans %}</a>
<a href="{{ url('trombi:moderate_comments', trombi_id=object.id) }}">{% trans %}Moderate comments{% endtrans %}</a>
<p>{% trans %}Subscription deadline: {% endtrans %}{{ object.subscription_deadline|date(DATETIME_FORMAT) }}</p>
<p>{% trans %}Comment deadline: {% endtrans %}{{ object.comments_deadline|date(DATETIME_FORMAT) }}</p>
<a href="#">Export</a>

View File

@ -25,9 +25,9 @@
<img src="{{ scrub_file }}" alt="" style="max-width: 200px">
</div>
<dl>
{% for c in trombi_user.received_comments.all() %}
{% for c in trombi_user.received_comments.filter(is_moderated=True) %}
<dt style="font-weight: bold; font-size: 110%">{{ c.author.user.get_display_name() }}</dt>
<dd>{{ c.content}}</dd>
<dd>{{ c.content }}</dd>
{% endfor %}
</dl>
</div>

View File

@ -29,6 +29,8 @@ from trombi.views import *
urlpatterns = [
url(r'^(?P<club_id>[0-9]+)/new$', TrombiCreateView.as_view(), name='create'),
url(r'^(?P<trombi_id>[0-9]+)/edit$', TrombiEditView.as_view(), name='edit'),
url(r'^(?P<trombi_id>[0-9]+)/moderate_comments$', TrombiModerateCommentsView.as_view(), name='moderate_comments'),
url(r'^(?P<comment_id>[0-9]+)/moderate$', TrombiModerateCommentView.as_view(), name='moderate_comment'),
url(r'^(?P<trombi_id>[0-9]+)/delete/(?P<user_id>[0-9]+)$', TrombiDeleteUserView.as_view(), name='delete_user'),
url(r'^(?P<trombi_id>[0-9]+)$', TrombiDetailView.as_view(), name='detail'),
url(r'^(?P<user_id>[0-9]+)/new_comment$', TrombiCommentCreateView.as_view(), name='new_comment'),

View File

@ -29,6 +29,7 @@ from django.views.generic import ListView, DetailView, RedirectView, TemplateVie
from django.views.generic.edit import UpdateView, CreateView, DeleteView, FormView, SingleObjectMixin
from django.utils.translation import ugettext_lazy as _
from django import forms
from django.conf import settings
from django.forms.models import modelform_factory
from datetime import date
@ -95,6 +96,63 @@ class TrombiDeleteUserView(CanEditPropMixin, SingleObjectMixin, RedirectView):
# See if we need to also delete the comments on the user, or if we keep them
return redirect(self.object.get_absolute_url()+"?qn_success")
class TrombiModerateCommentsView(CanEditPropMixin, QuickNotifMixin, DetailView):
model = Trombi
template_name = 'trombi/comment_moderation.jinja'
pk_url_kwarg = 'trombi_id'
def get_context_data(self, **kwargs):
kwargs = super(TrombiModerateCommentsView, self).get_context_data(**kwargs)
kwargs['comments'] = TrombiComment.objects.filter(is_moderated=False,
author__trombi__id=self.object.id).exclude(target__user__id=self.request.user.id)
return kwargs
class TrombiModerateForm(forms.Form):
reason = forms.CharField(help_text=_("Explain why you rejected the comment"))
action = forms.CharField(initial="delete", widget=forms.widgets.HiddenInput)
class TrombiModerateCommentView(DetailView):
model = TrombiComment
template_name = 'core/edit.jinja'
pk_url_kwarg = 'comment_id'
def dispatch(self, request, *args, **kwargs):
self.object = self.get_object()
if not request.user.is_owner(self.object.author.trombi):
raise Http404()
return super(TrombiModerateCommentView, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
if "action" in request.POST:
if request.POST['action'] == "accept":
self.object.is_moderated = True
self.object.save()
return redirect(reverse('trombi:moderate_comments', kwargs={'trombi_id': self.object.author.trombi.id})+"?qn_success")
elif request.POST['action'] == "reject":
return super(TrombiModerateCommentView, self).get(request, *args, **kwargs)
elif request.POST['action'] == "delete" and "reason" in request.POST.keys():
self.object.author.user.email_user(
subject="[%s] %s" % (settings.SITH_NAME, _("Rejected comment")),
message=_("Your comment to %(target)s on the Trombi \"%(trombi)s\" was rejected for the following "
"reason: %(reason)s\n\n"
"Your comment was:\n\n%(content)s"
) % {
'target': self.object.target.user.get_display_name(),
'trombi': self.object.author.trombi,
'reason': request.POST["reason"],
'content': self.object.content,
},
)
self.object.delete()
return redirect(reverse('trombi:moderate_comments', kwargs={'trombi_id': self.object.author.trombi.id})+"?qn_success")
raise Http404
def get_context_data(self, **kwargs):
kwargs = super(TrombiModerateCommentView, self).get_context_data(**kwargs)
kwargs['form'] = TrombiModerateForm()
return kwargs
# User side
class TrombiModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):