Example #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)
Example #2
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)