Skip to main content

Anonymize production data so it can be safely used in not-so-safe environments

Project description

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

https://raw.githubusercontent.com/Tesorio/django-anon/master/django-anon-recording.gif

Features

🚀

Really fast data anonymization and database operations using bulk updates to operate over huge tables

🍰

Flexible to use your own anonymization functions or external libraries like Faker

🐩

Elegant solution following consolidated patterns from projects like Django and Factory Boy

🔨

Powerful. It can be used on any projects, not only Django, not only Python. Really!

Table of Contents

Installation

pip install django-anon

Supported versions

  • Python (2.7, 3.7)

  • Django (1.8, 1.11, 2.2, 3.0)

License

MIT

Usage

Use anon.BaseAnonymizer to define your anonymizer classes:

import anon

from your_app.models import Person

class PersonAnonymizer(anon.BaseAnonymizer):
   email = anon.fake_email

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

   class Meta:
      model = Person

# run anonymizer: be cautious, this will affect your current database!
PersonAnonymizer().run()

Built-in functions

import anon

anon.fake_word(min_size=_min_word_size, max_size=20)
anon.fake_text(max_size=255, max_diff_allowed=5, separator=' ')
anon.fake_small_text(max_size=50)
anon.fake_name(max_size=15)
anon.fake_username(max_size=10, separator='')
anon.fake_email(max_size=25, suffix='@example.com')
anon.fake_url(max_size=50, scheme='http://', suffix='.com')
anon.fake_phone_number(format='999-999-9999')

Lazy attributes

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

import anon

from your_app.models import Person

class PersonAnonymizer(anon.BaseAnonymizer):
   name = anon.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

The clean method

import anon

class UserAnonymizer(anon.BaseAnonymizer):
   class Meta:
      model = User

   def clean(self, obj):
      obj.set_password('test')
      obj.save()

Defining a custom QuerySet

A custom QuerySet can be used to select the rows that should be anonymized:

import anon

from your_app.models import Person

class PersonAnonymizer(anon.BaseAnonymizer):
   email = anon.fake_email

   class Meta:
      model = Person

   def get_queryset(self):
      # keep admins unmodified
      return Person.objects.exclude(is_admin=True)

High-quality fake data

In order to be really fast, django-anon uses it’s own algorithm to generate fake data. It is really fast, but the generated data is not pretty. If you need something prettier in terms of data, we suggest using Faker, which can be used out-of-the-box as the below:

import anon

from faker import Faker
from your_app.models import Address

faker = Faker()

class PersonAnonymizer(anon.BaseAnonymizer):
   postalcode = faker.postalcode

   class Meta:
      model = Address

Changelog

Check out CHANGELOG.rst for release notes

Contributing

Check out CONTRIBUTING.rst for information about getting involved


Icon made by Eucalyp from www.flaticon.com

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-anon-0.2.tar.gz (12.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_anon-0.2-py2.py3-none-any.whl (11.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-anon-0.2.tar.gz.

File metadata

  • Download URL: django-anon-0.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for django-anon-0.2.tar.gz
Algorithm Hash digest
SHA256 f2d77f8d3c88c3a20bee02be531c04466e9ec04a7fd779137b58fb8a529fdfe6
MD5 21cc2c58d6b37512c9bd3f70e31f946d
BLAKE2b-256 cebc901b5374958473fe82ba0548278f173b258791645cfde224435b7c2d5c54

See more details on using hashes here.

File details

Details for the file django_anon-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: django_anon-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for django_anon-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 beee9f201ff16d37404ac4a05a9eff6bcd81d663ecc49f3756c6853413392ddd
MD5 55f598391a9d385c05b6637763f1d4e0
BLAKE2b-256 7b41512893179372397f680a95661c56cda79737ebad37a704585b39f389eecf

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