def patched_base_post_serializer(mocker): """Fixture that patches the base serializer class for ESPostSerializer""" article_content = {"text": "hello world"} base_serialized_data = { "author_id": 1, "author_name": "Author Name", "author_headline": "Author Headline", "article_content": article_content, "plain_text": render_article_text(article_content), "profile_image": "/media/profile/1/208c7d959608417eb13bc87392cb5f77-2018-09-21T163449_small.jpg", "channel_title": "channel 1", "channel_name": "channel_1", "channel_type": "restricted", "post_type": LINK_TYPE_SELF, "text": "Post Text", "score": 1, "created": 123, "num_comments": 0, "removed": False, "deleted": False, "id": 1, "title": "post_title", "url": None, "thumbnail": None, "slug": "post-title", } yield mocker.patch( "search.serializers.ESPostSerializer.base_serializer", return_value=mocker.Mock(data=base_serialized_data, _get_user=mocker.Mock()), )
def get_plain_text(self, instance): """Return plain text content""" if instance.article is not None: return render_article_text(instance.article.content) elif instance.is_self: return markdown_to_plain_text(instance.selftext) else: return instance.preview_text
def plain_text(self): """Returns a plaintext represention of the post""" if getattr(self, "article", None) is not None: return render_article_text(self.article.content) elif self.post_type == LINK_TYPE_SELF: return markdown_to_plain_text(self.text) elif self.post_type == LINK_TYPE_LINK: return self.preview_text return None
def default_post_response_data(channel, post, user): """ Helper function. Returns a dict containing some of the data that we expect from the API given a channel, post, and user. """ # For some reason, the default values are different for staff and non-staff users if user.is_staff: user_dependent_defaults = {"upvoted": False, "num_reports": 0} else: user_dependent_defaults = {"upvoted": True, "num_reports": None} post_obj = Post.objects.get(post_id=post.id) article = Article.objects.filter(post=post_obj).first() text = post.text if not text and not post.url: text = "" if article: plain_text = render_article_text(article.content) elif text: plain_text = markdown_to_plain_text(text) else: plain_text = None return { "url": post.url, "url_domain": urlparse(post.url).hostname if post.url else None, "cover_image": None, "thumbnail": None, "text": text, "article_content": article.content if article is not None else None, "plain_text": plain_text, "post_type": post_obj.post_type, "title": post.title, "removed": False, "deleted": False, "subscribed": False, "score": 1, "author_id": user.username, "id": post.id, "slug": get_reddit_slug(post.permalink), "created": post.created, "num_comments": 0, "channel_name": channel.name, "channel_title": channel.title, "channel_type": channel.channel_type, "profile_image": image_uri(user.profile), "author_name": user.profile.name, "author_headline": user.profile.headline, "edited": False, "stickied": False, **user_dependent_defaults, }
def test_update_post_text(mocker, reddit_submission_obj): """ Test that update_post_text calls the indexing task with the right parameters """ patched_task = mocker.patch("search.task_helpers.update_document_with_partial") update_post_text(reddit_submission_obj) assert patched_task.delay.called is True assert patched_task.delay.call_args[0] == ( gen_post_id(reddit_submission_obj.id), { "text": reddit_submission_obj.selftext, "plain_text": render_article_text(reddit_submission_obj.article_content), }, POST_TYPE, )
def update_post_text(post_obj): """ Serializes post object text and runs a task to update the text for the associated ES document. Args: post_obj (praw.models.reddit.submission.Submission): A PRAW post ('submission') object """ update_document_with_partial.delay( gen_post_id(post_obj.id), { "plain_text": render_article_text(post_obj.article_content), "text": post_obj.selftext, }, POST_TYPE, )
def reddit_submission_obj(): """A dummy Post object""" article_content = {"text": "some text"} return SimpleNamespace( author=SimpleNamespace(name="testuser"), article_content=article_content, plain_text=render_article_text(article_content), subreddit=SimpleNamespace( display_name="channel_1", title="Channel", subreddit_type="public" ), selftext="Body text", score=1, created=12345, id="a", title="Post Title", num_comments=1, is_self=True, likes=1, banned_by=None, edited=False, permalink="http://reddit.local/r/channel_1/a/post-title", )
def test_render_article_text(input_node, output_text): """render_article_text should extract the text from any arbitrary article node tree""" assert render_article_text(input_node) == output_text