1. Luke Plant
  2. django-anonymizer


Luke Plant  committed bb5eac4

Added support for DecimalField

  • Participants
  • Parent commits d720b9e
  • Branches default

Comments (0)

Files changed (5)

File anonymizer/base.py

View file
  • Ignore whitespace
 from datetime import datetime
+import decimal
 import random
 from faker import data
         d = self.datetime(field=field, val=val)
         return d.date()
+    def decimal(self, field=None, val=None):
+        source = lambda: decimal.Decimal(random.randrange(0, 100000))/(10**field.decimal_places)
+        return self.get_allowed_value(source, field)
     def uk_postcode(self, field=None):
         return self.get_allowed_value(uk_postcode, field)

File anonymizer/introspect.py

View file
  • Ignore whitespace
     'SmallIntegerField': '"small_integer"',
     'PositiveIntegerField': '"positive_integer"',
     'PositiveSmallIntegerField': '"positive_small_integer"',
+    'DecimalField': '"decimal"',
 # NB - order matters. 'address' is more generic so should be at the end.

File anonymizer/replacers.py

View file
  • Ignore whitespace
 Returns a random date
+decimal = lambda anon, obj, field, val: anon.faker.decimal(field=field)
+decimal.__doc__ = """
+Returns a random decimal
 uk_postcode = lambda anon, obj, field, val: anon.faker.uk_postcode(field=field)
 uk_postcode.__doc__ = """
 Generates a random UK postcode (not necessarily valid, but it will look like one).

File anonymizer/tests/__init__.py

View file
  • Ignore whitespace
 from datetime import datetime, timedelta, date
+import decimal
 import os
 import sys
         ('some_datetime', "datetime"),
         ('some_date', "date"),
         ('sex', "choice"),
+        ('price', "decimal"),
         self.assertEqual(mod.strip(), expected.strip())
+                                                       price=decimal.Decimal("1.23"),
     def test_eveything(self):
                 ('some_datetime', "datetime"),
                 ('some_date', "date"),
                 ('sex', "choice"),
+                ('price', "decimal"),

File anonymizer/tests/models.py

View file
  • Ignore whitespace
     some_date = models.DateField()
     sex = models.CharField(max_length=1, choices=[('M', 'Male'),
                                                   ('F', 'Female')])
+    price = models.DecimalField(decimal_places=2, max_digits=10)