def test_get_json_context(self): request = self.get_request(self.user) view = self.get_instance(TagStreamView, request=request) profile = ProfileSerializer(request.user.profile, context={ 'request': request }).data view.tag = self.tag_no_content self.assertEqual( view.get_json_context(), { "currentBrowsingProfileId": self.user.profile.id, "streamName": view.stream_name, "isUserAuthenticated": True, "ownProfile": profile, }) view = self.get_instance(TagStreamView, request=self.get_request(AnonymousUser())) view.tag = self.tag_no_content self.assertEqual( view.get_json_context(), { "currentBrowsingProfileId": None, "streamName": view.stream_name, "isUserAuthenticated": False, "ownProfile": {}, })
def set_object_and_data(self): if not self.object: self.object = self.get_object() if not self.data and self.object.visible_to_user(self.request.user): self.data = ProfileSerializer(self.object, context={ 'request': self.request }).data self.data["stream_type"] = self.profile_stream_type
def get_json_context(self): json_context = super().get_json_context() json_context.update({ "profile": ProfileSerializer(self.target_profile, context={ 'request': self.request }).data }) json_context["profile"].update( {"stream_type": self.profile_stream_type}) return json_context
def test_get_json_context(self): request = self.get_request(self.user) view = self.get_instance(LimitedStreamView, request=request) profile = ProfileSerializer(request.user.profile, context={'request': request}).data self.assertEqual( view.get_json_context(), { "currentBrowsingProfileId": self.user.profile.id, "streamName": view.stream_name, "isUserAuthenticated": True, "ownProfile": profile, } )
def get_json_context(self): if self.request.user.is_authenticated: profile = ProfileSerializer(self.request.user.profile, context={ 'request': self.request }).data else: profile = {} return { "currentBrowsingProfileId": getattr(getattr(self.request.user, "profile", None), "id", None), "isUserAuthenticated": bool(self.request.user.is_authenticated), "streamName": self.stream_name, "ownProfile": profile, }
def collect_data(self): # User serializer = UserSerializer(instance=self.user, context=self.context) self.data['user'] = serializer.data # Profile serializer = ProfileSerializer(instance=self.user.profile, context=self.context) self.data['profile'] = serializer.data # Followed profiles self.data['following'] = [] for follow in self.user.profile.following.all(): serializer = LimitedProfileSerializer(instance=follow, context=self.context) self.data['following'].append(serializer.data) # Content self.data['content'] = [] content_qs = Content.objects.filter( author=self.user.profile).order_by('created') for content in content_qs: serializer = ContentSerializer(instance=content, context=self.context) self.data['content'].append(serializer.data)
def test_get_json_context(self): request, view, contents, profile = self._get_request_view_and_content(create_content=False) ownProfile = ProfileSerializer(request.user.profile, context={'request': request}).data self.assertEqual( view.get_json_context(), { "currentBrowsingProfileId": profile.id, "streamName": view.stream_name, "isUserAuthenticated": True, "profile": { "id": profile.id, "uuid": str(profile.uuid), "fid": profile.fid, "followed_tags": [], "followers_count": 0, "following_count": profile.following.count(), "handle": profile.handle, "has_pinned_content": Content.objects.profile_pinned(profile, request.user).exists(), "home_url": profile.home_url, "image_url_large": profile.image_url_large, "image_url_medium": profile.image_url_medium, "image_url_small": profile.image_url_small, "is_local": profile.is_local, "location": profile.location, "name": profile.name, "nsfw": profile.nsfw, "stream_type": view.profile_stream_type, "url": profile.url, "user_following": False, "visibility": str(profile.visibility).lower(), }, "ownProfile": ownProfile, }, ) request, view, contents, profile = self._get_request_view_and_content(anonymous_user=True, create_content=False) self.assertEqual( view.get_json_context(), { "currentBrowsingProfileId": None, "streamName": view.stream_name, "isUserAuthenticated": False, "profile": { "id": profile.id, "uuid": str(profile.uuid), "fid": profile.fid, "followed_tags": [], "followers_count": 0, "following_count": profile.following.count(), "handle": profile.handle, "has_pinned_content": Content.objects.profile_pinned(profile, request.user).exists(), "home_url": profile.home_url, "image_url_large": profile.image_url_large, "image_url_medium": profile.image_url_medium, "image_url_small": profile.image_url_small, "is_local": profile.is_local, "location": profile.location, "name": profile.name, "nsfw": profile.nsfw, "stream_type": view.profile_stream_type, "url": profile.url, "user_following": False, "visibility": str(profile.visibility).lower(), }, "ownProfile": {}, }, )