def test_valid_post_form(self): profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = {"text": "Test Post Text"} form = PostForm(topic, profile.user, data=data) self.assertTrue(form.is_valid())
def test_invalid_post_form_empty_text(self): """Test when text contains only whitespace""" profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = {"text": " "} form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def test_invalid_post_form_missing_text(self): """Test when text is missing""" profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = {} form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def test_valid_post_form(self): profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = { 'text': 'Test Post Text' } form = PostForm(topic, profile.user, data=data) self.assertTrue(form.is_valid())
def test_invalid_post_form_missing_text(self): """ Test when text is missing """ profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = { } form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def test_invalid_post_form_text_too_long(self): """Test when text runs over the length limit""" profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = { "text": text_too_long, } form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def test_invalid_post_form_text_too_long(self): """ Test when text runs over the length limit """ profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = { 'text': text_too_long, } form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def test_invalid_post_form_empty_text(self): """ Test when text contains only whitespace """ profile = ProfileFactory() _, forum = create_category_and_forum() topic = create_topic_in_forum(forum, profile) data = { 'text': ' ' } form = PostForm(topic, profile.user, data=data) self.assertFalse(form.is_valid())
def get_context_data(self, **kwargs): context = super(TopicPostsListView, self).get_context_data(**kwargs) form = PostForm(self.object, self.request.user) form.helper.form_action = reverse('post-new') + "?sujet=" + str( self.object.pk) context.update({ 'topic': self.object, 'posts': self.build_list_with_previous_item(context['object_list']), 'last_post_pk': self.object.last_message.pk, 'form': form, 'form_move': MoveTopicForm(topic=self.object), }) reaction_ids = [post.pk for post in context['posts']] context["user_dislike"] = CommentDislike.objects\ .select_related('comment')\ .filter(user__pk=self.request.user.pk, comments__pk__in=reaction_ids)\ .values_list('pk', flat=True) context["user_like"] = CommentLike.objects\ .select_related('comment')\ .filter(user__pk=self.request.user.pk, comments__pk__in=reaction_ids)\ .values_list('pk', flat=True) context["is_staff"] = self.request.user.has_perm('forum.change_topic') if self.request.user.is_authenticated(): if never_read(self.object): mark_read(self.object) return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) form = PostForm(self.object, self.request.user) form.helper.form_action = reverse("post-new") + "?sujet=" + str( self.object.pk) posts = self.build_list_with_previous_item(context["object_list"]) context.update({ "topic": self.object, "posts": posts, "last_post_pk": self.object.last_message.pk, "form": form, "form_move": MoveTopicForm(topic=self.object), }) votes = CommentVote.objects.filter(user_id=self.request.user.pk, comment__in=context["posts"]).all() context["user_like"] = [ vote.comment_id for vote in votes if vote.positive ] context["user_dislike"] = [ vote.comment_id for vote in votes if not vote.positive ] context["is_staff"] = self.request.user.has_perm("forum.change_topic") context["is_antispam"] = self.object.antispam() context[ "subscriber_count"] = TopicAnswerSubscription.objects.get_subscriptions( self.object).count() if hasattr(self.request.user, "profile"): context["is_dev"] = self.request.user.profile.is_dev() context["has_token"] = self.request.user.profile.github_token != "" context["repositories"] = settings.ZDS_APP["github_projects"][ "repositories"] if self.request.user.has_perm("forum.change_topic"): context["user_can_modify"] = [post.pk for post in context["posts"]] else: context["user_can_modify"] = [ post.pk for post in context["posts"] if post.author == self.request.user ] if self.request.user.is_authenticated: if len(posts) > 0: signals.post_read.send(sender=posts[0].__class__, instances=posts, user=self.request.user) if not self.object.is_read: mark_read(self.object) return context
def get_context_data(self, **kwargs): context = super(TopicPostsListView, self).get_context_data(**kwargs) form = PostForm(self.object, self.request.user) form.helper.form_action = reverse('post-new') + '?sujet=' + str( self.object.pk) posts = self.build_list_with_previous_item(context['object_list']) context.update({ 'topic': self.object, 'posts': posts, 'last_post_pk': self.object.last_message.pk, 'form': form, 'form_move': MoveTopicForm(topic=self.object), }) votes = CommentVote.objects.filter(user_id=self.request.user.pk, comment__in=context['posts']).all() context['user_like'] = [ vote.comment_id for vote in votes if vote.positive ] context['user_dislike'] = [ vote.comment_id for vote in votes if not vote.positive ] context['is_staff'] = self.request.user.has_perm('forum.change_topic') context['isantispam'] = self.object.antispam() context[ 'subscriber_count'] = TopicAnswerSubscription.objects.get_subscriptions( self.object).count() if hasattr(self.request.user, 'profile'): context['is_dev'] = self.request.user.profile.is_dev() context['tags'] = settings.ZDS_APP['site']['repository']['tags'] context['has_token'] = self.request.user.profile.github_token != '' if self.request.user.has_perm('forum.change_topic'): context['user_can_modify'] = [post.pk for post in context['posts']] else: context['user_can_modify'] = [ post.pk for post in context['posts'] if post.author == self.request.user ] if self.request.user.is_authenticated(): for post in posts: signals.content_read.send(sender=post.__class__, instance=post, user=self.request.user) if not is_read(self.object): mark_read(self.object) return context