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 run_test_profile_entries(self, version, entry_type, order_by=None):
        get_created_entries = entry_type == 'created'
        get_published_entries = entry_type == 'published'
        get_favorited_entries = entry_type == 'favorited'
        entry_serializer_class = EntryWithCreatorsBaseSerializer
        if version == settings.API_VERSIONS['version_1']:
            entry_serializer_class = EntryWithV1CreatorsBaseSerializer

        user = self.user
        # "Created" entry_type profile used as default
        profile = EntryCreator.objects.first().profile

        if get_published_entries:
            profile = user.profile
            approved = ModerationState.objects.get(name='Approved')
            for _ in range(0, 3):
                BasicEntryFactory.create(published_by=user, moderation_state=approved)
        elif get_favorited_entries:
            profile = user.profile
            for entry in Entry.objects.all()[:4]:
                self.client.put(reverse('bookmark', kwargs={'entryid': entry.id}))

        expected_entries = UserProfileEntriesSerializer(
            instance=profile,
            context={
                'user': self.user,
                'created': get_created_entries,
                'published': get_published_entries,
                'favorited': get_favorited_entries,
                'created_ordering': order_by if get_created_entries else None,
                'published_ordering': order_by if get_published_entries else None,
                'favorited_ordering': order_by if get_favorited_entries else None,
                'EntrySerializerClass': entry_serializer_class
            },
        ).data[entry_type]

        profile_entries_url = '{url}?{entry_type}'.format(
            url=reverse(
                'profile-entries',
                args=[version + '/', profile.id]
            ),
            entry_type=entry_type
        )
        if order_by:
            profile_entries_url += '&{entry_type}_ordering={ordering_field}'.format(
                entry_type=entry_type,
                ordering_field=order_by
            )
        response = self.client.get(profile_entries_url)
        profile_json = json.loads(str(response.content, 'utf-8'))
        self.assertListEqual(profile_json[entry_type], expected_entries)
    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!'))