Need help with django-anon?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

Tesorio
134 Stars 1 Forks MIT License 50 Commits 2 Opened issues

Description

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

Services available

!
?

Need anything else?

Contributors list

# 26,167
Python
Django
ply
Erlang
47 commits

.. BANNERSTART .. Since PyPI does not support raw directives, we remove them from the README .. .. raw directives are only used to make README fancier on GitHub and do not .. contain relevant information to be displayed in PyPI, as they are not tied .. to the current version, but to the current development status .. raw:: html

django-anon

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

Install | Read Documentation | PyPI | Contribute

.. BANNEREND

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.

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

.. start-features

Features

.. start-features-table

.. csv-table::

"🚀", "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!"

.. end-features-table .. end-features .. start-table-of-contents

Table of Contents

.. contents:: :local:

.. end-table-of-contents .. start-introduction

Installation

.. code::

pip install django-anon

Supported versions

  • Python (2.7, 3.7)
  • Django (1.8, 1.11, 2.2, 3.0)

License

MIT 
_

.. end-introduction .. start-usage

Usage

Use

anon.BaseAnonymizer
to define your anonymizer classes:

.. code-block:: python

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

.. code:: python

import anon

anon.fakeword(minsize=minwordsize, maxsize=20) anon.faketext(maxsize=255, maxdiffallowed=5, separator=' ') anon.fakesmalltext(maxsize=50) anon.fakename(maxsize=15) anon.fakeusername(maxsize=10, separator='') anon.fakeemail(maxsize=40, suffix='@example.com') anon.fakeurl(maxsize=50, scheme='http://', suffix='.com') anon.fakephone_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.

.. code-block:: python

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

.. code-block:: python

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:

.. code-block:: python

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:

.. code-block:: python

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

.. end-usage

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 
_

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.