class PluginsHandler: history = History("plugins", 5) def GET(self, name=""): if not name.endswith(".py"): name += ".py" script_name = "plugins/" + name if not os.path.exists(os.path.join(xconfig.PLUGINS_DIR, name)): error = "file `%s` not found" % script_name return xtemplate.render("error.html", error=error) try: self.history.put( Storage(name=os.path.splitext(name)[0], link=web.ctx.path)) vars = dict() vars["script_name"] = script_name xutils.load_script(script_name, vars) main_class = vars.get("Main") if main_class != None: return main_class().render() else: return xtemplate.render("error.html", error="class `Main` not found!") except: error = xutils.print_exc() return xtemplate.render("error.html", error=error) def POST(self, name=""): return self.GET(name)
class ViewHandler: xconfig.note_history = History("笔记浏览记录", 200) @xutils.timeit(name = "Note.View", logfile = True) def GET(self, op, id = None): if id is None: id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) show_menu = xutils.get_argument("show_menu", "true") != "false" show_search = xutils.get_argument("show_search", "true") != "false" orderby = xutils.get_argument("orderby", None) is_iframe = xutils.get_argument("is_iframe", "false") user_name = xauth.current_name() show_add_file = False title = None show_pagination = True show_search_div = False kw = Storage() kw.show_left = False kw.show_groups = False kw.show_aside = True kw.groups = [] kw.recommended_notes = [] if id == "0": raise web.found("/") # 回收站的笔记也能看到 if id == "" and name == "": raise HTTPError(504) if id != "": file = NOTE_DAO.get_by_id(id) elif name is not None: file = NOTE_DAO.get_by_name(name) if file is None: raise web.notfound() if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator: raise web.seeother("/unauthorized") pathlist = xutils.call("note.list_path", file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 show_mdate = False files = [] recent_created = [] amount = 0 show_recommend = False template_name = "note/view.html" next_note = None prev_note = None title = file.name if file.type == "group": if orderby != None and file.orderby != orderby: NOTE_DAO.update(where = dict(id = file.id, creator = file.creator), orderby = orderby) else: orderby = file.orderby files = NOTE_DAO.list_by_parent(user_name, file.id, (page-1)*pagesize, pagesize, orderby) amount = NOTE_DAO.count(user_name, file.id) content = file.content show_search_div = True show_add_file = True show_mdate = True kw.show_aside = False elif file.type == "md" or file.type == "text": content = file.content show_recommend = True show_pagination = False if op == "edit": show_recommend = False template_name = "note/editor/markdown_edit.html" elif file.type == "list": kw.show_aside = False show_pagination = False else: # post/html 等其他类型 handle_note_content(file) show_recommend = True show_pagination = False # 处理笔记背后的文件系统 handle_note_files(kw, file) if show_recommend and user_name is not None: # 推荐系统 handle_note_recommend(kw, file, user_name) xmanager.fire("note.view", file) if op == "edit": kw.show_aside = False if is_iframe == "true": show_menu = False show_search = False kw.show_menu = show_menu kw.show_search = show_search # 如果是页面,需要查出上级目录列表 handle_left_dir(kw, user_name, file, op) return xtemplate.render(template_name, html_title = title, file = file, note_id = id, op = op, show_mdate = show_mdate, show_add_file = show_add_file, show_pagination = show_pagination, can_edit = can_edit, pathlist = pathlist, page_max = math.ceil(amount/pagesize), page = page, page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby), files = files, recent_created = recent_created, is_iframe = is_iframe, **kw)
class handler: xconfig.search_history = History("搜索记录", 200) def do_search(self, key, offset, limit): global _rules category = xutils.get_argument("category", "") words = textutil.split_words(key) files = [] xconfig.search_history.put(dict(key=key, category=category, user=xauth.get_current_name())) start_time = time.time() ctx = SearchContext() ctx.input_text = key ctx.words = words ctx.category = category ctx.search_message = (category == "message") ctx.search_file_full = (category == "content") ctx.search_dict = (category == "dict") ctx.user_name = xauth.get_current_name() if ctx.search_message: ctx.search_file = False ctx.search_file_full = False ctx.search_tool = False if ctx.search_dict: ctx.search_file = False if ctx.search_file_full: ctx.search_tool = False if ctx.category == "book": ctx.search_file = False ctx.search_tool = False xutils.log(" key=%s" % key) xmanager.fire("search.before", ctx) xmanager.fire("search", ctx) for rule in _rules: pattern = rule.pattern func = rule.func # re.match内部已经实现了缓存 m = re.match(pattern, key) if m: try: start_time0 = time.time() results = func(ctx, *m.groups()) cost_time0 = time.time() - start_time0 xutils.log(" >>> %s - %d ms" % (func.modfunc, cost_time0*1000)) if results is not None: files += results except Exception as e: xutils.print_exc() cost_time = (time.time() - start_time) * 1000 xutils.log(" === total - %d ms ===" % cost_time) xmanager.fire("search.after", ctx) return ctx.tools + files @xauth.login_required() def GET(self): """search files by name and content""" load_rules() key = xutils.get_argument("key", "") title = xutils.get_argument("title", "") category = xutils.get_argument("category", "") page = xutils.get_argument("page", 1, type = int) user_name = xauth.get_current_role() page_url = "/search/search?key=%s&category=%s&page="\ % (key, category) pagesize = xconfig.PAGE_SIZE offset = (page-1) * pagesize limit = pagesize if key == "" or key == None: return xtemplate.render("search_result.html", category=category, files=[], count=0) files = self.do_search(key, offset, pagesize) count = len(files) files = files[offset:offset+limit] return xtemplate.render("search_result.html", category = category, files = files, title = title, page_max = math.ceil(count/pagesize), page_url = page_url)
class ViewHandler: xconfig.note_history = History("笔记浏览记录", 200) def GET(self, op): id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) db = xtables.get_file_table() user_name = xauth.get_current_name() show_add_file = False if id == "" and name == "": raise HTTPError(504) if id != "": id = int(id) file = dao.get_by_id(id, db=db) elif name is not None: file = dao.get_by_name(name, db=db) if file is None: raise web.notfound() if not file.is_public and user_name != "admin" and user_name != file.creator: raise web.seeother("/unauthorized") show_search_div = False pathlist = dao.get_pathlist(db, file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 files = [] amount = 0 template_name = "note/view.html" xconfig.note_history.put( dict(user=user_name, file_id=id, name=file.name)) if file.type == "group": amount = db.count( where="parent_id=$id AND is_deleted=0 AND creator=$creator", vars=dict(id=file.id, creator=user_name)) files = db.select(where=dict(parent_id=file.id, is_deleted=0, creator=user_name), order="priority DESC, name", limit=pagesize, offset=(page - 1) * pagesize) content = file.content show_search_div = True show_add_file = True elif file.type == "md" or file.type == "text": content = file.content if op == "edit": template_name = "note/markdown_edit.html" else: content = file.content content = content.replace(u'\xad', '\n') content = content.replace(u'\n', '<br/>') file.data = file.data.replace(u"\xad", "\n") file.data = file.data.replace(u'\n', '<br/>') if file.data == None or file.data == "": file.data = content xmanager.fire("note.view", file) return xtemplate.render(template_name, file=file, op=op, show_add_file=show_add_file, can_edit=can_edit, pathlist=pathlist, page_max=math.ceil(amount / pagesize), page=page, page_url="/file/view?id=%s&page=" % id, files=files)
class ViewHandler: xconfig.note_history = History("笔记浏览记录", 200) @xutils.timeit(name="Note.View", logfile=True) def GET(self, op, id=None): if id is None: id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) show_menu = xutils.get_argument("show_menu", "true") != "false" show_search = xutils.get_argument("show_search", "true") != "false" orderby = xutils.get_argument("orderby", None) is_iframe = xutils.get_argument("is_iframe", "false") token = xutils.get_argument("token", "") user_name = xauth.current_name() kw = Storage() kw.show_left = False kw.show_groups = False kw.show_aside = True kw.groups = [] kw.files = [] kw.op = op kw.user_name = user_name kw.page = page kw.orderby = orderby kw.pagesize = pagesize kw.recommended_notes = [] kw.show_add_file = False kw.template_name = "note/page/view.html" kw.search_action = "/note/timeline" kw.search_placeholder = "搜索笔记" if id == "0": raise web.found("/") # 回收站的笔记也能看到 file = find_note_for_view(token, id, name) if file is None: raise web.notfound() if token == "": check_auth(file, user_name) pathlist = NOTE_DAO.list_path(file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 show_mdate = False recent_created = [] show_recommend = False next_note = None prev_note = None event_ctx = Storage(id=file.id, user_name=user_name) xmanager.fire("note.view", event_ctx) view_func = VIEW_FUNC_DICT.get(file.type, view_md_func) view_func(file, kw) if show_recommend and user_name is not None: # 推荐系统 handle_note_recommend(kw, file, user_name) if op == "edit": kw.show_aside = False kw.show_search = False kw.show_comment = False if is_iframe == "true": kw.show_menu = False kw.show_search = False template_name = kw['template_name'] del kw['template_name'] # 如果是页面,需要查出上级目录列表 handle_left_dir(kw, user_name, file, op) return xtemplate.render_by_ua( template_name, html_title=file.name, file=file, note_id=id, show_mdate=show_mdate, can_edit=can_edit, pathlist=pathlist, page_url="/note/view?id=%s&orderby=%s&page=" % (id, orderby), recent_created=recent_created, CREATE_BTN_TEXT_DICT=CREATE_BTN_TEXT_DICT, is_iframe=is_iframe, **kw)
import six import web import xutils import xconfig import xauth import xmanager import xtemplate import xtables from xutils import textutil, u, cacheutil from xutils import Queue, History, Storage config = xconfig _rules = [] # 初始化搜索记录 if xconfig.search_history is None: xconfig.search_history = History('search', 1000) class BaseRule: def __init__(self, pattern, func, scope="home"): self.pattern = pattern self.func = func self.scope = scope def add_rule(pattern, func_str): global _rules try: mod, func_name = func_str.rsplit('.', 1) # mod = __import__(mod, None, None, ['']) mod = six._import_module("handlers.search." + mod)
import six import web import xutils import xconfig import xauth import xmanager import xtemplate import xtables from xutils import textutil, u, cacheutil from xutils import Queue, History, Storage config = xconfig _rules = [] # 初始化搜索记录 if xconfig.search_history is None: xconfig.search_history = History('search', xconfig.SEARCH_HISTORY_MAX_SIZE) class BaseRule: def __init__(self, pattern, func, scope="home"): self.pattern = pattern self.func = func self.scope = scope def add_rule(pattern, func_str): global _rules try: mod, func_name = func_str.rsplit('.', 1) # mod = __import__(mod, None, None, ['']) mod = six._import_module("handlers.search." + mod) func = getattr(mod, func_name)
class ViewHandler: xconfig.note_history = History("笔记浏览记录", 200) @xutils.timeit(name = "Note.View", logfile = True) def GET(self, op): id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) show_menu = xutils.get_argument("show_menu", "true") != "false" orderby = xutils.get_argument("orderby", "mtiem_desc") user_name = xauth.get_current_name() show_add_file = False title = None show_pagination = True show_search_div = False if id == "0": raise web.found("/") # 回收站的笔记也能看到 if id == "" and name == "": raise HTTPError(504) if id != "": file = xutils.call("note.get_by_id", id) elif name is not None: file = xutils.call("note.get_by_name", name, db=db) if file is None: raise web.notfound() if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator: raise web.seeother("/unauthorized") pathlist = xutils.call("note.list_path", file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 show_groups = False show_mdate = False files = [] recent_created = [] groups = [] amount = 0 show_recommend = False template_name = "note/view.html" next_note = None prev_note = None filelist = None xconfig.note_history.put(dict(user=user_name, link = "/note/view?id=%s" % id, name = file.name)) recommended_notes = [] title = file.name if file.type == "group": files = xutils.call("note.list_by_parent", user_name, file.id, (page-1)*pagesize, pagesize, orderby) amount = xutils.call("note.count", user_name, file.id) content = file.content show_search_div = True show_add_file = True show_mdate = True elif file.type == "md" or file.type == "text": content = file.content show_recommend = True show_pagination = False if op == "edit": show_recommend = False template_name = "note/editor/markdown_edit.html" else: content = file.content content = content.replace(u'\xad', '\n') content = content.replace(u'\n', '<br/>') file.data = file.data.replace(u"\xad", "\n") file.data = file.data.replace(u'\n', '<br/>') if file.data == None or file.data == "": file.data = content show_recommend = True show_pagination = False fpath = os.path.join(xconfig.UPLOAD_DIR, file.creator, str(file.parent_id), str(id)) # 处理相册 if file.type == "gallery": if os.path.exists(fpath): filelist = fsutil.list_files(fpath, webpath = True) else: filelist = [] file.path = fpath if show_recommend and user_name is not None: show_groups = False # 推荐系统 ctx = Storage(id=file.id, name = file.name, creator = file.creator, content = file.content, parent_id = file.parent_id, result = []) xmanager.fire("note.recommend", ctx) recommended_notes = ctx.result next_note = xutils.call("note.find_next_note", file) prev_note = xutils.call("note.find_prev_note", file) xmanager.fire("note.view", file) show_aside = True if op == "edit": show_aside = False return xtemplate.render(template_name, show_aside = show_aside, html_title = title, file = file, path = fpath, filelist = filelist, note_id = id, op = op, show_mdate = show_mdate, show_add_file = show_add_file, show_menu = show_menu, show_pagination = show_pagination, can_edit = can_edit, pathlist = pathlist, page_max = math.ceil(amount/pagesize), page = page, page_url = "/note/view?id=%s&orderby=%s&page=" % (id, orderby), files = files, recent_created = recent_created, show_groups = show_groups, groups = groups, prev_note = prev_note, next_note = next_note, recommended_notes = recommended_notes)
class ViewHandler: xconfig.note_history = History("笔记浏览记录", 200) def GET(self, op): id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") page = xutils.get_argument("page", 1, type=int) pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) show_menu = xutils.get_argument("show_menu", "true") != "false" db = xtables.get_file_table() user_name = xauth.get_current_name() show_add_file = False title = None show_pagination = True show_search_div = False if id == "" and name == "": raise HTTPError(504) if id != "": id = int(id) file = dao.get_by_id(id, db=db) elif name is not None: file = dao.get_by_name(name, db=db) if file is None: raise web.notfound() if file.is_deleted == 1: raise web.seeother("/") if file.type != "group" and not file.is_public and user_name != "admin" and user_name != file.creator: raise web.seeother("/unauthorized") pathlist = dao.get_pathlist(db, file) can_edit = (file.creator == user_name) or (user_name == "admin") role = xauth.get_current_role() # 定义一些变量 show_groups = False show_mdate = False files = [] recent_created = [] groups = [] amount = 0 show_recommend = False template_name = "note/view.html" next_note = None prev_note = None xconfig.note_history.put( dict(user=user_name, link="/note/view?id=%s" % id, name=file.name)) recommended_notes = [] title = file.name if file.type == "group": where_sql = "parent_id=$parent_id AND is_deleted=0 AND (creator=$creator OR is_public=1)" if xauth.is_admin(): where_sql = "parent_id=$parent_id AND is_deleted=0" amount = db.count(where=where_sql, vars=dict(parent_id=file.id, creator=user_name)) files = db.select(where=where_sql, vars=dict(parent_id=file.id, is_deleted=0, creator=user_name), order="name", limit=pagesize, offset=(page - 1) * pagesize) content = file.content show_search_div = True show_add_file = True show_mdate = True # recent_created = xutils.call("note.list_recent_created", file.id, 10) elif file.type == "md" or file.type == "text": content = file.content if op == "edit": template_name = "note/markdown_edit.html" show_recommend = True show_pagination = False else: content = file.content content = content.replace(u'\xad', '\n') content = content.replace(u'\n', '<br/>') file.data = file.data.replace(u"\xad", "\n") file.data = file.data.replace(u'\n', '<br/>') if file.data == None or file.data == "": file.data = content show_recommend = True show_pagination = False if show_recommend: show_groups = False # 推荐系统 ctx = Storage(id=file.id, name=file.name, creator=file.creator, content=file.content, parent_id=file.parent_id, result=[]) xmanager.fire("note.recommend", ctx) recommended_notes = ctx.result next_note = xutils.call("note.find_next_note", file) prev_note = xutils.call("note.find_prev_note", file) xmanager.fire("note.view", file) show_aside = True if op == "edit": show_aside = False return xtemplate.render(template_name, show_aside=show_aside, html_title=title, file=file, note_id=id, op=op, show_mdate=show_mdate, show_add_file=show_add_file, show_menu=show_menu, show_pagination=show_pagination, can_edit=can_edit, pathlist=pathlist, page_max=math.ceil(amount / pagesize), page=page, page_url="/note/view?id=%s&page=" % id, files=files, recent_created=recent_created, show_groups=show_groups, groups=groups, prev_note=prev_note, next_note=next_note, recommended_notes=recommended_notes)
def list_recent_plugins(): items = History.get_items("plugins") links = list(items) links.reverse() return links