Exemple #1
0
def save_handler(sender, instance, **kwargs):
    """
    The following things are done after creating an object in moderated class:
    1. Creates monitor entries for object and its parents.
    2. Auto-approves object, its parents & specified related objects if user
       has ``moderate`` permission. Otherwise, they are put in pending.
    """
    import django_monitor
    from django_monitor.models import MonitorEntry
    from django.contrib.contenttypes.models import ContentType

    # Auto-moderation
    user = get_current_user()
    opts = instance.__class__._meta
    mod_perm = '%s.moderate_%s' % (
        opts.app_label.lower(), opts.object_name.lower()
    )
    if user and user.has_perm(mod_perm):
        status = APPROVED_STATUS
    else:
        status = PENDING_STATUS

    # Create corresponding monitor entry
    if kwargs.get('created', None):
        me = MonitorEntry.objects.create(
            status = status,
            content_object = instance,
            timestamp = datetime.now()
        )
        me.moderate(status, user)

        # Create one monitor_entry per moderated parent.
        monitored_parents = filter(
            lambda x: django_monitor.model_from_queue(x),
            instance._meta.parents.keys()
        )
        for parent in monitored_parents:
            parent_ct = ContentType.objects.get_for_model(parent)
            parent_pk_field = instance._meta.get_ancestor_link(parent)
            parent_pk = getattr(instance, parent_pk_field.attname)
            try:
                me = MonitorEntry.objects.get(
                    content_type = parent_ct, object_id = parent_pk
                )
            except MonitorEntry.DoesNotExist:
                me = MonitorEntry(
                    content_type = parent_ct, object_id = parent_pk,
                )
            me.moderate(status, user)

        # Moderate related objects too...
        model = django_monitor.model_from_queue(instance.__class__)
        if model:
            for rel_name in model['rel_fields']:
                rel_obj = getattr(instance, rel_name, None)
                if rel_obj:
                    moderate_rel_objects(rel_obj, status, user)
Exemple #2
0
def save_handler(sender, instance, **kwargs):
    """
    The following things are done after creating an object in moderated class:
    1. Creates monitor entries for object and its parents.
    2. Auto-approves object, its parents & specified related objects if user
       has ``moderate`` permission. Otherwise, they are put in pending.
    """
    import django_monitor
    from django_monitor.models import MonitorEntry
    from django.contrib.contenttypes.models import ContentType

    # Auto-moderation
    user = get_current_user()
    opts = instance.__class__._meta
    mod_perm = '%s.moderate_%s' % (opts.app_label.lower(),
                                   opts.object_name.lower())
    if user and user.has_perm(mod_perm):
        status = APPROVED_STATUS
    else:
        status = PENDING_STATUS

    # Create corresponding monitor entry
    if kwargs.get('created', None):
        me = MonitorEntry.objects.create(status=status,
                                         content_object=instance,
                                         timestamp=datetime.now())
        me.moderate(status, user)

        # Create one monitor_entry per moderated parent.
        monitored_parents = filter(
            lambda x: django_monitor.model_from_queue(x),
            instance._meta.parents.keys())
        for parent in monitored_parents:
            parent_ct = ContentType.objects.get_for_model(parent)
            parent_pk_field = instance._meta.get_ancestor_link(parent)
            parent_pk = getattr(instance, parent_pk_field.attname)
            try:
                me = MonitorEntry.objects.get(content_type=parent_ct,
                                              object_id=parent_pk)
            except MonitorEntry.DoesNotExist:
                me = MonitorEntry(
                    content_type=parent_ct,
                    object_id=parent_pk,
                )
            me.moderate(status, user)

        # Moderate related objects too...
        model = django_monitor.model_from_queue(instance.__class__)
        if model:
            for rel_name in model['rel_fields']:
                rel_obj = getattr(instance, rel_name, None)
                if rel_obj:
                    moderate_rel_objects(rel_obj, status, user)
Exemple #3
0
 def moderate_parents(self, obj, user, status):
     """Create one monitor_entry per moderated parent"""
     monitored_parents = filter(lambda x: model_from_queue(x),
                                obj._meta.parents.keys())
     for parent in monitored_parents:
         parent_ct = ContentType.objects.get_for_model(parent)
         parent_pk_field = obj._meta.get_ancestor_link(parent)
         parent_pk = getattr(obj, parent_pk_field.attname)
         try:
             me = MonitorEntry.objects.get(content_type=parent_ct,
                                           object_id=parent_pk)
         except MonitorEntry.DoesNotExist:
             me = MonitorEntry(
                 content_type=parent_ct,
                 object_id=parent_pk,
             )
         me.moderate(status, user)
