Login

Content Moderator

Author:
mutazmq
Posted:
May 3, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.