Exemple #1
0
def _log_create(instance, created):
    if created:
        changes = model_instance_diff(None, instance)

        log_entry = LogEntry.objects.log_create(instance,
                                                action=LogEntry.Action.CREATE,
                                                changes=json.dumps(changes))
Exemple #2
0
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    from auditlog.registry import auditlog

    if instance.pk is not None and auditlog.can_update:
        try:
            old = sender.objects.get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                log_entry = LogEntry.objects.log_create(
                    instance,
                    action=LogEntry.Action.UPDATE,
                    changes=json.dumps(changes),
                )
Exemple #3
0
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        try:
            old = sender.objects.get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                log_entry = LogEntry.log_create(
                    instance,
                    action=LogEntry.Action.UPDATE,
                    changes=changes,
                )
                transaction.on_commit(lambda: log_entry.save())
                return log_entry
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        try:
            old = sender.objects.get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            try:
                actor = get_current_user()
            except:
                actor = None

            if isinstance(actor, AnonymousUser):
                actor = None

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                log_entry = LogEntry.objects.log_create(
                    instance,
                    action=LogEntry.Action.UPDATE,
                    changes=changes,
                    actor=actor)
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        try:
            # Support multiple database while retriveing old data
            db = instance._state.db
            if db is None or db == '':
                old = sender.objects.get(pk=instance.pk)
            else:
                old = sender.objects.using(db).get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                log_entry = LogEntry.objects.log_create(
                    instance,
                    action=LogEntry.Action.UPDATE,
                    changes=json.dumps(changes),
                )
