Skip to main content

Django-Hattori

pypi

Command to anonymize sensitive data. This app helps you anonymize data in a database used for development of a Django project.

This app is based on Django-Database-Anonymizer, using Faker to anonymize the values.

Installation

Install using pip:

pip install django-hattori

Then add 'hattori' to your INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    'hattori',
]

Important

You should only run the anonymize process in PRE or development environments. To avoid problems by default anonymization is disabled.

To enable you must add to settings ANONYMIZE_ENABLED=True

Usage

How to execute command:

./manage.py anonymize_db

Possible arguments:

  • -a, --app: Define a app you want to anonymize. All anonymizers in this app will be run. Eg. anonymize_db -a shop
  • -m, --models: List of models you want to anonymize. Eg. anonymize_db -m Customer,Product
  • -b, --batch-size: batch size used in the bulk_update of the instances. Depends on the DB machine, default use 500.

Writing anonymizers

In order to use the management command we need to define anonymizers.

  • Create a module anonymizers.py in the given django-app
  • An anonymizer is a simple class that inherits from BaseAnonymizer
  • Each anonymizer class is going to represent one model
  • An anonymizer has the following members:
    • model: (required) The model class for this anonymizer
    • attributes: (required) List of tuples that determine which fields to replace. The first value of the tuple is the fieldname, the second value is the replacer
    • get_query_set(): (optional) Define your QuerySet
  • A replacer is either of type str or callable
  • A callable replacer is a Faker instance or custom replacer.
  • All Faker methods are available. For more info read the official documentation Faker!

Example

from hattori.base import BaseAnonymizer, faker
from shop.models import Customer

class CustomerAnonymizer(BaseAnonymizer):
    model = Customer

    attributes = [
        ('card_number', faker.credit_card_number),
        ('first_name', faker.first_name),
        ('last_name', faker.last_name),
        ('phone', faker.phone_number),
        ('email', faker.email),
        ('city', faker.city),
        ('comment', faker.text),
        ('description', 'fix string'),
        ('code', faker.pystr),
    ]

    def get_query_set(self):
        return Customer.objects.filter(age__gt=18)

Extending the existing replacers with arguments

Use lambdas to extend certain predefined replacers with arguments, like min_chars or max_chars on faker.pystr:

('code', lambda **kwargs: faker.pystr(min_chars=250, max_chars=250, **kwargs)),

Important: don't forget the **kwargs!

Release files for django-hattori 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-hattori 0.2.1
File Size Uploaded
django-hattori-0.2.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-hattori 0.2.1
File Interpreter ABI Platform
django_hattori-0.2.1-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 14.8 kB

Release files / django-hattori-0.2.1.tar.gz

Download URL django-hattori-0.2.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6953d40881317252f19f62c4e7fe8058924b852c7498bc42beb7bc4d268c252c
BLAKE2b-256 checksum
How to use checksums
27f90087cf8ccbd57082082146c719c1afd9a3745e188bf483c225f275d4d197
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

Release files / django_hattori-0.2.1-py2.py3-none-any.whl

Download URL django_hattori-0.2.1-py2.py3-none-any.whl
Size 8.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e529ed7af8fc34a0169c797c477672b687a205a56f3f5206f90c260acb83b7ac
BLAKE2b-256 checksum
How to use checksums
f483705362cb906746bddb6213f6cd67930f5329852f58e89403dc5ad3640be3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page