Exemple #4
0
 def moderate_parents(self, obj, user, status):
     """Create one monitor_entry per moderated parent"""
     monitored_parents = filter(
         lambda x: model_from_queue(x),
         obj._meta.parents.keys()
     )
     for parent in monitored_parents:
         parent_ct = ContentType.objects.get_for_model(parent)
         parent_pk_field = obj._meta.get_ancestor_link(parent)
         parent_pk = getattr(obj, parent_pk_field.attname)
         try:
             me = MonitorEntry.objects.get(
                 content_type = parent_ct, object_id = parent_pk
             )
         except MonitorEntry.DoesNotExist:
             me = MonitorEntry(
                 content_type = parent_ct, object_id = parent_pk,
             )
         me.moderate(status, user)
def nq(
    model, rel_fields = [], import_unmoderated=False, can_delete_approved = True, long_desc=None,
    manager_name = 'objects', status_name = 'status',
    monitor_name = 'monitor_entry', base_manager = None, notify_moderators=None
):
    """ Register(enqueue) the model for moderation."""
    if not model_from_queue(model):
        signals.post_save.connect(save_handler, sender = model)
        signals.pre_delete.connect(delete_handler, sender = model)
        registered_model = get_model(
            model._meta.app_label, model._meta.object_name, only_installed=False
        )
        add_fields(
            registered_model, manager_name, status_name,
            monitor_name, base_manager
        )
        _queue[model] = {
            'rel_fields': rel_fields,
            'can_delete_approved': can_delete_approved,
            'manager_name': manager_name,
            'status_name': status_name,
            'monitor_name': monitor_name,
            'long_desc': long_desc or _default_long_desc,
            'notify_moderators': notify_moderators,
        }
        if import_unmoderated:
            try:
                mod_obj_ids = model.objects.all().values_list('pk', flat=True)
                unmod_objs = model._base_manager.exclude(pk__in=mod_obj_ids)
                print 'importing %s unmoderated objects...' % unmod_objs.count()
                for obj in unmod_objs:
                    me = MonitorEntry(
                        status=PENDING_STATUS,
                        content_object=obj,
                        status_date=datetime.datetime.now())
                    me.save()
            except:
                pass
Exemple #6
0
def save_handler(sender, instance, **kwargs):
    """
    The following things are done after creating an object in moderated class:
    1. Creates monitor entries for object and its parents.
    2. Auto-approves object, its parents & specified related objects if user 
       has ``moderate`` permission. Otherwise, they are put in pending.
    """
    import django_monitor
    # Auto-moderation
    user = get_current_user()
    opts = instance.__class__._meta
    mod_perm = '%s.moderate_%s' % (
        opts.app_label.lower(), opts.object_name.lower()
    )
    if user and user.has_perm(mod_perm):
        status = APPROVED_STATUS
    else:
        status = PENDING_STATUS

    # Create corresponding monitor entry
    if kwargs.get('created', None):
        from django_monitor import _queue
        
        me = MonitorEntry.objects.create(
            status = status,
            content_object = instance,
            timestamp = datetime.now()
        )
        me.moderate(status, user)

        # Create one monitor_entry per moderated parent.
        monitored_parents = filter(
            lambda x: django_monitor.model_from_queue(x),
            instance._meta.parents.keys()
        )
        for parent in monitored_parents:
            parent_ct = ContentType.objects.get_for_model(parent)
            parent_pk_field = instance._meta.get_ancestor_link(parent)
            parent_pk = getattr(instance, parent_pk_field.attname)
            try:
                me = MonitorEntry.objects.get(
                    content_type = parent_ct, object_id = parent_pk
                )
            except MonitorEntry.DoesNotExist:
                me = MonitorEntry(
                    content_type = parent_ct, object_id = parent_pk,
                )
            me.moderate(status, user)

        # Moderate related objects too... 
        model = django_monitor.model_from_queue(instance.__class__)
        if model:
            for rel_name in model['rel_fields']:
                rel_obj = getattr(instance, rel_name, None)
                if rel_obj:
                    moderate_rel_objects(rel_obj, status, user)
                    
        if callable(_queue[instance.__class__]['notify_moderators']):
            if not _queue[instance.__class__]['notify_moderators'](instance):
                return

        if MODERATOR_LIST:

            from django.contrib.sites.models import Site
            domain = Site.objects.get(id=settings.SITE_ID).domain

            status = me.get_status_display()
            instance_class = instance.__class__.__name__
            app_label = instance._meta.app_label
            long_desc = _queue[instance.__class__]['long_desc']

            # message
            message = _long_desc(instance, long_desc)
            if status == 'Pending':
                message += "\n\nTo moderate, go to http://%s%s?ot=desc&status=IP&o=2" % (domain, reverse('admin:%s_%s_changelist' % (app_label, instance_class.lower())))

            # subject
            key = "%s:%s" % (instance_class, status)
#             if me.moderation_status_by and me.moderation_status_by.username == 'gatekeeper_automod':
#                 key = "%s:auto" % key
            subject = "[%s] New monitor object on %s" % (key, domain)

            # sender
            from_addr = settings.DEFAULT_FROM_EMAIL

            send_mail(subject, message, from_addr, MODERATOR_LIST, fail_silently=True)