Exemple #1
0
    def test_known_transition_should_succeed(self):
        self.assertTrue(can_proceed(self.model.publish))
        self.model.publish()
        self.assertEqual(self.model.state, 'published')

        self.assertTrue(can_proceed(self.model.hide))
        self.model.hide()
        self.assertEqual(self.model.state, 'hidden')
Exemple #2
0
    def test_known_transition_should_succeed(self):
        self.assertTrue(can_proceed(self.model.publish))
        self.model.publish()
        self.assertEqual(self.model.state, 'published')

        self.assertTrue(can_proceed(self.model.stick))
        self.model.stick()
        self.assertEqual(self.model.state, 'sticked')
Exemple #3
0
    def test_known_transition_should_succeed(self):
        self.assertTrue(can_proceed(self.model.publish))
        self.model.publish()
        self.assertEqual(self.model.state, "published")

        self.assertTrue(can_proceed(self.model.hide))
        self.model.hide()
        self.assertEqual(self.model.state, "hidden")
Exemple #4
0
 def test_variable_condition2(self):
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertFalse(can_proceed(self.model.approve, 'anonymous', moderator='root'))
     self.assertTrue(can_proceed(self.model.approve, 'root', moderator='root'))
     self.assertFalse(self.model.approve('anonymous', moderator='root'))
     self.model.approve('root', moderator='root')
     self.assertEqual(self.model.state, 'approved')
Exemple #5
0
 def test_variable_condition(self):
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertFalse(can_proceed(self.model.approve, 'foo'))
     self.assertTrue(can_proceed(self.model.approve, 'bar'))
     self.assertFalse(self.model.approve('foo'))
     self.model.approve('bar')
     self.assertEqual(self.model.state, 'approved')
Exemple #6
0
def mark_ham(request, post_id):
    post = get_object_or_404(Post, pk=post_id)

    try:
        post_status = post.poststatus
    except PostStatus.DoesNotExist:
        post_status = None

    if not (request.user.is_superuser
            or request.user in post.topic.forum.moderators.all()):
        messages.success(request,
                         _("You don't have permission to mark this post."))
        return HttpResponseForbidden(post.get_absolute_url())

    if post_status is None:
        messages.success(
            request,
            _("There was not enough data collected to mark this post."))
        return HttpResponseRedirect(post.get_absolute_url())

    if can_proceed(post_status.mark_ham):
        post_status.mark_ham()
        messages.success(request, _("Post un-marked as spam."))
    else:
        messages.success(
            request,
            _("This post is not in the right state to be un-marked as spam."))
        return HttpResponseRedirect(post.get_absolute_url())

    return HttpResponseRedirect(post_status.post.get_absolute_url())
Exemple #7
0
def mark_spam(request, post_id):
    post = get_object_or_404(Post, pk=post_id)
    topic = post.topic
    forum = post.topic.forum
    is_head = topic.head == post

    try:
        post_status = post.poststatus
    except PostStatus.DoesNotExist:
        post_status = None

    if not (
            request.user.is_superuser or
            request.user in post.topic.forum.moderators.all()):
        messages.success(request, _("You don't have permission to mark this post."))
        return HttpResponseForbidden(post.get_absolute_url())

    if post_status is None:
        messages.success(request, _("There was not enough data collected to mark this post."))
        return HttpResponseRedirect(post.get_absolute_url())

    if can_proceed(post_status.mark_spam):
        post_status.mark_spam()
        messages.success(request, _("Post marked as spam."))
    else:
        messages.success(request, _("This post is not in the right state to be marked as spam."))
        return HttpResponseRedirect(post.get_absolute_url())

    if is_head:
        return HttpResponseRedirect(forum.get_absolute_url())
    return HttpResponseRedirect(topic.get_absolute_url())
Exemple #8
0
 def test_known_transition_should_succeed(self):
     self.assertTrue(can_proceed(self.model.publish))
     self.model.publish()
     self.assertEqual(self.model.state, "published")
     self.assertEqual(self.model.approvement, "new")
     self.assertEqual(self.model.get_available_state_transitions(), [])
     self.assertEqual([t[0] for t in self.model.get_available_approvement_transitions()], ["approved", "declined"])
Exemple #9
0
 def test_known_transition_should_succeed(self):
     self.assertTrue(can_proceed(self.model.publish))
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertEqual(self.model.approvement, 'new')
     self.assertEqual(self.model.get_available_state_transitions(), [])
     self.assertEqual([t[0] for t in self.model.get_available_approvement_transitions()], 
                      ['approved', 'declined'])
