Exemplo n.º 1
0
 def test_comment_vote_prune(self):
     c = db.Comment(article=self.article,
                    user=self.blue_user,
                    content='nice test')
     c.save()
     c.upvote(self.green_user)
     db.CommentVote.prune()
Exemplo n.º 2
0
 def test_comment_delete(self):
     c = db.Comment(article=self.article,
                    user=self.blue_user,
                    content='nice test')
     c.save()
     assert c.is_deleted == False
     c.delete()
     assert c.is_deleted == True
Exemplo n.º 3
0
 def test_comment(self):
     c = db.Comment(article=self.article,
                    user=self.blue_user,
                    content='nice test')
     c.save()
     assert str(c) != ''
     assert c.get_absolute_url() != ''
     assert c.get_forum_url() != ''
     assert len(c.as_dict().keys()) > 0
Exemplo n.º 4
0
    def test_article_comments_as_user(self):
        a = db.Article(author=self.red_user,
                       title='test title',
                       slug='test',
                       published=datetime.now(),
                       content='this is a test')
        a.save()

        # add comment as self.blue_user
        c = db.Comment(article=a, user=self.blue_user, content='nice test')
        c.save()
Exemplo n.º 5
0
 def test_comment_up_down_vote(self):
     c = db.Comment(article=self.article,
                    user=self.blue_user,
                    content='nice test')
     c.save()
     c.upvote(self.green_user)
     assert c.karma == 1
     assert c.ups == 1
     c.downvote(self.red_user)
     assert c.karma == 0
     assert c.downs == 1
     c.karma = 100
     c.save()
     db.Comment.recalculate()
     c = db.Comment.objects.get(content='nice test')
     assert c.karma == 0
Exemplo n.º 6
0
def comment_new(user, article_slug, parent_id, content, **kwargs):
    """Leave a comment on an article

    If parent_id is set, this will be a reply to an existing comment.

    Also upvotes comment and increments article comment count.
    """
    if not (user and user.id):
        raise APIException(_("you're not logged in"))
    content = content.strip()
    try:
        article = db.Article.objects.get(slug=article_slug, is_deleted=False)
    except db.Article.DoesNotExist:
        raise APIException(_('article not found'))

    def comment_depth(id):
        depth = 0
        current = id
        while current:
            current = comhash.get(current, 0).parent_id
            depth += 1
        return depth

    if parent_id:
        other_comments = (db.Comment.objects.select_related("article").filter(
            article=article, is_deleted=False))
        comhash = dict((c.id, c) for c in other_comments)
        parent = comhash[int(parent_id)]
        if int(parent_id) not in comhash:
            raise APIException(_("parent comment not found"))
        if comment_depth(int(parent_id)) + 1 > settings.OWS_MAX_COMMENT_DEPTH:
            raise APIException(_("comment nested too deep"))
    else:
        parent = None
        parent_id = None

    if not settings.DEBUG and not user.is_staff:
        last = user.comment_set.order_by('-published')[:1]
        if last:
            _limiter(last[0].published, settings.OWS_LIMIT_COMMENT)
        ip = _try_to_get_ip(kwargs)
        if ip:
            last = cache.get('api_comment_new_' + ip)
            if last:
                _limiter(last, settings.OWS_LIMIT_COMMENT)

    comment = db.Comment()
    comment.article = article
    username = user.username
    comment.user = user
    comment.content = content
    comment.parent_id = parent_id
    comment.ip = _try_to_get_ip(kwargs)
    _check_post(user, comment)
    comment.save()
    comment_vote(user, comment, "up", **kwargs)
    if not comment.is_removed:
        article.comment_count += 1
        article.killed = now()
    article.save()
    if not comment.is_removed:
        if parent:
            db.Notification.send(
                parent.user, comment.get_absolute_url(),
                '%s replied to your comment: %s' %
                (username, synopsis(parent.content, 7)))
        else:
            db.Notification.send(
                article.author, comment.get_absolute_url(),
                '%s replied to your post: %s' %
                (username, synopsis(article.content, 7)))
    return comment_get(user, comment.id)