~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to threadedcomments/moderation.py

  • Committer: franku
  • Date: 2016-05-15 14:41:54 UTC
  • mto: This revision was merged to the branch mainline in revision 409.
  • Revision ID: somal@arcor.de-20160515144154-00m3tiibyxm0nw2w
added the old threadedcomments app as wildelands app

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, FreeThreadedComment, 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
class Moderator(moderation.Moderator):
 
37
    def connect(self):
 
38
        for model in (ThreadedComment, FreeThreadedComment):
 
39
            signals.pre_save.connect(self.pre_save_moderation, sender=model)
 
40
            signals.post_save.connect(self.post_save_moderation, sender=model)
 
41
    
 
42
    ## THE FOLLOWING ARE HACKS UNTIL django-comment-utils GETS UPDATED SIGNALS ####
 
43
    def pre_save_moderation(self, sender=None, instance=None, **kwargs):
 
44
        return super(Moderator, self).pre_save_moderation(sender, instance)
 
45
 
 
46
    def post_save_moderation(self, sender=None, instance=None, **kwargs):
 
47
        return super(Moderator, self).post_save_moderation(sender, instance)
 
48
 
 
49
 
 
50
# Instantiate the ``Moderator`` so that other modules can import and 
 
51
# begin to register with it.
 
52
 
 
53
moderator = Moderator()
 
 
b'\\ No newline at end of file'