def test_unshare_removes_a_share(self): self.public_content.share(self.local_user.profile) assert Content.has_shared(self.public_content.id, self.local_user.profile.id) self.public_content.unshare(self.local_user.profile) self.assertFalse( Content.has_shared(self.public_content.id, self.local_user.id))
def test_has_shared(self): self.assertFalse( Content.has_shared(self.public_content.id, self.local_user.profile.id)) # Do a share self.public_content.share(self.local_user.profile) self.assertTrue( Content.has_shared(self.public_content.id, self.local_user.profile.id))
def test_get_rendered_contents_for_user(self): qs = Content.objects.filter( id__in=[self.public_content.id, self.site_content.id]) contents = Content.get_rendered_contents(qs) self.assertEqual( contents, [{ "id": self.public_content.id, "guid": self.public_content.guid, "author": self.public_content.author_id, "author_image": self.public_content.author.safer_image_url_small, "author_name": self.public_content.author.name, "rendered": "<p><strong>Foobar</strong></p>", "humanized_timestamp": self.public_content.humanized_timestamp, "formatted_timestamp": self.public_content.formatted_timestamp, }, { "id": self.site_content.id, "guid": self.site_content.guid, "author": self.site_content.author_id, "author_image": self.site_content.author.safer_image_url_small, "author_name": self.site_content.author.handle, "rendered": "<p><em>Foobar</em></p>", "humanized_timestamp": self.site_content.humanized_timestamp, "formatted_timestamp": self.site_content.formatted_timestamp, }])
def test_get_rendered_contents__with_throughs(self): qs = Content.objects.filter( id__in=[self.public_content.id, self.site_content.id]) contents = Content.get_rendered_contents( qs, self.user2, throughs={self.site_content.id: 123}) self.assertEqual(contents[0].get("through"), self.public_content.id) self.assertEqual(contents[1].get("through"), 123)
def handle_load_children(self, data): content_id = data.get("content_id") if not content_id: return # TODO: get recursively qs = Content.objects.children(content_id, self.message.user) contents = Content.get_rendered_contents(qs, self.message.user) payload = self.make_payload(contents, "children") payload["parent_id"] = content_id self.send_payload(payload)
def handle_load_content(self, data): """Send back the requested content.""" ids = data.get("ids") if not ids: return qs = self._get_stream_qs() qs = qs.filter(id__in=ids) contents = Content.get_rendered_contents(qs) payload = self.make_payload(contents, "prepended") self.send(payload)
def handle_load_more(self, data): """Load more content to the stream.""" last_id = data.get("last_id") if not last_id: return qs = self._get_stream_qs() qs = qs.filter(id__lt=last_id)[:20] contents = Content.get_rendered_contents(qs) payload = self.make_payload(contents, "appended") self.send(payload)
def test_get_rendered_contents_for_user(self): qs = Content.objects.filter(id__in=[self.public_content.id, self.site_content.id]) contents = Content.get_rendered_contents(qs, self.user2) self.assertEqual(contents, [ { "id": self.public_content.id, "guid": self.public_content.guid, "author": self.public_content.author_id, "author_guid": self.public_content.author.guid, "author_image": self.public_content.author.safer_image_url_small, "author_name": self.public_content.author.handle, "author_profile_url": self.public_content.author.get_absolute_url(), "author_home_url": self.public_content.author.home_url, "author_is_local": bool(self.public_content.author.user), "rendered": "<p><strong>Foobar</strong></p>", "humanized_timestamp": self.public_content.humanized_timestamp, "formatted_timestamp": self.public_content.formatted_timestamp, "author_handle": self.public_content.author.handle, "is_author": False, "is_following_author": False, "slug": self.public_content.slug, "update_url": "", "delete_url": "", "reply_url": "", "child_count": 0, "is_authenticated": False, "parent": "", "profile_id": "", }, { "id": self.site_content.id, "guid": self.site_content.guid, "author": self.site_content.author_id, "author_guid": self.site_content.author.guid, "author_image": self.site_content.author.safer_image_url_small, "author_name": self.site_content.author.handle, "author_profile_url": self.site_content.author.get_absolute_url(), "author_home_url": self.site_content.author.home_url, "author_is_local": bool(self.site_content.author.user), "rendered": "<p><em>Foobar</em></p>", "humanized_timestamp": self.site_content.humanized_timestamp, "formatted_timestamp": self.site_content.formatted_timestamp, "author_handle": self.site_content.author.handle, "is_author": False, "is_following_author": False, "slug": self.site_content.slug, "update_url": "", "delete_url": "", "reply_url": "", "child_count": 0, "is_authenticated": False, "parent": "", "profile_id": "", } ])
def handle_load_content(self, data): """Send back the requested content.""" ids = data.get("ids") if ids: contents = Content.get_rendered_contents_for_user( ids, self.message.user) payload = json.dumps({ "event": "content", "contents": contents, }) self.send(payload)
def handle_load_more(self, data): """Load more content to the stream.""" last_id = data.get("last_id") if not last_id: return qs, throughs = self._get_stream_qs(last_id=last_id) contents = Content.get_rendered_contents(qs, self.message.user, throughs=throughs) payload = self.make_payload(contents, "appended") self.send_payload(payload)
def test_get_rendered_contents_for_user(self): user = self.make_user() contents = Content.get_rendered_contents_for_user( [self.public_content.id, self.site_content.id], user) self.assertEqual(contents, [{ "id": self.public_content.id, "author": self.public_content.author_id, "rendered": "<p><strong>Foobar</strong></p>", }, { "id": self.site_content.id, "author": self.site_content.author_id, "rendered": "<p><em>Foobar</em></p>", }])
def dump_content(content): is_user_author = (content.author == request_user_profile) return { "htmlSafe": content.rendered, "author": { "guid": content.author.guid, "name": content.author.name if content.author.name else content.author.handle, "handle": content.author.handle, "imageUrlSmall": content.author.safer_image_url_small, "absoluteUrl": content.author.get_absolute_url(), "homeUrl": content.author.home_url, "isUserFollowingAuthor": (content.author.id in getattr(request_user_profile, "following_ids", [])) }, "id": content.id, "timestamp": content.timestamp, "humanizedTimestamp": content.humanized_timestamp, "edited": bool(content.edited), "repliesCount": content.reply_count, "sharesCount": content.shares_count, "isUserLocal": bool(content.author.user), "contentUrl": content.get_absolute_url(), "isUserAuthor": is_user_author, "hasShared": Content.has_shared(content.id, request_user_profile.id) if request_user_profile else False, }
def test_gets_guid_on_save_with_user(self): content = Content(text="foobar") content.save(author=ProfileFactory()) assert content.guid
def test_get_contents_for_authenticated_other_user(self): user = self.make_user() contents = Content.get_contents_for_user(self.ids, user) self.assertEqual(set(contents), self.set - {self.self_content, self.limited_content})
def get_user_has_shared(self, obj): request = self.context.get("request") if not request: return False return Content.has_shared(obj.id, request.user.profile.id) if hasattr(request.user, "profile") else False
def test_get_contents_for_self(self): user = self.make_user() user.profile = self.self_content.author user.save() contents = Content.get_contents_for_user(self.ids, user) self.assertEqual(set(contents), self.set - {self.limited_content})
def has_shared(content, user): if not hasattr(user, "profile"): return False return Content.has_shared(content.id, user.profile.id)
def test_raises_on_save_without_user(self): content = Content(text="foobar") with transaction.atomic(), self.assertRaises(IntegrityError): content.save()
def test_get_contents_for_unauthenticated_user(self): user = AnonymousUser() contents = Content.get_contents_for_user(self.ids, user) self.assertEqual(set(contents), {self.public_content})
def test_unshare_removes_a_share(self): self.public_content.share(self.local_user.profile) assert Content.has_shared(self.public_content.id, self.local_user.profile.id) self.public_content.unshare(self.local_user.profile) self.assertFalse(Content.has_shared(self.public_content.id, self.local_user.id))
def test_has_shared(self): self.assertFalse(Content.has_shared(self.public_content.id, self.local_user.profile.id)) # Do a share self.public_content.share(self.local_user.profile) self.assertTrue(Content.has_shared(self.public_content.id, self.local_user.profile.id))
def test_content_saved_in_correct_order(self): profile = ProfileFactory(guid="1234") pinned_content_1 = Content(pinned=True, text="foobar") pinned_content_2 = Content(pinned=True, text="foobar") pinned_content_3 = Content(pinned=True, text="foobar") pinned_content_1.save(author=profile) pinned_content_2.save(author=profile) pinned_content_3.save(author=profile) assert [pinned_content_1.order, pinned_content_2.order, pinned_content_3.order] == [1, 2, 3]