Skip to content

GDPR compliance fix: Support parameter to auditlog.register to change how auditlog populates object_repr #818

Description

@shundread

We have a custom User model that includes user email / name into the string representation:

    def __str__(self):
        if self.first_name and self.last_name:
            return f"{self.last_name} {self.first_name} ({self.email})"
        else:
            return self.email

Due to django-auditlog's grabbing instances' string representation for its object_repr, this isn't enough to reduce the amount of PII we're writing onto our logs:

 auditlog.register(
    User,
+    mask_fields={"first_name", "last_name", "email"},
 )

We are putting in a workaround with signals at the time being, but it would be nice if similar projects can eliminate the same kind of limitation through a feature in the official package, using something like:

 auditlog.register(
    User,
+    mask_fields={"first_name", "last_name", "email"},
+    object_repr_field="pk",
 )

For reference, our workaround looks like this:

@receiver(post_log, sender=User)
def anonymize_user_object_repr(sender, instance, log_entry, log_created, **kwargs):
    if log_created and log_entry and log_entry.object_repr:
        log_entry.object_repr = instance.pk
        log_entry.save(update_fields=["object_repr"])

I have skimmed the classes involved on django-auditlog and it looks to me like some small changes in LogEntryManager and AuditLogModelRegistry could be sufficient to support the feature and spare the need for a workaround for our project and potentially others.

I'm happy to provide a pull request if this feature seems sensible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions