def is_admin(): if not is_user(): return False token = web.ctx.session.token userid = auth.decode_token(token)['userid'] db = MySQLDB_(settings['MYSQL_HOST'], settings['MYSQL_USER'], settings['MYSQL_PASSWD'], settings['MYSQL_DB']) user_li = db.query_all("select * from user where `id`='%s'" % userid) db.close() if 0 == len(user_li): return False if int(user_li[0]['level']) != user_admin: return False return True
class BaseAction(object): resp = None template_dir = settings['TEMPLATE_DIR'] theme_dir = settings['TEMPLATE_THEME_DIR'] def __init__(self): self.db = MySQLDB_(settings['MYSQL_HOST'], settings['MYSQL_USER'], settings['MYSQL_PASSWD'], settings['MYSQL_DB']) def get(self): return web.ctx.method == 'GET' def req_params(self): return web.input() def session(self): return web.ctx.session def redirect(self, uri): '''301 ''' self.db.close() raise web.seeother(uri) def notfound(self): self.db.close() return web.notfound('404 Not Found.') def _print(self, page_name, base=None): """显示模板 """ if base: render = web.template.render(fileutil.join_path(self.template_dir, self.theme_dir), base=base) else: render = web.template.render(fileutil.join_path(self.template_dir, self.theme_dir)) self.db.close() return getattr(render, page_name)(self.resp) def error(self): '''出错 ''' self.db.close() return self._print('error') def json(self): """response json 从webpy返回json被chrome认为是错误的 http://www.douban.com/group/topic/32487954/ """ self.db.close() return simplejson.dumps(self.resp) def xml(self, xml_name): """response xml 提供XML访问 http://webpy.org/cookbook/xmlfiles.zh-cn """ web.header('Content-Type', 'text/xml') self.db.close() return self._print(xml_name)