append.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:rvo 作者: noqqe 项目源码 文件源码
def append(ctx, docid, password, content):
    """
    Append string to a document

    Trys to find the object, (decrypt,) adds content to the bottom,
    (encrypt,) update date fields and regenerates the title.

    :docid: str (bson object)
    :returns: bool
    """

    coll = db.get_document_collection(ctx)
    doc, docid = db.get_document_by_id(ctx, docid)

    template, c = db.get_content(ctx, doc, password=password)

    d = datetime.datetime.now()

    if not content:
        content = ""
        for l in click.get_text_stream('stdin'):
                content = content + l

    content = template + content

    if isinstance(content, unicode):
        content = content.encode("utf-8")

    if doc["encrypted"] is True:
        title = utils.get_title_from_content(content)
        content = c.encrypt_content(content)
    else:
        if not "links" in doc["categories"]:
            title = utils.get_title_from_content(content)


    if content != template:
        doc["content"] = content
        doc["title"] = title
        doc["updated"] = d
        if validate(doc):
            coll.save(doc)
            transaction.log(ctx, docid, "append", title)
            utils.log_info("Content appended to \"%s\"." % title)
        else:
            utils.log_error("Validation of the updated object did not succeed")
    else:
        utils.log_info("No changes detected for \"%s\"" % title)

    return True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号