Example #1
0
def add(db_session, data, username):
    logger.info(LogMsg.START, username)

    schema_validate(data, COMMENT_ADD_SCHEMA_PATH)
    logger.debug(LogMsg.SCHEMA_CHECKED)

    book_id = data.get('book_id')

    logger.debug(LogMsg.COMMENT_VALIDATING_BOOK, book_id)
    book = get_book(book_id, db_session)
    if book is None:
        logger.error(LogMsg.NOT_FOUND, {'book_id': book_id})
        raise Http_error(404, Message.NOT_FOUND)

    logger.debug(LogMsg.CHECK_USER_EXISTANCE, username)
    user = check_user(username, db_session)
    if user is None:
        logger.error(LogMsg.INVALID_USER, username)
        raise Http_error(400, Message.INVALID_USER)

    if user.person_id is None:
        logger.error(LogMsg.USER_HAS_NO_PERSON, username)
        raise Http_error(400, Message.Invalid_persons)

    validate_person(user.person_id, db_session)
    logger.debug(LogMsg.PERSON_EXISTS)

    logger.debug(LogMsg.COMMENT_CHECK_FOR_PARENT)
    parent_id = data.get('parent_id', None)
    if parent_id:
        logger.debug(LogMsg.COMMENT_GETTING_PARENT, parent_id)
        parent_comment = get_comment(parent_id, db_session)
        if parent_comment is None:
            logger.error(LogMsg.COMMENT_PARENT_NOT_FOUND, parent_id)
            raise Http_error(404, Message.PARENT_NOT_FOUND)
        if parent_comment.book_id != book_id:
            logger.error(
                LogMsg.COMMENT_PARENT_NOT_MATCH, {
                    'book_id': book_id,
                    'parent': comment_to_dict(db_session, parent_comment)
                })
            raise Http_error(400, Message.ACCESS_DENIED)

    model_instance = Comment()
    logger.debug(LogMsg.POPULATING_BASIC_DATA)
    populate_basic_data(model_instance, username, data.get('tags'))
    model_instance.person_id = user.person_id
    model_instance.book_id = book_id
    model_instance.body = data.get('body')
    model_instance.parent_id = data.get('parent_id')

    logger.debug(LogMsg.DATA_ADDITION)

    db_session.add(model_instance)
    logger.debug(LogMsg.DB_ADD)

    logger.info(LogMsg.END)

    return comment_to_dict(db_session, model_instance, username)
Example #2
0
    def post(self, request, post_id):
        comment = Comment()  # 实例化类
        comment.post_id = post_id
        comment.parent_id = request.POST['parent_id']
        comment.reply_id = request.POST['reply_id']
        comment.nick = request.POST['nick']
        comment.mail = request.POST['mail']
        comment.content = request.POST['content']

        ua = parse_user_agent(request.META.get('HTTP_USER_AGENT',
                                               ''))  # 解析HTTP_USER_AGENT
        comment.browser = ua['browser']
        comment.client = ua['client']

        # 处理回复评论
        if request.POST['reply_id'] != '0':
            comment.comment_type = 'reply'
            reply_comment = Comment.objects.filter(
                id=request.POST['reply_id']).first()
            if reply_comment:
                comment.to_nick = reply_comment.nick
                comment.to_mail = reply_comment.mail

                # 如果是回复评论,则发送邮件通知相关评论人
                recipient_list = EMAIL_RECEIVE_LIST + [reply_comment.mail]
            else:
                recipient_list = None
        else:
            # 如果是新的评论内容,则只需要发送通知博客作者
            recipient_list = EMAIL_RECEIVE_LIST

        comment.save()  # 保存评论数据到数据库

        redirect_url = request.POST['redirect_url'] + '#comment-{0}'.format(
            comment.id)
        if recipient_list:  # 发送邮件
            try:
                send_email(url=redirect_url,
                           recipient_list=recipient_list,
                           post_id=post_id)
            except BaseException as e:
                print('发送邮件错误: {}'.format(e))
        return redirect(redirect_url)  # 重定向到指定页面