Example #1
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 = datetime.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)
Example #2
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)
Example #3
0
 def item_title(self, comment):
     return escape(synopsis(comment.content))
Example #4
0
 def item_title(self, comment):
     return escape(synopsis(comment.content))