def log_create(sender, instance, created, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is first saved to the database.

    Direct use is discouraged, connect your model through auditlog.registry.registry instead.
    """
    if created:
        changes = model_instance_diff(None, instance)

        log_entry = LogEntry.objects.log_create(instance, action=LogEntry.Action.CREATE, changes=json.dumps(changes))
def log_delete(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is deleted from the database.

    Direct use is discouraged, connect your model through auditlog.registry.registry instead.
    """
    if instance.pk is not None:
        changes = model_instance_diff(instance, None)

        log_entry = LogEntry.objects.log_create(instance, action=LogEntry.Action.DELETE, changes=json.dumps(changes))
Exemple #8
0
    def log_delete(self, instance, **kwargs):
        """Handle model deletion logging"""
        if instance.pk is not None:
            changes = model_instance_diff(instance, None)

            self.create_log_entry(
                instance=instance,
                action=LogEntry.Action.DELETE,
                changes=changes,
                **kwargs)
Exemple #9
0
    def log_create(self, instance, created, **kwargs):
        """Handle model creation logging"""
        if created:
            changes = model_instance_diff(None, instance)

            self.create_log_entry(
                instance=instance,
                action=LogEntry.Action.CREATE,
                changes=changes,
                **kwargs)
def log_create(sender, instance, created, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is first saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if created:
        changes = model_instance_diff(None, instance)

        logging.info({ "LogType": "AuditLog", "Class": str(instance.__class__.__name__),
                       "InstanceID": int(instance.id), "Action": "Create",
                       "Changes": json.dumps(changes)}
                     )
def log_delete(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is deleted from the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        changes = model_instance_diff(instance, None)

        logging.info({ 'LogType': 'AuditLog', 'Class': str(instance.__class__.__name__),
                       'InstanceID': int(instance.id), 'Action': 'Delete',
                       'Changes': changes}
                     )
def log_delete(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is deleted from the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        changes = model_instance_diff(instance, None)

        log_entry = LogEntry.objects.log_create(
            instance,
            action=LogEntry.Action.DELETE,
            changes=json.dumps(changes),
        )
def log_create(sender, instance, created, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is first saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if created:
        changes = model_instance_diff(None, instance)

        log_entry = LogEntry.objects.log_create(
            instance,
            action=LogEntry.Action.CREATE,
            changes=json.dumps(changes),
        )
Exemple #14
0
def log_delete(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is deleted from the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        changes = model_instance_diff(instance, None)
        log_entry = LogEntry.log_create(
            instance,
            action=LogEntry.Action.DELETE,
            changes=changes,
        )
        transaction.on_commit(lambda: log_entry.save())
        return log_entry
Exemple #15
0
def log_create(sender, instance, created, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is first saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if created:
        changes = model_instance_diff(None, instance)
        log_entry = LogEntry.log_create(
            instance,
            action=LogEntry.Action.CREATE,
            changes=changes,
        )
        transaction.on_commit(lambda: log_entry.save())
        return log_entry
Exemple #16
0
    def log_update(self, instance, **kwargs):
        """Handle model update logging"""
        if instance.pk is not None:
            try:
                old = instance.__class__.objects.get(pk=instance.pk)
            except instance.__class__.DoesNotExist:
                pass
            else:
                new = instance

                changes = model_instance_diff(old, new)

                # Log an entry only if there are changes
                if changes:
                    self.create_log_entry(
                        instance=instance,
                        action=LogEntry.Action.UPDATE,
                        changes=changes,
                        **kwargs)
Exemple #17
0
def log_event(request, start, end, action: int, extra=None):
    if start == None and end == None:
        raise ValueError('start and end cannot both be none!')

    object_key = start.id if start != None else end.id
    instance = start if start != None else end

    # Determine if instance has created/last updated
    # We don't want to log these fields

    exclude = []
    try:
        instance._meta.get_field('created')
        exclude.append('created')
    except:
        pass
    try:
        instance._meta.get_field('last_updated')
        exclude.append('last_updated')
    except:
        pass

    # Register the type to allow logging
    auditlog.register(type(instance), exclude_fields=exclude)

    log = LogEntry.objects.log_create(
        instance,
        object_pk=object_key,
        object_id=object_key,
        object_repr=str(start) if start != None else str(end),
        action=action,
        changes=model_instance_diff(start, end),
        actor_id=request.user.id,
        timestamp=timezone.now(),
        content_type_id=
        6,  # This is the id for auditlog listed in django_content_types table
        remote_addr=get_client_ip(request),
        additional_data=extra)

    # Unregister the type to disable auto (duplicate) logging
    auditlog.unregister(type(instance))
Exemple #18
0
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through auditlog.registry.registry instead.
    """
    if instance.pk is not None:
        try:
            old = sender.objects.get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                log_entry = LogEntry.objects.log_create(
                    instance, action=LogEntry.Action.UPDATE, changes=json.dumps(changes)
                )
def log_create(sender, instance, created, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is first saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if created:
        try:
            actor = get_current_user()
        except:
            actor = None

        if isinstance(actor, AnonymousUser):
            actor = None

        changes = model_instance_diff(None, instance)

        log_entry = LogEntry.objects.log_create(instance,
                                                action=LogEntry.Action.CREATE,
                                                changes=changes,
                                                actor=actor)
def log_update(sender, instance, **kwargs):
    """
    Signal receiver that creates a log entry when a model instance is changed and saved to the database.

    Direct use is discouraged, connect your model through :py:func:`auditlog.registry.register` instead.
    """
    if instance.pk is not None:
        try:
            old = sender.objects.get(pk=instance.pk)
        except sender.DoesNotExist:
            pass
        else:
            new = instance

            changes = model_instance_diff(old, new)

            # Log an entry only if there are changes
            if changes:
                logging.info({ 'LogType': 'AuditLog', 'Class': str(instance.__class__.__name__),
                               'InstanceID': int(instance.id), 'Action': 'Update',
                               'Changes':json.dumps(changes)}
                             )
def get_diff(m1, m2, fields=None):
    try:
        return model_instance_diff(m1, m2, fields)
    except Exception as ex:
        logger.error('Error getting diff between models %s for aduitlog: %s',
                     m1 or m2, ex)