def handle(self, *args, **options): if options['delete']: call_command('flush_data') if options['seed']: seed = options['seed'] else: seed = randint(0, 5000000) print('Seeding Faker with {}'.format(seed)) faker = factory.faker.Faker._get_faker(locale='en-US') faker.random.seed(seed) print('Creating tags') [TagFactory.create() for i in range(options['tags_count'])] print('Creating users') generate_fake_data(BasicEmailUserFactory, options['users_count']) # Since every Mozilla users' emails must be @mozillafoundation.org, we run into "UNIQUE constraint failed" # error from time to time try: generate_fake_data(MozillaEmailUserFactory, options['users_count']) except IntegrityError: print( 'Failed to create user: this email address is already in the database. No more users will be ' 'created this run.') pass print('Creating pulse entries') generate_fake_data(BasicEntryFactory, options['entries_count']) generate_fake_data(GetInvolvedEntryFactory, options['entries_count']) print('Creating fellows') generate_fellows(options['fellows_count']) # Select random published entries and bookmark them for 1 to 10 users print('Creating bookmarks') approved_entries = Entry.objects.public().with_related() for e in sample(list(approved_entries), k=len(approved_entries) // 2): [ UserBookmarksFactory.create(entry=e) for i in range(randint(1, 10)) ] # Select random entries and link them to 1 to 5 creators print('Linking random profiles as creators with random entries') all_entries = Entry.objects.all() for e in sample(list(all_entries), k=len(all_entries) // 2): try: [ EntryCreatorFactory.create(entry=e) for i in range(randint(1, 5)) ] # That creator and entry might already be linked. If it's the case, skip it and move on: except IntegrityError: continue print(self.style.SUCCESS('Done!'))
def handle(self, *args, **options): if options['delete']: call_command('flush_data') if options['seed']: seed = options['seed'] else: seed = randint(0, 5000000) self.stdout.write('Seeding Faker with {}'.format(seed)) faker = factory.faker.Faker._get_faker(locale='en-US') faker.random.seed(seed) self.stdout.write('Creating tags') [TagFactory.create() for i in range(6)] self.stdout.write('Creating generic users') BasicEmailUserFactory.create() [BasicEmailUserFactory.create(active=True) for i in range(2)] BasicEmailUserFactory.create(group=True) BasicEmailUserFactory.create(group=True, active=True) BasicEmailUserFactory.create(use_custom_name=True) BasicEmailUserFactory.create(use_custom_name=True, active=True) # Call the generate_fellows command to handle fellows generation fellows_count = options['fellows_count'] call_command('generate_fellows', f'--fellowsCount={fellows_count}') self.stdout.write('Creating Mozilla users') MozillaEmailUserFactory.create() MozillaEmailUserFactory.create(active=True) MozillaEmailUserFactory.create(active=True, extended_profile=True) MozillaEmailUserFactory.create(active=True, staff=True) MozillaEmailUserFactory.create(active=True, staff=True, extended_profile=True) MozillaEmailUserFactory.create(active=True, staff=True, admin=True) MozillaEmailUserFactory.create(active=True, staff=True, admin=True, extended_profile=True) self.stdout.write('Creating pulse entries') [BasicEntryFactory.create() for i in range(20)] [ BasicEntryFactory.create(is_published_by_creator=True) for i in range(20) ] [BasicEntryFactory.create(mozillauser=True) for i in range(20)] [ BasicEntryFactory.create(mozillauser=True, is_published_by_creator=True) for i in range(20) ] self.stdout.write('Creating featured pulse entries') [BasicEntryFactory.create(is_featured=True) for i in range(20)] [ BasicEntryFactory.create(is_featured=True, is_published_by_creator=True) for i in range(20) ] [ BasicEntryFactory.create(mozillauser=True, is_featured=True) for i in range(20) ] self.stdout.write('Creating "get involved" pulse entries') [GetInvolvedEntryFactory.create() for i in range(20)] [GetInvolvedEntryFactory.create(is_featured=True) for i in range(20)] [GetInvolvedEntryFactory.create(mozillauser=True) for i in range(20)] # Select random published entries and bookmark them for 1 to 10 users self.stdout.write('Creating bookmarks') approved_entries = Entry.objects.public().with_related() for e in sample(list(approved_entries), k=len(approved_entries) // 2): [ UserBookmarksFactory.create(entry=e) for i in range(randint(1, 10)) ] # Select random entries and link them to 1 to 5 creators self.stdout.write( 'Linking random profiles as creators with random entries') for e in sample(list(Entry.objects.all()), k=100): [EntryCreatorFactory.create(entry=e) for i in range(randint(1, 5))] self.stdout.write(self.style.SUCCESS('Done!'))