Django Comments Moderation

November 22, 2009

Django comments allow you to set up moderation options.

models.py

In your models.py that you have comments for

from django.contrib.comments.moderation import CommentModerator, moderatorclass

BlogModerator(CommentModerator):
    email_notification = True
    enable_field = 'enable_comments'
    auto_moderate_field = 'publication_date'
    moderate_after = 14

moderator.register(Blog, BlogModerator)

This sets up emailing of comments to you when they are posted, and disallows any comments where the enable_comments field on the Blog is False. It also holds for moderation any comments that are more than 14 days after the publication_date on the Blog.

The only other thing you'll need is lots of templates.

comments/comment_notification_email.txt

A new comment has been posted for {{content_object.title}}.

http://www.site.co.uk{{content_object.get_absolute_url}}

Name: {{comment.user_name}}
Email: {{comment.user_email}}
Url: {{comment.user_url}}
Comment:
{{comment.comment}}

http://www.site.co.uk/comments/delete/{{comment.id}}/

{% if not comment.is_public %}
http://www.site.co.uk/comments/approve/{{comment.id}}/
{% endif %}

This provides a link to the thing that is being commented on, a link to delete the comment, and a link to approve it where it was held for moderation.

comments/delete.html

Two templates are required for delete. The first provides the confirmation window, whilst the second is shown once the delete has been done.

{% extends "blog/template.html" %}
{% load i18n %}

{% block title %}{% trans "Remove a comment" %}{% endblock %}

{% block content %}
<h1>{% trans "Really remove this comment?" %}</h1>
  <blockquote>{{ comment|linebreaks }}</blockquote>
  <form action="." method="post">{% csrf_token %}
    {% if next %}<input type="hidden" name="next" value="{{ next }}" id="next" />{% endif %}
    <p class="submit">
    <input type="submit" name="submit" value="{% trans "Remove" %}" /> or <a href="{{ comment.get_absolute_url }}">cancel</a>
    </p>
  </form>
{% endblock %}

comments/deleted.html

{% extends "blog/template.html" %}
{% load i18n %}

{% block title %}{% trans "Thanks for removing" %}.{% endblock %}

{% block content %}
<div class="post">
<h1>Comment Deleted</h1>
<div class="entry">
<p>{% trans "Thanks for taking the time to improve the quality of discussion on our site" %}.</p>
</div>
</div>
{% endblock %}

comments/approve.html

Two similar temples are required for the approval process.

{% extends "blog/template.html" %}
{% load i18n %}

{% block title %}{% trans "Approve a comment" %}{% endblock %}

{% block content %}
  <h1>{% trans "Really make this comment public?" %}</h1>
  <blockquote>{{ comment|linebreaks }}</blockquote>
  <form action="." method="post">{% csrf_token %}
    {% if next %}<input type="hidden" name="next" value="{{ next }}" id="next" />{% endif %}
    <p class="submit">
      <input type="submit" name="submit" value="{% trans "Approve" %}" /> or <a href="{{ comment.get_absolute_url }}">cancel</a>
    </p>
  </form>
{% endblock %}

comments/approved.html

{% extends "blog/template.html" %}
{% load i18n %}

{% block title %}{% trans "Thanks for approving" %}.{% endblock %}

{% block content %}
<div class="post">
<h1>Comment Approved</h1>
<div class="entry">
<p>{% trans "Thanks for taking the time to improve the quality of discussion on our site" %}.</p>
</div>
</div>
{% endblock %}

Tags: django comments