Luke Plant avatar Luke Plant committed 20dc2dd

Introduced 'replacers' module to publicly document the built-in replacers that are available

Comments (0)

Files changed (3)

 
   The 'attributes' dictionary is the key attribute to edit. The keys are the
   attribute names of attributes on the model that need to be set.  The values
-  are either strings (as a shortcut, see below), or callables that take the
-  following arguments:
-
-  * The Anonymizer instance
-  * The object being edited.
-  * The field being edited
-  * The current value of the field.
-
-
-  The Anonymizer instance has an attribute 'faker' attribute which is useful for
-  generating faked data.
-
-  If the value is a string, e.g. 'email', it is turned into a lambda
-  as follows::
-
-     lambda self, obj, field, val: self.faker.email(field=field)
+  are either strings or callables. If strings, they will be interpreted as a
+  function in the module ``anonymizers.replacers``. If callables, they should
+  have a signature compatible with those callables. You can use ``lambda *args:
+  my_constant_value`` to return a constant.
 
   For some fields, you will want to remove them from the list of attributes, so
   that the values will be unchanged - especially things like denormalised
-  fields. You may need to override the 'alter_object' to fix up any fields like
-  that.
+  fields. You can also override the 'alter_object' to do any fixing that may
+  be necessary.
 
   An example Anonymizer for django.contrib.auth.models.User might look like
   this::

anonymizer/base.py

 from faker import Faker
 from faker.utils import uk_postcode, bothify
 
+from anonymizer import replacers
+
 randrange = random.SystemRandom().randrange
 
 alphanumeric = ""
     # first_name
     # last_name
     # name
+    # email
     # full_address
     # phonenumber
     # street_address
        currentval = getattr(obj, attname)
        field = obj._meta.get_field_by_name(attname)[0]
        if isinstance(replacer, str):
-           # 'email' is shortcut for: lambda self, obj, field, val: self.faker.email(field=field)
-           fake_source = getattr(self.faker, replacer)
-           replacer = lambda self, obj, field, val: fake_source(field=field)
+           # 'email' is shortcut for: replacers.email
+           replacer = getattr(replacers, replacer)
        elif not callable(replacer):
            raise Exception("Expected callable or string to be passed, got %r." % replacer)
 

anonymizer/replacers.py

+# Pre-built replacers.
+
+varchar = lambda anon, obj, field, val: anon.faker.varchar(field=field)
+bool = lambda anon, obj, field, val: anon.faker.bool(field=field)
+integer = lambda anon, obj, field, val: anon.faker.integer(field=field)
+positive_integer = lambda anon, obj, field, val: anon.faker.positive_integer(field=field)
+small_integer = lambda anon, obj, field, val: anon.faker.small_integer(field=field)
+positive_small_integer = lambda anon, obj, field, val: anon.faker.positive_small_integer(field=field)
+datetime = lambda anon, obj, field, val: anon.faker.datetime(field=field)
+date = lambda anon, obj, field, val: anon.faker.date(field=field)
+uk_postcode = lambda anon, obj, field, val: anon.faker.uk_postcode(field=field)
+uk_country = lambda anon, obj, field, val: anon.faker.uk_country(field=field)
+uk_county = lambda anon, obj, field, val: anon.faker.uk_county(field=field)
+username = lambda anon, obj, field, val: anon.faker.username(field=field)
+first_name = lambda anon, obj, field, val: anon.faker.first_name(field=field)
+last_name = lambda anon, obj, field, val: anon.faker.last_name(field=field)
+name = lambda anon, obj, field, val: anon.faker.name(field=field)
+email = lambda anon, obj, field, val: anon.faker.email(field=field)
+full_address = lambda anon, obj, field, val: anon.faker.full_address(field=field)
+phonenumber = lambda anon, obj, field, val: anon.faker.phonenumber(field=field)
+street_address = lambda anon, obj, field, val: anon.faker.street_address(field=field)
+city = lambda anon, obj, field, val: anon.faker.city(field=field)
+state = lambda anon, obj, field, val: anon.faker.state(field=field)
+zip_code = lambda anon, obj, field, val: anon.faker.zip_code(field=field)
+company = lambda anon, obj, field, val: anon.faker.company(field=field)
+lorem = lambda anon, obj, field, val: anon.faker.lorem(field=field)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.