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)
def load_page_data(page): page = normalize_page_ids(page) where = page_where() web.ctx.page = page web.ctx.nav = web.storage( root=db.select( "pages", where="level=1 AND NOT is_deleted AND is_navigatable" + where, order="position").list(), secondary=db.select( "pages", where="(parent_id in $ids OR parent_id = $id) AND " "level=2 AND NOT is_deleted AND is_navigatable" + where, order="position", vars=page).list() if page.level > 0 else [], children=get_page_children(page.id, True), siblings=db.select( "pages", page, where="parent_id=$parent_id AND " "NOT is_deleted AND is_navigatable" + where, order="position").list(), breadcrumbs=( db.select( "pages", where="id in $ids AND NOT is_deleted AND " "is_navigatable" + where, vars=page).list() + [page] if page.ids else []) )
def get_block_by_id(block_id): return db.select( "blocks", locals(), where="id = $block_id AND NOT is_deleted", limit=1, )[0]
def get_document_by_id(document_id): return db.select( "documents", locals(), where="id = $document_id AND NOT is_deleted", limit=1, )[0]
def get_document_by_filename(filename): return db.select( "documents", locals(), where="filename = $filename AND NOT is_deleted", limit=1, )[0]
def get_page_by_id(page_id, with_auth_check=True, with_published_check=True): return db.select( "pages", locals(), where="id = $page_id AND NOT is_deleted" + page_where(with_auth_check, with_published_check), limit=1)[0]
def get_last_position(table_name, parent_id): position = db.select( table_name, what="MAX(position) AS position", where="parent_id = $parent_id", vars=locals() )[0].position return (position or 0) + 1
def get_pages_by_parent_id(parent_id, with_auth_check=True, with_published_check=True): return db.select( "pages", locals(), where="parent_id=$parent_id AND NOT is_deleted" + page_where(with_auth_check, with_published_check) ).list()
def replace_links_match(match): # TODO: what if link is to unpublished page page_id = match.group("page_id") try: page = db.select("pages", locals(), what="path", where="id=$page_id AND NOT is_deleted")[0] return 'href="%(path)s"' % page except IndexError: return 'href="#"'
def get_page_block_by_page_id(page_id): """Returns root block of the page""" return db.select( "blocks", locals(), where="page_id = $page_id AND parent_id IS " "NULL AND NOT is_deleted", limit=1, )[0]
def get_page_blocks_by_page_id(page_id=None): """Returns all page blocks or template blocks""" where = "page_id IS NULL" if page_id is None else "page_id = $page_id" return db.select( "blocks", locals(), where=where + " AND NOT is_deleted", order="position", ).list()
def get_documents_by_parent_id(parent_id, document_type=None): """Select and return documents from parent folder optionally filtered by @document_type""" where = "parent_id = $parent_id AND NOT is_deleted" if document_type == "folder": where += " AND type = 'folder'" elif document_type in ("image", "document"): where += " AND type IN ('folder', '%s')" % document_type return db.select("documents", locals(), where=where, order="position ASC").list()
def get_page_children(parent_id, with_subchildren=False): children = db.select( "pages", locals(), where="parent_id=$parent_id AND is_navigatable AND NOT is_deleted" + page_where(), order="position", ).list() if with_subchildren: ids = [p.id for p in children] if ids: subchildren = db.select( "pages", locals(), where="parent_id in $ids AND is_navigatable AND " "NOT is_deleted" + page_where(), order="position", ).list() for child in children: child.pages = [p for p in subchildren if p.parent_id == child.id] return children
def get_page_by_path(path): params = 0 args = [] while True: page = next(iter(db.select( "pages", locals(), where="path = $path AND NOT is_deleted" " AND params = $params" + page_where(), limit=1)), None) if page is not None: page.args = args return page else: params += 1 path, arg = os.path.split(path) if path == "/" or path == "": break args.append(arg) raise web.notfound()
def delete_tree_branch(table_name, parent_obj, func=None): """Recursively deletes document tree branch. Ignores is_system flag, deletes everything.""" db.update( table_name, where="id = $id AND NOT is_deleted", vars=parent_obj, is_deleted=True, deleted_at=web.SQLLiteral("CURRENT_TIMESTAMP"), ) if func is not None: func(parent_obj) for obj in db.select(table_name, where="parent_id = $id AND NOT is_deleted", vars=parent_obj): delete_tree_branch(table_name, obj, func) return parent_obj
def GET(self, user_id): users = db.select("users", where="NOT is_deleted", order="id DESC") user = db.select("users", locals(), where="id=$user_id")[0] user_form = userForm() user_form.fill(user) return render.users.form(users, user_form, user)
def get_blocks_by_parent_id(parent_id=None): """Returns all blocks for specific parent block""" return db.select("blocks", locals(), where="parent_id = $parent_id AND NOT is_deleted", order="position").list()
def GET(self): users = db.select("users", where="NOT is_deleted", order="id DESC") user_form = userForm() return render.users.list(users, user_form)
def get_document_path(document): return db.select( "documents", vars=document, where="id in $ids AND NOT is_deleted", ).list() if document.ids else []
def get_pages_in_tree_order(): all_pages = db.select("pages", where="NOT is_deleted" + page_where(), order="level, position").list() root = next(p for p in all_pages if p.parent_id is None) return order_pages_tree(root, all_pages)
def GET(self): pages = db.select("pages", where="NOT is_deleted") return render.pages.sitemap(pages)