Commits

Luke Plant committed bb5eac4

Added support for DecimalField

Comments (0)

Files changed (5)

anonymizer/base.py

 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)
 

anonymizer/introspect.py

     '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.

anonymizer/replacers.py

 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).

anonymizer/tests/__init__.py

 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())
                                                        some_datetime=datetime.now(),
                                                        some_date=date.today(),
                                                        sex='X',
+                                                       price=decimal.Decimal("1.23"),
                                                        )
 
     def test_eveything(self):
                 ('some_datetime', "datetime"),
                 ('some_date', "date"),
                 ('sex', "choice"),
+                ('price', "decimal"),
             ]
 
         EverythingAnonmyizer().run()

anonymizer/tests/models.py

     some_date = models.DateField()
     sex = models.CharField(max_length=1, choices=[('M', 'Male'),
                                                   ('F', 'Female')])
+    price = models.DecimalField(decimal_places=2, max_digits=10)
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.