def get(self, request, pk, **kwargs): """ Return a list of entries associated with this profile that can be filtered by entries that this profile - was a creator on, was a publisher of, or favorited. """ profile = get_object_or_404( UserProfile.objects.select_related('related_user'), pk=pk, ) query = request.query_params EntrySerializerClass = EntryWithCreatorsBaseSerializer if request and request.version == settings.API_VERSIONS['version_1']: EntrySerializerClass = EntryWithV1CreatorsBaseSerializer return Response( UserProfileEntriesSerializer(instance=profile, context={ 'user': request.user, 'created': 'created' in query, 'published': 'published' in query, 'favorited': 'favorited' in query, 'created_ordering': query.get('created_ordering'), 'published_ordering': query.get('published_ordering'), 'favorited_ordering': query.get('favorited_ordering'), 'EntrySerializerClass': EntrySerializerClass, }).data )
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 test_profile_entries_published(self): """ Get the published entries for a profile """ profile = UserProfile.objects.filter( related_user__entries__isnull=False)[:1].get() entries = [ UserProfileEntriesSerializer.serialize_entry(entry) for entry in Entry.objects.public().filter( published_by=profile.user) ] response = self.client.get('{url}?published'.format( url=reverse('profile-entries', kwargs={'pk': profile.id}))) profile_json = json.loads(str(response.content, 'utf-8')) self.assertListEqual(profile_json['published'], entries)
def test_profile_entries_created(self): """ Get the created entries for a profile """ profile = OrderedCreatorRecord.objects.filter( creator__profile__isnull=False)[:1].get().creator.profile entries = [ UserProfileEntriesSerializer.serialize_entry(ocr.entry) for ocr in OrderedCreatorRecord.objects.filter( creator__profile=profile) ] response = self.client.get('{url}?created'.format( url=reverse('profile-entries', kwargs={'pk': profile.id}))) profile_json = json.loads(str(response.content, 'utf-8')) self.assertListEqual(profile_json['created'], entries)
def test_profile_entries_favorited(self): """ Get the favorited entries for a profile """ profile = self.user.profile entries = Entry.objects.public()[:2] serialized_entries = [] for entry in entries: self.client.put(reverse('bookmark', kwargs={'entryid': entry.id})) serialized_entries.append( UserProfileEntriesSerializer.serialize_entry(entry)) response = self.client.get('{url}?favorited'.format( url=reverse('profile-entries', kwargs={'pk': profile.id}))) profile_json = json.loads(str(response.content, 'utf-8')) self.assertListEqual(profile_json['favorited'], serialized_entries)
def get(self, request, pk, **kwargs): """ Return a list of entries associated with this profile that can be filtered by entries that this profile - was a creator on, was a publisher of, or favorited. """ profile = get_object_or_404( UserProfile.objects.select_related('related_creator', 'related_user'), pk=pk, ) query = request.query_params return Response( UserProfileEntriesSerializer(instance=profile, context={ 'created': 'created' in query, 'published': 'published' in query, 'favorited': 'favorited' in query }).data)