Exemple #10
0
 def review(self, certainly_spam=False):
     """
     Process this post, used by the manager and the spam-hammer. The
     ``certainly_spam`` argument is used to force mark as spam/delete the
     post, no matter what status Akismet returns.
     """
     if can_proceed(self.filter_spam):
         self.filter_spam()
     elif can_proceed(self.filter_ham):
         self.filter_ham()
         if certainly_spam:
             self.mark_spam()
     else:
         if certainly_spam:
             self._delete_post()
         logger.warn(
             "Couldn't filter post.", exc_info=True, extra={
                 'post_id': self.post.id, 'content_length': len(self.post.body)})
Exemple #11
0
 def test_transition_filter_ham(self):
     """
     If a post is ham, it can transition to filtered as ham.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status._comment_check = lambda: False
     self.post_status.is_ham = lambda: True
     self.post_status.is_spam = lambda: False
     self.assertTrue(can_proceed(self.post_status.filter_ham))
     self.post_status.filter_ham()
     self.assertEqual(PostStatus.FILTERED_HAM, self.post_status.state)
Exemple #12
0
 def test_transition_filter_ham(self):
     """
     If a post is ham, it can transition to filtered as ham.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status._comment_check = lambda: False
     self.post_status.is_ham = lambda: True
     self.post_status.is_spam = lambda: False
     self.assertTrue(can_proceed(self.post_status.filter_ham))
     self.post_status.filter_ham()
     self.assertEqual(PostStatus.FILTERED_HAM, self.post_status.state)
Exemple #13
0
 def review(self, certainly_spam=False):
     """
     Process this post, used by the manager and the spam-hammer. The
     ``certainly_spam`` argument is used to force mark as spam/delete the
     post, no matter what status Akismet returns.
     """
     if can_proceed(self.filter_spam):
         self.filter_spam()
     elif can_proceed(self.filter_ham):
         self.filter_ham()
         if certainly_spam:
             self.mark_spam()
     else:
         if certainly_spam:
             self._delete_post()
         logger.warn("Couldn't filter post.",
                     exc_info=True,
                     extra={
                         'post_id': self.post.id,
                         'content_length': len(self.post.body)
                     })
Exemple #14
0
 def test_transition_filter_spam(self):
     """
     If a post is spam, it can transition to filtered as spam.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status._comment_check = lambda: True
     self.post_status.is_ham = lambda: False
     self.post_status.is_spam = lambda: True
     self.assertTrue(can_proceed(self.post_status.filter_spam))
     self.post_status.filter_spam()
     self.assertEqual(PostStatus.FILTERED_SPAM, self.post_status.state)
     self.assertEqual(
         self.post_status.post.topic.forum.name, forum_settings.SPAM_FORUM_NAME)
Exemple #15
0
 def test_transition_filter_spam(self):
     """
     If a post is spam, it can transition to filtered as spam.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status._comment_check = lambda: True
     self.post_status.is_ham = lambda: False
     self.post_status.is_spam = lambda: True
     self.assertTrue(can_proceed(self.post_status.filter_spam))
     self.post_status.filter_spam()
     self.assertEqual(PostStatus.FILTERED_SPAM, self.post_status.state)
     self.assertEqual(self.post_status.post.topic.forum.name,
                      forum_settings.SPAM_FORUM_NAME)
Exemple #16
0
 def test_transition_mark_spam(self):
     """
     A post filtered as ham should be able to be marked as spam, and is moved
     to the spam forum/topic when marked.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status.state = PostStatus.FILTERED_HAM
     self.post_status.save()
     self.post_status._submit_spam = lambda: True
     self.post_status._submit_ham = lambda: True
     self.assertTrue(can_proceed(self.post_status.mark_spam))
     self.post_status.mark_spam()
     self.assertEqual(PostStatus.MARKED_SPAM, self.post_status.state)
     self.assertEqual(self.post_status.post.topic.forum.name,
                      forum_settings.SPAM_FORUM_NAME)
Exemple #17
0
 def test_transition_mark_spam(self):
     """
     A post filtered as ham should be able to be marked as spam, and is moved
     to the spam forum/topic when marked.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status.state = PostStatus.FILTERED_HAM
     self.post_status.save()
     self.post_status._submit_spam = lambda: True
     self.post_status._submit_ham = lambda: True
     self.assertTrue(can_proceed(self.post_status.mark_spam))
     self.post_status.mark_spam()
     self.assertEqual(PostStatus.MARKED_SPAM, self.post_status.state)
     self.assertEqual(
         self.post_status.post.topic.forum.name, forum_settings.SPAM_FORUM_NAME)
