Login

Content Moderator

Author:
mutazmq
Posted:
May 3, 2009
Language:
Python
Version:
1.0
Tags:
generic pinax
Score:
0 (after 0 ratings)

This snippet is for django-flag Pinax app to make it generic moderator for any content model. You don't need to modify neither your model nor your views to moderate your flagged content objects.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from django.contrib.sites.managers import CurrentSiteManager
from django.db.models import Manager
from django.db.models.manager import ManagerDescriptor
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q

from flag.models import FlaggedContent


class GenericManager(Manager):
    def get_query_set(self):
        f=~Q(id__in=getapprovedids(self.model))
        return super(GenericManager, self).get_query_set().filter(f)

class SiteManager(CurrentSiteManager):
    def get_query_set(self):
        f=~Q(id__in=getapprovedids(self.model))
        return super(SiteManager, self).get_query_set().filter(f)


def getapprovedids(model):
    ct=ContentType.objects.get_for_model(model)
    statusfilter= Q(status=4) | Q(status=5)
    ids=FlaggedContent.objects.filter(Q(content_type=ct), statusfilter).values_list('object_id')
    return map(lambda x: x[0],ids)
    
    

def register(model):
        for key in model.__dict__:
            if(type(model.__dict__[key])==ManagerDescriptor):
                if type(model.__dict__[key].manager)==Manager:
                    GenericManager().contribute_to_class(model,key)
                else:
                    SiteManager().contribute_to_class(model,key)  


"""

Usage Example:

Put the above code in your django-flag.__init__.py .

Wherever applicable, state the following code:

from flag import register
from myapp.models import mymodel
register(mymodel)


"""  

More like this

  1. Prevent Django newcomments spam with Akismet (reloaded) by sciyoshi 5 years, 9 months ago
  2. Unobtrusive comment moderation by ubernostrum 8 years, 1 month ago
  3. comments_allowed function for your model/... by mathijs 4 years, 8 months ago
  4. LinkSleeve comment moderation by zgoda 7 years ago
  5. Email on new comments by nikolaj 7 years, 8 months ago

Comments

Please login first before commenting.