示例#1
0
def delete_document_by_id(document_id):
    document = get_document_by_id(document_id)

    # cannot delete system files and folders
    if document.is_system:
        raise flash.error(
            _("Cannot edit or delete system files and folders."))

    # Collapse positions
    collapse_tree_siblings("documents", document)

    # Delete branch recursively
    return delete_tree_branch("documents", document, func=delete_document_file)
示例#2
0
def delete_block_by_id(block_id, delete_system=False):
    """Deletes block and returns deleted block."""

    block = get_block_by_id(block_id)

    # Cannot delete system blocks
    if block.is_system and not delete_system:
        raise flash.error(
            _("Cannot edit or delete system blocks."))

    # TODO: wrap the code below in transaction
    if block.parent_id:
        # Collapse positions of the blocks after deleted block
        collapse_tree_siblings("blocks", block)

    return delete_tree_branch("blocks", block)
示例#3
0
def update_document_by_id(document_id, data):

    document = get_document_by_id(document_id)

    # Cannot edit system files and folders
    if document.is_system:
        raise flash.error(
            _("Cannot edit or delete system files and folders."))

    parent = get_document_by_id(data.parent_id)

    # TODO: custom input field that returns integer value
    data.update(
        ids=(parent.ids or "") + "," + str(parent.id),
        level=parent.level + 1,
        position=int(data.position),
        parent_id=parent.id,
        updated_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
    )

    with db.transaction():

        # Transact changes to positions
        if (data.position != document.position or
                data.parent_id != document.parent_id):

            # Collapse positions for the removed document
            collapse_tree_siblings("documents", document)

            # Shift positions to free the space to insert document
            expand_tree_siblings("documents", data)

        # Cannot change documents type and upload
        del data["type"]
        del data["upload"]

        db.update(
            "documents",
            where="id = $document_id",
            vars=locals(),
            **data)

    # Update document with data
    # TODO: fix updated_at
    document.update(data)

    return document
示例#4
0
def create_document(document):
    """Creates new document and saves upload"""

    parent = get_document_by_id(document.parent_id)

    document.update(
        ids=(parent.ids or "") + "," + str(parent.id),
        level=parent.level + 1,
        parent_id=int(document.parent_id),
        created_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
        user_id=auth.get_user().id,
        is_published=True,  # True for the new documents
    )

    if document.type == "folder":
        del document["upload"]
        if not document.title:
            document.title = _("Untitled Folder")
    else:
        upload = document.pop("upload")
        try:
            mimetype, encoding = mimetypes.guess_type(upload.filename)
            filename, filesize = save_document(upload.file)
            title, extension = os.path.splitext(upload.filename)
            document.update(
                title=document.title or title,
                filename=filename,
                extension=extension.lower(),
                mimetype=mimetype,
                type="image" if "image" in mimetype else "document",
                filesize=filesize,
            )
        except:
            raise flash.error(_("File upload error."))

    if document.position:
        document.position = int(document.position)
        # Shift positions to free the space to insert document
        expand_tree_siblings("documents", document)
    else:
        document.position = get_last_position("documents", document.parent_id)

    document.id = db.insert("documents", **document)

    # TODO: fix created_at
    return document
示例#5
0
def update_block_by_id(block_id, data):

    block = get_block_by_id(block_id)

    # Cannot edit or delete system blocks
    if block.is_system:
        raise flash.error(
            _("Cannot edit or delete system blocks."))

    data.updated_at = web.SQLLiteral("CURRENT_TIMESTAMP")

    # Cannot change type and template of block
    del data["template"]
    del data["type"]

    if block.type == "wysiwyg":
        data.content_cached = smarty(sanitize(data.content))
    else:
        data.content_cached = smarty(data.content)

    # Get column sizes from data
    sizes = data.pop("sizes")

    # Don't change block position, it may be wrong
    # TODO: ensure correct position from the server
    position = data.pop("position")

    # TODO: wrap the code below in transaction
    db.update(
        "blocks",
        where="$block_id = id",
        vars=locals(),
        **data)

    # Update block with data
    # TODO: fix updated_at
    block.update(data)

    # Create columns for row block
    if block.template == "row" and sizes:
        block.orphans = update_columns(block, sizes)

    return block