Пример #1
0
def update_page_by_id(page_id, data):

    page = get_page_by_id(page_id)

    # Cannot change page type
    del data["type"]

    if page.is_system:
        # Cannot edit slug of the system page
        del data["slug"]
        # position can be changed, but not parent_id
        data.parent_id = page.parent_id
        data.path = page.path
    else:
        data.parent_id = int(data.parent_id)
        data.update(unique_path(data, page_id))

    # if position specified and parent has not changed
    if data.position and data.parent_id == page.parent_id:
        data.position = int(data.position)
    else:
        data.position = get_last_position("pages", data.parent_id)

    data.update(
        description_cached=smarty(sanitize(data.description)),
        updated_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
    )

    # Set published_at to NOW
    if data.published_at is None:
        data.published_at = web.SQLLiteral("CURRENT_TIMESTAMP")

    with db.transaction():

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

            # Collapse positions for the removed page
            collapse_tree_siblings("pages", page)

            # Shift positions to free the space to insert page
            expand_tree_siblings("pages", data)

        db.update(
            "pages",
            where="id = $page_id",
            vars=locals(),
            **data)

        # TODO: expand this for tree
        if data.path != page.path:
            update_branch(page.id)

    page.update(data)
    return page
Пример #2
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
Пример #3
0
def create_block(block):
    """Creates block from passed dict and returns it."""

    block.update(
        position=int(block.position),
        created_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
        user_id=auth.get_user().id,
        is_published=True,
    )

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

    # TODO: wrap the code below in transaction
    if block.get("parent_id"):
        parent = get_block_by_id(block.parent_id)
        if parent.ids:
            parent_blocks = (parent.ids or "") + "," + str(parent.id)
        else:
            parent_blocks = str(parent.id)
        block.update(
            page_id=parent.page_id,
            ids=parent_blocks,
            level=parent.level + 1,
        )
        # Shift blocks positions to free the place for new block
        expand_tree_siblings("blocks", block)

    else:
        block.level = 0

    sizes = block.pop("sizes")
    block.id = db.insert("blocks", **block)

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

    # TODO: fix created_at
    return block
Пример #4
0
def create_page(page):
    """Creates new page and pageblock"""

    page.update(unique_path(page))

    page.update(
        user_id=auth.get_user().id,
        created_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
        description_cached=smarty(sanitize(page.description)),
    )

    # Set published_at to NOW
    if page.published_at is None:
        page.published_at = web.SQLLiteral("CURRENT_TIMESTAMP")

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

    page.id = db.insert("pages", **page)

    # Generate pages initial blocks stucture
    page_block = config.page_types[page.type]["block"]
    create_tree_branch(
        "blocks",
        page_block,
        page_id=page.id,
        user_id=auth.get_user().id,
        is_published=True,
        created_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
        published_at=web.SQLLiteral("CURRENT_TIMESTAMP"),
    )

    return page