def test_profile_entries_count(self): """ Get the number of entries associated with a profile """ user = BasicEmailUserFactory() profile = user.profile approved = ModerationState.objects.get(name='Approved') BasicEntryFactory(published_by=user, moderation_state=approved) published_entry_2 = BasicEntryFactory(published_by=user, moderation_state=approved, published_by_creator=True) created_entry = BasicEntryFactory(published_by=self.user, moderation_state=approved) EntryCreatorFactory(profile=profile, entry=created_entry) favorited_entry_1 = BasicEntryFactory(published_by=self.user, moderation_state=approved) favorited_entry_2 = BasicEntryFactory(published_by=self.user, moderation_state=approved) UserBookmarksFactory(profile=profile, entry=favorited_entry_1) UserBookmarksFactory(profile=profile, entry=favorited_entry_2) # Overlapping entries (entries that belong to more than one of created, published, or favorited) # These entries should not be duplicated in the entry count EntryCreatorFactory(profile=profile, entry=published_entry_2) UserBookmarksFactory(profile=profile, entry=created_entry) response = self.client.get(reverse('profile-entries', kwargs={'pk': profile.id})) response_profile = json.loads(str(response.content, 'utf-8')) self.assertEqual(response_profile['entry_count'], 5)
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(extended_profile=True) BasicEmailUserFactory.create(group=True) BasicEmailUserFactory.create(group=True, active=True) BasicEmailUserFactory.create(use_custom_name=True) BasicEmailUserFactory.create(use_custom_name=True, active=True) 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)) ] self.stdout.write('Creating creators') [CreatorFactory.create() for i in range(5)] # Select random entries and link them to 1 to 5 creators self.stdout.write('Linking random creators with random entries') for e in sample(list(Entry.objects.all()), k=100): [ OrderedCreatorRecordFactory.create(entry=e) for i in range(randint(1, 5)) ] self.stdout.write(self.style.SUCCESS('Done!'))