Commits

Luke Plant committed 40b8534

Added shortcut for common case of using self.faker(field=field)

  • Participants
  • Parent commits d3f4787

Comments (0)

Files changed (2)

   ``anonymizer.base.Anonymizer`` in order to do filtering and other
   customization.
 
-  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 callables that take the following arguments:
+  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' which is useful for
+
+  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)
+
   An example Anonymizer for django.contrib.auth.models.User might look like
   this::
 
+      from datetime import datetime
+
       from anonymizer import Anonymizer
       from django.contrib.auth.models import User
 
           model = User
 
           attributes = {
-              'username':   lambda self, obj, field, val: self.faker.username(field=field),
-              'first_name': lambda self, obj, field, val: self.faker.first_name(field=field),
-              'last_name':  lambda self, obj, field, val: self.faker.last_name(field=field),
-              'email':      lambda self, obj, field, val: self.faker.email(field=field),
+              'username':   'username',
+              'first_name': 'first_name',
+              'last_name':  'last_name',
+              'email':      'email',
+              # Set the date_joined to a similar time to when they actually
+              # joined, by passing the 'val' parameter to faker.datetime
+              'date_joined': lambda self, obj, field, val: self.faker.datetime(field=field, val=val),
+              # Set to today:
+              'last_login': lambda *args: datetime.now()
           }
 
           def alter_object(self, obj):
+              if obj.is_superuser:
+                  return False # don't change, so we can still log in.
               super(UserAnonymizer, self).alter_object(obj)
+              # Destroy all passwords for everyone else
               obj.set_unusable_password()
 
 * If you need to create anonymizers for apps that you do not control, you may
   control. It doesn't matter if the anonymizer classes are for models that do
   not correspond to the applications they are contained it.
 
-  (For example, if you specify 'django.contrib.auth' as an app to specify, you
+  (For example, if you want to anonymize the models in django.contrib.auth, you
   will probably want to move the contents of django/contrib/auth/anonymizers.py
   into yourprojectapp/anonymizers.py)
 

anonymizer/base.py

        """
        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)
+       elif not callable(replacer):
+           raise Exception("Expected callable or string to be passed, got %r." % replacer)
+
        replacement = replacer(self, obj, field, currentval)
+
        setattr(obj, attname, replacement)
 
    def run(self):