3131from django .core .management .base import BaseCommand , CommandError
3232from django .db import connection , transaction
3333from django .utils import timezone
34-
34+ from faker import Faker
3535# Local app imports
3636from claim .models import Claim , ClaimItem , ClaimService
3737from core .models import Officer
@@ -62,48 +62,21 @@ def reset_optimizations():
6262
6363
6464class DataGenerator :
65- """Generate realistic test data without external dependencies like Faker, Faker would be great choice and is in TODO"""
65+ """Generate realistic test data using Faker library"""
66+
6667 def __init__ (self ):
67- self .first_names = [
68- 'John' , 'Mary' , 'James' , 'Patricia' , 'Robert' , 'Jennifer' , 'Michael' , 'Linda' ,
69- 'William' , 'Elizabeth' , 'David' , 'Barbara' , 'Richard' , 'Susan' , 'Joseph' , 'Jessica' ,
70- 'Thomas' , 'Sarah' , 'Charles' , 'Karen' , 'Christopher' , 'Nancy' , 'Daniel' , 'Lisa' ,
71- 'Matthew' , 'Betty' , 'Anthony' , 'Helen' , 'Mark' , 'Sandra' , 'Donald' , 'Donna' ,
72- 'Steven' , 'Carol' , 'Paul' , 'Ruth' , 'Andrew' , 'Sharon' , 'Joshua' , 'Michelle' ,
73- 'Kenneth' , 'Laura' , 'Kevin' , 'Kimberly' , 'Brian' , 'Deborah' , 'George' , 'Dorothy' ,
74- 'Edward' , 'Amy' , 'Ronald' , 'Angela' , 'Timothy' , 'Ashley' , 'Jason' , 'Brenda' ,
75- 'Jeffrey' , 'Emma' , 'Ryan' , 'Olivia' , 'Jacob' , 'Cynthia' , 'Gary' , 'Marie'
76- ]
77-
78- self .last_names = [
79- 'Smith' , 'Johnson' , 'Williams' , 'Brown' , 'Jones' , 'Garcia' , 'Miller' , 'Davis' ,
80- 'Rodriguez' , 'Martinez' , 'Hernandez' , 'Lopez' , 'Gonzalez' , 'Wilson' , 'Anderson' ,
81- 'Thomas' , 'Taylor' , 'Moore' , 'Jackson' , 'Martin' , 'Lee' , 'Perez' , 'Thompson' ,
82- 'White' , 'Harris' , 'Sanchez' , 'Clark' , 'Ramirez' , 'Lewis' , 'Robinson' , 'Walker' ,
83- 'Young' , 'Allen' , 'King' , 'Wright' , 'Scott' , 'Torres' , 'Nguyen' , 'Hill' ,
84- 'Flores' , 'Green' , 'Adams' , 'Nelson' , 'Baker' , 'Hall' , 'Rivera' , 'Campbell' ,
85- 'Mitchell' , 'Carter' , 'Roberts' , 'Gomez' , 'Phillips' , 'Evans' , 'Turner' , 'Diaz'
86- ]
87-
88- self .streets = [
89- 'Main St' , 'First St' , 'Second St' , 'Park Ave' , 'Oak St' , 'Pine St' , 'Maple Ave' ,
90- 'Cedar St' , 'Elm St' , 'Washington St' , 'Lake St' , 'Hill St' , 'Church St' ,
91- 'School St' , 'High St' , 'Union St' , 'Water St' , 'Broadway' , 'Market St' ,
92- 'Mill St' , 'South St' , 'North St' , 'West St' , 'East St' , 'Center St'
93- ]
94-
95- self .cities = [
96- 'Springfield' , 'Franklin' , 'Georgetown' , 'Madison' , 'Riverside' , 'Arlington' ,
97- 'Fairview' , 'Salem' , 'Kingston' , 'Clinton' , 'Greenwood' , 'Bristol' , 'Oxford' ,
98- 'Ashland' , 'Burlington' , 'Manchester' , 'Auburn' , 'Dayton' , 'Lexington' ,
99- 'Milford' , 'Hudson' , 'Hampton' , 'Lincoln' , 'Newport' , 'Chester'
100- ]
101-
102- self .chf_id_counter = random .randint (100000000 , 200000000 ) #chf_id counter , relevant for large dataset between these integer value
103-
104- def get_first_name (self ): return random .choice (self .first_names )
105- def get_last_name (self ): return random .choice (self .last_names )
106- def get_address (self ): return f"{ random .randint (1 , 9999 )} { random .choice (self .streets )} , { random .choice (self .cities )} "
68+ self .faker = Faker ()
69+ self .chf_id_counter = random .randint (100000000 , 200000000 )
70+
71+ def get_first_name (self ):
72+ return self .faker .first_name ()
73+
74+ def get_last_name (self ):
75+ return self .faker .last_name ()
76+
77+ def get_address (self ):
78+ return self .faker .street_address () + ", " + self .faker .city ()
79+
10780 def get_unique_chf_id (self ):
10881 self .chf_id_counter += 1
10982 return f"CHF{ self .chf_id_counter :09d} "
@@ -362,10 +335,8 @@ def handle(self, *args, **options):
362335 if input (f"\n { self .style .WARNING ('WARNING:' )} This will create a large amount of test data. Continue? (yes/no): " ).lower () not in ['yes' , 'y' ]:
363336 self .stdout .write (self .style .ERROR ("Operation cancelled." )); return
364337
365- try :
366- with transaction .atomic ():
367- generator = BulkInsureeGenerator (batch_size = batch_size , stdout = self .stdout )
368- results = generator .generate_bulk_data (config ['families' ], config ['members' ], config ['claims' ])
369- self .stdout .write (self .style .SUCCESS (f"\n Successfully generated synthetic data in { results ['duration' ]:.2f} seconds!" ))
370- except Exception as e :
371- raise CommandError (f'Data generation failed: { e } ' )
338+ with transaction .atomic ():
339+ generator = BulkInsureeGenerator (batch_size = batch_size , stdout = self .stdout )
340+ results = generator .generate_bulk_data (config ['families' ], config ['members' ], config ['claims' ])
341+
342+ self .stdout .write (self .style .SUCCESS (f"\n Successfully generated synthetic data in { results ['duration' ]:.2f} seconds!" ))
0 commit comments