Example #1
0
def unique_path(page, page_id=None):
    """Makes unique_path for page, returns new path and slug.
       Provided @page_id means do not check against self"""
    slug = slugify(page.slug or page.name)[0:PAGE_SLUG_LENGTH]
    parent_page = db.select("pages", page, where="id=$parent_id")[0]
    test_slug, i = slug, 1
    try:
        while True:
            if not test_slug in config.reserved:
                new_path = join_path(parent_page.path, test_slug)
                test = db.select(
                    "pages",
                    locals(),
                    where=("path=$new_path" +
                           web.cond(page_id, " AND NOT id=$page_id", "")),
                )[0]
            test_slug = "%s-%d" % (slug, i)
            i += 1
    except IndexError:
        # Page with test_slug doesn't exist — take this slug
        if parent_page.ids:
            ids = parent_page.ids + "," + str(parent_page.id)
        else:
            ids = parent_page.id
        return dict(
            path=new_path,
            slug=test_slug,
            ids=ids,
            level=parent_page.level + 1)
Example #2
0
def join_path(path, slug=""):
    """Appends slug to path"""
    return web.cond(path.endswith("/"), path, path + "/") + slug