def item_list(req, pager, **kwargs): keys = list("%s %s %%s" % (k, 'in' if islistable(v) else '=') for k, v in kwargs.items()) cond = "WHERE " + ' AND '.join(keys) if keys else '' tran = req.db.transaction(req.log_info) c = tran.cursor(DictCursor) c.execute(""" SELECT page_id, author_id, name, title, locale, editor_rights, format FROM page_files %s ORDER BY %s %s LIMIT %%s, %%s """ % (cond, pager.order, pager.sort), tuple(kwargs.values()) + (pager.offset, pager.limit)) items = [] for row in iter(c.fetchone, None): page = Page() page.from_row(row) page.get_modify(req) items.append(page) # endfow c.execute("SELECT count(*) FROM page_files %s" % cond, kwargs.values()) pager.total = c.fetchone()[0] tran.commit() return items
def admin_pages_regenerate_all(req): check_login(req, '/log_in?referer=/admin/pages') check_right(req, 'pages_modify') Page.regenerate_all(req) # TODO: redirect to same page redirect(req, '/admin/pages?error=%d' % SUCCESS)
def regenerate_all(req): tran = req.db.transaction(req.log_info) c = tran.cursor() c.execute(""" SELECT page_id, author_id, name, title, locale, format FROM page_files """) for row in iter(c.fetchone, None): page = Page() page.from_row(row) page.regenerate(req) tran.commit()
def runtime_file(req): text = None if req.uri == '/': text = Page.text_by_name(req, 'index.html') elif req.uri.endswith('.html'): text = Page.text_by_name(req, req.uri[req.uri.rfind('/')+1:]) else: # without .html text = Page.text_by_name(req, req.uri[req.uri.rfind('/')+1:]+'.html') if text is None: text = Page.text_by_name(req, req.uri[req.uri.rfind('/')+1:]+'.rst') if text is None: raise SERVER_RETURN(state.HTTP_NOT_FOUND) text = rst2html(text) return generate_page(req, 'runtime_file.html', text=text, runtime=True)
def admin_pages(req): check_login(req) match_right(req, module_rights) error = req.args.getfirst('error', 0, int) pager = Pager() pager.bind(req.args) if not do_match_right(req, ('pages_modify', 'pages_listall')): rows = Page.list(req, pager, author_id=req.login.id) else: rows = Page.list(req, pager) return generate_page(req, "admin/pages.html", token=do_create_token(req, '/admin/pages'), pager=pager, rows=rows, error=error)
def admin_pages_del(req, id): """ Delete page, could: * author of page if have still pages_author right * admin with pages_modify """ check_login(req, '/log_in?referer=/admin/pages') match_right(req, ('pages_author', 'pages_modify')) check_token(req, req.form.get('token')) page = Page(id) if not page.check_right(req): raise SERVER_RETURN(state.HTTP_FORBIDDEN) page.delete(req) # TODO: redirect to same page redirect(req, '/admin/pages?error=%d' % SUCCESS)
def admin_pagse_add(req): check_login(req) match_right(req, ('pages_author', 'pages_modify')) token = do_create_token(req, '/admin/pages/add') if req.method == 'POST': check_token(req, req.form.get('token')) page = Page() page.bind(req.form, req.login.id) error = page.add(req) if error: return generate_page(req, "admin/pages_mod.html", token=token, rights=rights, page=page, error=error) redirect(req, '/admin/pages/%d' % page.id) # end return generate_page(req, "admin/pages_mod.html", token=token, rights=rights)
def admin_pages_mod(req, id): """Edit page could: * author of page, if still have pages_author right * admin with pages_modify right * admin with pages_listall right and right which must have page too """ check_login(req) match_right(req, module_rights) token = do_create_token(req, '/admin/pages/%d' % id) page = Page(id) if (not do_check_right(req, 'pages_modify')) \ and (not page.check_right(req)): raise SERVER_RETURN(state.HTTP_FORBIDDEN) if req.method == 'POST': check_token(req, req.form.get('token')) page.bind(req.form) error = page.mod(req) if error: return generate_page(req, "admin/pages_mod.html", token=token, page=page, rights=rights, error=error, extra_rights=req.cfg.pages_extra_rights) # endif if not page.get(req): raise SERVER_RETURN(state.HTTP_NOT_FOUND) return generate_page(req, "admin/pages_mod.html", token=token, page=page, rights=rights, extra_rights=req.cfg.pages_extra_rights)
def refresh_page_files(req, cfg_timestamp, clear=True): global timestamp check = check_timestamp(req, cfg_timestamp) if check > timestamp: # if last load was in past to timestamp file req.log_error("file timestamp is older, refresh page_files ...", state.LOG_INFO) if clear: for uri, hdls in app.handlers.items(): if hdls.get(state.METHOD_GET) == runtime_file: app.pop_route(uri, state.METHOD_GET) app.pop_route(uri, state.METHOD_HEAD) if req.cfg.pages_index_is_root: app.set_route('/', runtime_file) # / will be index if req.cfg.pages_runtime_without_html: for it in Page.list(req, Pager(limit=-1)): if not it.found: req.cfg.log_error("Page %s not found" % it.name) continue name = it.name[:it.name.rfind('.')] if name in ('admin', 'user', 'login', 'logout'): req.cfg.log_error('Denied runtime file uri: %s' % it.name[:-5], state.LOG_ERR) continue req.cfg.log_info("Adding /%s" % name) app.set_route('/'+name, runtime_file) # without .html else: for it in Page.list(req, Pager(limit=-1)): if not it.found: req.cfg.log_error("Page %s not found" % it.name) continue req.cfg.log_info("Adding /%s" % name) # rst not work app.set_route('/'+it.name, runtime_file) timestamp = check