Esempio n. 1
0
def articles_comment_internal(req, uri=None, id=None):
    if not uri and not id:
        raise SERVER_RETURN(state.HTTP_NOT_FOUND)

    article = Article(id)
    article.uri = uri
    if uri and not article.get(req, key='uri'):
        raise SERVER_RETURN(state.HTTP_NOT_FOUND)
    if id and not article.get(req):
        raise SERVER_RETURN(state.HTTP_NOT_FOUND)

    comment = ArticleComment()
    comment.bind(req.form, user_agent=req.user_agent, ip=req.remote_addr)

    robot = True if req.form.getfirst("robot", "", str) else False
    qid = int(req.form.getfirst("qid", '0', str), 16)
    question, answer = robot_questions[qid]
    check = req.form.getfirst("answer", "", str) == answer

    if robot or not check:
        rv = RobotError(comment=comment, check=check)
        return (article, rv)

    if req.login:
        comment.author_id = req.login.id
    rv = comment.add(req, parent=req.form.getfirst('parent', '', str))
    return (article, rv)
Esempio n. 2
0
def articles_detail_internal(req, article, **kwargs):
    if article.format == FORMAT_RST:
        article.perex = rst2html(article.perex)
        article.body = rst2html(article.body)

    if article.data.get('discussion', True):
        discussion = ArticleComment.list(req, article.id, Pager(limit=-1))
    else:
        discussion = []

    qid = randint(0, len(robot_questions)-1)
    question, answer = robot_questions[qid]
    return generate_page(req, "articles_detail.html", article=article,
                         discussion=discussion,
                         question=question,  answer=answer, qid=hex(qid),
                         staticmenu=req.cfg.get_static_menu(req),
                         styles=('tiny-writer',), **kwargs)
Esempio n. 3
0
def articles_comment_xhr(req, arg):
    id = arg if isinstance(arg, int) else None
    uri = arg if isinstance(arg, unicode) else None

    article, rv = articles_comment_internal(req, uri, id)
    # XXX: at now, isinstance not work, becouase, have another namespace
    # that rv....
    # if hasattr(rv, 'reason'):
    if isinstance(rv, ErrorValue):
        req.status = state.HTTP_BAD_REQUEST
        return send_json(req, rv, cls=ObjectEncoder)
    elif rv is None:
        req.status = state.HTTP_INTERNAL_SERVER_ERROR
        return send_json(req, {'reason': 'integrity_error'})

    return send_json(req, {'discussion': ArticleComment.list(req,
                           article.id, Pager(limit=-1)), 'last': rv.id},
                     cls=ObjectEncoder)