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)
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)
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)