Skip to content

Commit

Permalink
For #73 🐛 fix display not public post
Browse files Browse the repository at this point in the history
  • Loading branch information
lissa3 committed Nov 12, 2023
1 parent f522c5a commit 42019f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/posts/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,36 @@ def test_no_results_en(self):
self.assertEqual(len(posts), 0)


class PostDetailCommentsTestCase(TestCase):
@override_settings(LANGUAGE_CODE="en", LANGUAGES=(("en", "English"),))
class PostDetailAndCommentsTestCase(TestCase):
def setUp(self) -> None:
self.post_1 = PostFactory(status=Post.CurrentStatus.PUB.value)
self.post_2 = PostFactory(status=Post.CurrentStatus.PUB.value)
self.post_3 = PostFactory(status=Post.CurrentStatus.DRAFT.value)
self.user = UserFactory(username="tissa")
self.comment_1 = CommentFactory(body="foo", post=self.post_1, user=self.user)
self.comment_2 = CommentFactory(
body="tired cat", post=self.post_1, user=self.user
)
self.comment_3 = CommentFactory(body="no fun", post=self.post_2, user=self.user)

@override_settings(LANGUAGE_CODE="en", LANGUAGES=(("en", "English"),))
def test_fail_to_get_draft_posts(self):
"""no detail view for not public post"""
path = reverse("posts:post_detail", kwargs={"slug": self.post_3.slug})

resp = self.client.get(path)

self.assertEqual(resp.status_code, 404)

def test_success_get_public_post(self):
"""detail view for public post"""
path = reverse("posts:post_detail", kwargs={"slug": self.post_1.slug})

resp = self.client.get(path)

self.assertEqual(resp.status_code, 200)
self.assertTrue(resp.context_data["post"])

def test_count_posts_comments(self):
"""comments for a given post;"""
path = reverse("posts:post_detail", kwargs={"slug": self.post_1.slug})
Expand Down
5 changes: 5 additions & 0 deletions src/posts/views/post_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class PostDetail(CategoryCrumbMixin, DetailView):
template_name = "posts/post_detail.html"
_thread_uuid = None

def get_object(self, queryset=None):
return get_object_or_404(
Post.objects.get_public(), slug=self.kwargs.get("slug")
)

def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
# print("full path is",self.request.get_full_path())
Expand Down

0 comments on commit 42019f9

Please sign in to comment.