Beispiel #1
0
def add_comment_action(item_id, user_id, comment, reply_to_user=0, reply_to_comment=0):
    """
    """
    if reply_to_user == user_id:
        return '不能回复自己'
    item_set = Item.objects.filter(pk=item_id)
    if len(item_set) == 0:
        return 'item does not exists'
    item = item_set[0]
    item.comment_count += 1
    item.save()
    try:
        new_comment = Comment(comment=comment)
        new_comment.item_id = item_id
        new_comment.comment_user_id = user_id
        if reply_to_user > 0:
            new_comment.reply_to_user_id = reply_to_user
            new_comment.reply_to_id = reply_to_comment
        new_comment.save()
    except ValueError:
        return '评论失败'
    
    if reply_to_user > 0:
        # Add Notification
        if reply_to_comment == 0:
            return '评论失败'
        notification = Notification(operation='c')
        notification.item_id = item_id
        notification.user_id = reply_to_user
        notification.related_user_id = user_id
        notification.comment_id = new_comment.pk
        notification.save()
        
        profile = Profile.objects.get(pk=reply_to_user)
        profile.notification_count += 1
        profile.save()
    
    return new_comment
Beispiel #2
0
def create_comment(request):
    """
    User can comment to an item directly or reply to a comment.
    """
    item_id = request.POST['item']
    comment = request.POST['comment']
    reply_to = request.POST['reply_to']
    redirect_to = APP_URL + "comment/list_comments/" + item_id
    if not item_id or not comment or not item_id.isdigit():
        messages.error(request, '评论失败')
        return redirect(redirect_to)
    item_id = int(item_id)
    item = Item.objects.get(pk=item_id)
    if reply_to:
        try:
            reply_to = int(reply_to)
            if reply_to == request.user.pk:
                messages.error(request, '评论失败,不能回复自己')
                return redirect(redirect_to)
            comment = Comment(comment=comment)
            comment.item_id = item_id
            comment.comment_user_id = request.user.pk
            comment.reply_to_id = reply_to
            comment.save()
            item.comment_count += 1
            item.save()
        except ValueError:
            messages.error(request, '评论失败')
            return redirect(redirect_to)
    else:
        comment = Comment(comment=comment)
        comment.item_id = item_id
        comment.comment_user_id = request.user.pk
        comment.save()
        item.comment_count += 1
        item.save()
    messages.error(request, '评论成功')
    return redirect(redirect_to)