Skip to main content

Anonymize specific django model for specific instance

Project description

Django Neuralyzer

PyPi - version Licence Python version Ruff

Anonymize instance data according to your need (GDPR, used in not-so-safe environments... )

Heavily inspired by django-anon

Like django-anon, django-neuralyzer will help you anonymize your model instance so it can be shared among developers, helping to reproduce bugs and make performance improvements in a production-like environment.

It also provide ability to target specific instance and not the whole database at once, so it can be use for GDPR compliance or "forget about be" feature of your app.

Usage

Use django-neuralyzer.BaseNeuralyzer to define your neuralyzer classes:

from django_neuralyzer.base import BaseNeuralyzer

from your_app.models import Person

class PersonNeuralyzer(BaseNeuralyzer):
   email = "example@anonymized.org"

   # You can use static values instead of callables
   is_admin = False

   class Meta:
      model = Person

# run neuralyzer: be cautious, this will affect your current database!
person = Person.objects.last()

# neuralyze full table:
PersonNeuralyzer().run()

# neuralyze only some instance
PersonNeuralyzer().run(filters={"pk": person.pk})

Lazy attributes

Lazy attributes can be defined as inline lambdas or methods, as shown below, using the lazy_attribute function/decorator.

from django_neuralyzer.base import BaseNeuralyzer, lazy_attribute

from your_app.models import Person

class PersonNeuralyzer(BaseNeuralyzer):
   name = lazy_attribute(lambda o: 'x' * len(o.name))

   @lazy_attribute
   def date_of_birth(self):
      """Keep year and month"""
      return self.date_of_birth.replace(day=1)

   class Meta:
      model = Person

Management command

First, to have access to the management command, you need to register the app

INSTALLED_APPS = [
  ...
  "django_neuralyzer",
  ...
]

2 management command are given:

  1. ensure_fields_are_handled command
  2. export_neuralyzed_fields command

Ensure that all fields are neuralyzed

Ensure that all field of your model are handle by the neuralyzer:

django-manage ensure_fields_are_handled

If you want to ensure a field is handled, but you don't want to change its value, you can set it to NEURALYZER_NOOP:

from django_neuralyzer.base import BaseNeuralyzer, NEURALYZER_NOOP

from your_app.models import Person

class PersonNeuralyzer(BaseNeuralyzer):
   id = NEURALYZER_NOOP
   name = lazy_attribute(lambda o: 'x' * len(o.name))

   class Meta:
      model = Person

this way, ensure_fields_are_handled will not complain that id is not handled.

Export neuralyzed fields

It may be handy to export what fields are neuralyzed and how. Run the

django-manage export_neuralyzed_fields

and a neuralyzed_fields.csv will be created at the root of your application.

In order to document the lazy_attribute, the docstring of the function will be used to document how this field is neuralyzed.

Why neuralyzer ?

In Men in Black, a "neuralyzer" is a tool that wipe the mind of anybody who sees the flash via isolating and editing certain element of their memory.

This is exactly what this django package intent to do (remove or fake some attributes of instances), so we find it funny to name it like that.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_neuralyzer-0.4.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_neuralyzer-0.4.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file django_neuralyzer-0.4.0.tar.gz.

File metadata

  • Download URL: django_neuralyzer-0.4.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_neuralyzer-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7ce361b644c60fcaa6d30b9683e411b73a14d5f5b7bd123700a48b399309f10b
MD5 541c74bf7addd022f019c931fd3de6a9
BLAKE2b-256 90e5f99355fd92099cf19dd47fbf944456085d3e625392d0ceacc0a7b0815f6d

See more details on using hashes here.

File details

Details for the file django_neuralyzer-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_neuralyzer-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c2595f0530d4299827af975f4a673bcbb73d01871bc1a6180fd62d92939fc7b
MD5 1cdecc3cd45f329a03c61f3de61a1ab4
BLAKE2b-256 34acd0387e4587fc2e9ca8979ed4acdcafe46aa273396f2f87b7db290fa18bcb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page