views:

200

answers:

4

Is there a way to make Django automatically set the is_public field of the comment as True.

I only allow comments for registered users and would like to skip manual review of comments posted.

A: 

overwrite the comments-form save-method, and set is_public to True

renton
I'm not sure where I would overwrite this method. I haven't done any customization on my comments setup.
Sævar
+1  A: 

The built in comments form should already set every comment to have is_public=True. See CommentDetailsForm.get_comment_create_data in http://code.djangoproject.com/browser/django/trunk/django/contrib/comments/forms.py

If you want to change this for logged in vs not logged in users, take a look at the built in comment moderation docs: http://docs.djangoproject.com/en/1.1/ref/contrib/comments/moderation/#ref-contrib-comments-moderation

You can write your own moderator which checks the comment to see if the comment.user is set and if it is do not moderate (is_public=True) otherwise set is_public=False.

Mark Lavin
OK, I'm looking at the django source, and I see that the default should be is_public=True. But in my setup, the comment always has is_public=False. Very strange
Sævar
A: 

OK if anyone is looking for the answer for this, this is how I solved it:

# in models.py:
import datetime
def moderate_comment(sender, instance, **kwargs):
    if not instance.id:
        instance.is_public = True
from django.contrib.comments.models import Comment
from django.db.models import signals

signals.pre_save.connect(moderate_comment, sender=Comment)
Sævar
A: 

Overriding the moderate of CommentModerator works for me:

from django.contrib.comments.moderation import CommentModerator

class EntryModerator(CommentModerator):
    # [...]

    def moderate(self, comment, content_object, request):
        # If the user who commented is a staff member, don't moderate
        if comment.user and comment.user.is_staff:
            return False
        else:
            return True
And then register the moderator via moderator.register I suppose?
Sævar