~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/moderation.py

  • Committer: Holger Rapp
  • Date: 2009-02-21 18:24:02 UTC
  • Revision ID: sirver@kallisto.local-20090221182402-k3tuf5c4gjwslbjf
Main Page contains now the same informations as before

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from django.db.models import signals
2
 
from threadedcomments.models import ThreadedComment, MARKUP_CHOICES
3
 
from threadedcomments.models import DEFAULT_MAX_COMMENT_LENGTH, DEFAULT_MAX_COMMENT_DEPTH
4
 
from comment_utils import moderation
5
 
 
6
 
MARKUP_CHOICES_IDS = [c[0] for c in MARKUP_CHOICES]
7
 
 
8
 
 
9
 
class CommentModerator(moderation.CommentModerator):
10
 
    max_comment_length = DEFAULT_MAX_COMMENT_LENGTH
11
 
    allowed_markup = MARKUP_CHOICES_IDS
12
 
    max_depth = DEFAULT_MAX_COMMENT_DEPTH
13
 
 
14
 
    def _is_past_max_depth(self, comment):
15
 
        i = 1
16
 
        c = comment.parent
17
 
        while c != None:
18
 
            c = c.parent
19
 
            i = i + 1
20
 
            if i > self.max_depth:
21
 
                return True
22
 
        return False
23
 
 
24
 
    def allow(self, comment, content_object):
25
 
        if self._is_past_max_depth(comment):
26
 
            return False
27
 
        if comment.markup not in self.allowed_markup:
28
 
            return False
29
 
        return super(CommentModerator, self).allow(comment, content_object)
30
 
 
31
 
    def moderate(self, comment, content_object):
32
 
        if len(comment.comment) > self.max_comment_length:
33
 
            return True
34
 
        return super(CommentModerator, self).moderate(comment, content_object)
35
 
 
36
 
 
37
 
class Moderator(moderation.Moderator):
38
 
 
39
 
    def connect(self):
40
 
        signals.pre_save.connect(self.pre_save_moderation, sender=ThreadedComment)
41
 
        signals.post_save.connect(self.post_save_moderation, sender=ThreadedComment)
42
 
 
43
 
    # THE FOLLOWING ARE HACKS UNTIL django-comment-utils GETS UPDATED SIGNALS
44
 
    # ####
45
 
    def pre_save_moderation(self, sender=None, instance=None, **kwargs):
46
 
        return super(Moderator, self).pre_save_moderation(sender, instance)
47
 
 
48
 
    def post_save_moderation(self, sender=None, instance=None, **kwargs):
49
 
        return super(Moderator, self).post_save_moderation(sender, instance)
50
 
 
51
 
 
52
 
# Instantiate the ``Moderator`` so that other modules can import and
53
 
# begin to register with it.
54
 
 
55
 
moderator = Moderator()