Exemple #18
0
 def test_transition_mark_ham(self):
     """
     Ensure a post filtered as spam can be marked as ham, and moved back to
     its original location after marking as ham.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status.state = PostStatus.FILTERED_SPAM
     self.post_status.save()
     self.post_status._delete_post()
     self.post_status._submit_spam = lambda: True
     self.post_status._submit_ham = lambda: True
     self.assertTrue(can_proceed(self.post_status.mark_ham))
     self.post_status.mark_ham()
     self.assertEqual(PostStatus.MARKED_HAM, self.post_status.state)
     self.assertEqual(self.post_status.post.topic.name, "Test Topic")
     self.assertEqual(self.post_status.post.topic.forum.name, "Test Forum")
Exemple #19
0
 def test_transition_mark_ham(self):
     """
     Ensure a post filtered as spam can be marked as ham, and moved back to
     its original location after marking as ham.
     """
     self.post_status = PostStatus.objects.create_for_post(self.test_post)
     self.post_status.state = PostStatus.FILTERED_SPAM
     self.post_status.save()
     self.post_status._delete_post()
     self.post_status._submit_spam = lambda: True
     self.post_status._submit_ham = lambda: True
     self.assertTrue(can_proceed(self.post_status.mark_ham))
     self.post_status.mark_ham()
     self.assertEqual(PostStatus.MARKED_HAM, self.post_status.state)
     self.assertEqual(self.post_status.post.topic.name, "Test Topic")
     self.assertEqual(self.post_status.post.topic.forum.name, "Test Forum")
Exemple #20
0
 def test_star_shortcut_succeed(self):
     self.assertTrue(can_proceed(self.model.moderate))
     self.model.moderate()
     self.assertEqual(self.model.state, "moderated")
Exemple #21
0
 def test_state_non_changed_after_fail(self):
     self.assertTrue(can_proceed(self.model.remove))
     self.assertRaises(Exception, self.model.remove)
     self.assertEqual(self.model.state, "new")
Exemple #22
0
 def test_unknow_transition_fails(self):
     self.assertFalse(can_proceed(self.model.hide))
     self.assertRaises(TransitionNotAllowed, self.model.hide)
Exemple #23
0
 def test_func_without_args_kwargs(self):
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertTrue(can_proceed(self.model.mark_spam))
     self.model.mark_spam()
     self.assertEqual(self.model.state, 'spam')
Exemple #24
0
 def test_allowed_null_transition_should_succeed(self):
     self.assertTrue(can_proceed(self.model.publish))
     self.model.publish()
     self.model.notify_all()
     self.assertEqual(self.model.state, 'published')
Exemple #25
0
 def test_unmet_condition(self):
     self.model.publish()
     self.assertEqual(self.model.state, "published")
     self.assertFalse(can_proceed(self.model.destroy))
     self.assertRaises(TransitionNotAllowed, self.model.destroy)
Exemple #26
0
 def test_unknow_transition_fails(self):
     self.assertFalse(can_proceed(self.model.hide))
     self.assertRaises(NotImplementedError, self.model.hide)
Exemple #27
0
 def test_star_shortcut_succeed(self):
     self.assertTrue(can_proceed(self.model.moderate))
     self.model.moderate()
     self.assertEqual(self.model.state, 'moderated')
Exemple #28
0
 def test_state_non_changed_after_fail(self):
     self.assertTrue(can_proceed(self.model.remove))
     self.assertRaises(Exception, self.model.remove)
     self.assertEqual(self.model.state, 'new')
Exemple #29
0
 def test_unknow_transition_fails(self):
     self.assertFalse(can_proceed(self.model.hide))
     self.assertRaises(TransitionNotAllowed, self.model.hide)
Exemple #30
0
 def test_allowed_null_transition_should_succeed(self):
     self.assertTrue(can_proceed(self.model.publish))
     self.model.publish()
     self.model.notify_all()
     self.assertEqual(self.model.state, 'published')
Exemple #31
0
 def test_unmet_condition(self):
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertFalse(can_proceed(self.model.destroy))
     self.assertRaises(TransitionNotAllowed, self.model.destroy)
Exemple #32
0
 def test_unmet_condition(self):
     self.model.publish()
     self.assertEqual(self.model.state, 'published')
     self.assertFalse(can_proceed(self.model.destroy))
     self.assertFalse(self.model.destroy())