def build_search_context(user_name, category, key): words = textutil.split_words(key) ctx = SearchContext() ctx.key = key ctx.input_text = key ctx.words = words ctx.category = category ctx.search_message = (category == "message") ctx.search_note_content = (category == "content") ctx.search_dict = (category == "dict") ctx.user_name = user_name if ctx.search_message: ctx.search_note = False ctx.search_note_content = False ctx.search_tool = False if ctx.search_dict: ctx.search_note = False ctx.search_tool = False if ctx.search_note_content: ctx.search_tool = False if ctx.category == "book": ctx.search_note = False ctx.search_tool = False return ctx
def on_search_plugins(ctx): if not xauth.is_admin(): return if not ctx.search_tool: return if ctx.search_dict: return name = ctx.key results = [] words = textutil.split_words(name) for name in xconfig.PLUGINS_DICT: plugin = xconfig.PLUGINS_DICT[name] unquote_name = xutils.unquote(plugin.fname) unquote_name, ext = os.path.splitext(unquote_name) plugin_context = plugin if textutil.contains_all(unquote_name, words) \ or (textutil.contains_all(plugin_context.title, words)): result = SearchResult() result.category = "plugin" result.icon = "fa-cube" result.name = u(unquote_name) if plugin_context != None: # result.raw = u(plugin_context.title) # result.name = u("插件 %s (%s)") % (u(plugin_context.title), unquote_name) if plugin_context.title != None: result.name = u(plugin_context.title + "(" + unquote_name + ")") result.url = u(plugin.url) result.edit_link = u("/code/edit?path=" + plugin.fpath) results.append(result) ctx.tools += results
def do_search(self, page_ctx, key, offset, limit): category = xutils.get_argument("category", "") words = textutil.split_words(key) user_name = xauth.get_current_name() start_time = time.time() ctx = build_search_context(user_name, category, key) # 阻断性的搜索,比如特定语法的 xmanager.fire("search.before", ctx) if ctx.stop: return ctx.join_as_files() # 普通的搜索行为 xmanager.fire("search", ctx) ctx.files = apply_search_rules(ctx, key) cost_time = int((time.time() - start_time) * 1000) log_search_history(user_name, key, category, cost_time) if ctx.stop: return ctx.join_as_files() # 慢搜索,如果时间过长,这个服务会被降级 xmanager.fire("search.slow", ctx) xmanager.fire("search.after", ctx) page_ctx.tools = [] return ctx.join_as_files()
def do_search(self, key, offset, limit): global _rules category = xutils.get_argument("category", "") words = textutil.split_words(key) files = [] 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) xconfig.search_history.put( Storage(name="#search# %s - %d ms" % (key, cost_time), category=category, user=xauth.get_current_name(), link=web.ctx.fullpath)) xmanager.fire("search.after", ctx) return ctx.tools + files
def on_search_plugins(ctx): if not xauth.is_admin(): return if not ctx.search_tool: return if ctx.search_dict: return name = ctx.key results = [] dirname = xconfig.PLUGINS_DIR words = textutil.split_words(name) for fname in xutils.listdir(dirname): unquote_name = xutils.unquote(fname) unquote_name, ext = os.path.splitext(unquote_name) plugin_context = xconfig.PLUGINS.get(fname) if textutil.contains_all(unquote_name, words) \ or (plugin_context != None and textutil.contains_all(plugin_context.title, words)): result = SearchResult() result.category = "plugin" result.name = u("[插件] " + unquote_name) if plugin_context != None: # result.raw = u(plugin_context.title) # result.name = u("插件 %s (%s)") % (u(plugin_context.title), unquote_name) if plugin_context.title != None: result.name = u("[插件] " + plugin_context.title + "(" + unquote_name + ")") result.url = u("/plugins/" + unquote_name) result.edit_link = u("/code/edit?path=" + os.path.join(dirname, fname)) results.append(result) ctx.tools += results
def list_search_func(context): # 主要是搜索 offset = context['offset'] limit = context['limit'] search_key = context['search_key'] type = context['type'] user_name = context['user_name'] parent_id = xutils.get_argument("parent_id", "") words = None rows = [] if parent_id == "": parent_id = None start_time = time.time() if search_key != None and search_key != "": # TODO 公共笔记的搜索 search_key = xutils.unquote(search_key) words = split_words(search_key) rows = NOTE_DAO.search_name(words, user_name, parent_id=parent_id) rows = rows[offset:offset + limit] cost_time = time.time() - start_time NOTE_DAO.add_search_history(user_name, search_key, "note", cost_time) return build_date_result(rows, 'ctime', sticky_title=True, group_title=True)
def search_plugins(key): words = textutil.split_words(key) plugins = list_plugins("all") result = [] for p in plugins: if textutil.contains_all(p.title, words) or textutil.contains_all(p.url, words) or textutil.contains_all(p.fname, words): result.append(p) return result
def search_plugins(key): from xutils.functions import dictvalues words = textutil.split_words(key) plugins = list_plugins("all") result = dict() for p in plugins: if is_plugin_matched(p, words): result[p.url] = p return dictvalues(result)
def GET(self): offset = xutils.get_argument("offset", 0, type=int) limit = xutils.get_argument("limit", 20, type=int) type = xutils.get_argument("type", "root") parent_id = xutils.get_argument("parent_id", None, type=str) search_key = xutils.get_argument("key", None, type=str) user_name = xauth.current_name() if type == "public": rows = NOTE_DAO.list_public(offset, limit) elif type == "sticky": rows = NOTE_DAO.list_sticky(user_name, offset, limit) elif type == "removed": rows = NOTE_DAO.list_removed(user_name, offset, limit) elif type in ("md", "group", "gallery", "document", "list", "table", "csv"): rows = NOTE_DAO.list_by_type(user_name, type, offset, limit) elif type == "archived": rows = NOTE_DAO.list_archived(user_name, offset, limit) elif type == "all": rows = NOTE_DAO.list_recent_created(user_name, offset, limit) elif type == "root": rows = NOTE_DAO.list_group(user_name) rows.insert(0, TaskGroup()) orderby = "mtime" else: # 主要是搜索 words = None if search_key != None and search_key != "": # TODO 公共笔记的搜索 search_key = xutils.unquote(search_key) search_key_lower = search_key.lower() parent_id = None words = textutil.split_words(search_key_lower) # groups = search_group(user_name, words) def list_func(key, value): if value.is_deleted: return False if value.name is None: return False if parent_id != None and str( value.parent_id) != str(parent_id): return False if words != None and not textutil.contains_all( value.name.lower(), words): return False return True # TODO 搜索公开内容 rows = NOTE_DAO.list_by_func(user_name, list_func, offset, limit) orderby = "ctime" if type in ("mtime", "group", "root"): orderby = "mtime" return build_date_result(rows, type, orderby)
def search_plugins(name): results = [] dirname = xconfig.PLUGINS_DIR words = textutil.split_words(name) for fname in xutils.listdir(dirname): unquote_name = xutils.unquote(fname) if textutil.contains_all(unquote_name, words): result = SearchResult() result.category = "plugin" result.name = u("插件 - " + unquote_name) result.url = u("/plugins/" + fname) result.edit_link = u("/code/edit?path=" + os.path.join(dirname, fname)) results.append(result) return results
def search_scripts(name): words = textutil.split_words(name) results = [] for fname in xutils.listdir(xconfig.SCRIPTS_DIR): fpath = os.path.join(xconfig.SCRIPTS_DIR, fname) if not os.path.isfile(fpath): continue if fname.endswith(".zip"): continue if textutil.contains_all(fname, words): result = xutils.SearchResult() result.icon = "icon-script" result.name = xutils.u(fname) result.raw = xutils.u("搜索到可执行脚本 - ") + fname result.url = xutils.u("/code/edit?path=%s") % fpath result.command = xutils.u("/system/script_admin/execute?name=%s") % fname results.append(result) return results
def POST(self, method='POST'): name = xutils.get_argument("name", "") tags = xutils.get_argument("tags", "") key = xutils.get_argument("key", "") content = xutils.get_argument("content", "") type = xutils.get_argument("type", "md") format = xutils.get_argument("_format", "") parent_id = xutils.get_argument("parent_id", "0") if key == "": key = time.strftime("%Y.%m.%d") + dateutil.current_wday() type = NOTE_TYPE_MAPPING.get(type, type) creator = xauth.current_name() note = Storage(name = name) note.atime = xutils.format_datetime() note.mtime = xutils.format_datetime() note.ctime = xutils.format_datetime() note.creator = creator note.parent_id = parent_id note.type = type note.content = content note.data = "" note.size = len(content) note.is_public = 0 note.priority = 0 note.version = 0 note.is_deleted = 0 note.tags = textutil.split_words(tags) heading = T("创建笔记") code = "fail" error = "" ctx = Storage(method = method) try: if type not in VALID_NOTE_TYPE_SET: raise Exception(u"无效的类型: %s" % type) create_func = CREATE_FUNC_DICT.get(type, default_create_func) inserted_id = create_func(note, ctx) if format == "json": return dict(code="success", id = inserted_id, url = "/note/edit?id=%s" % inserted_id) if inserted_id != None: raise web.seeother("/note/edit?id={}".format(inserted_id)) except web.HTTPError as e1: xutils.print_exc() raise e1 except Exception as e: xutils.print_exc() error = xutils.u(e) if format == 'json': return dict(code = 'fail', message = error) heading = get_heading_by_type(type) return xtemplate.render("note/page/create.html", show_search = False, heading = heading, key = "", type = type, name = key, tags = tags, error = error, message = error, NOTE_TYPE_LIST = NOTE_TYPE_LIST, groups = NOTE_DAO.list_group(creator), code = code)
def do_search(self, key, offset, limit): global _rules category = xutils.get_argument("category", "") words = textutil.split_words(key) files = [] user_name = xauth.get_current_name() start_time = time.time() ctx = SearchContext() ctx.key = key ctx.input_text = key ctx.words = words ctx.category = category ctx.search_message = (category == "message") ctx.search_note_content = (category == "content") ctx.search_dict = (category == "dict") ctx.user_name = user_name if ctx.search_message: ctx.search_note = False ctx.search_note_content = False ctx.search_tool = False if ctx.search_dict: ctx.search_note = False ctx.search_tool = False if ctx.search_note_content: ctx.search_tool = False if ctx.category == "book": ctx.search_note = False ctx.search_tool = False xutils.trace("SearchKey", key) # 阻断性的搜索,比如特定语法的 xmanager.fire("search.before", ctx) if ctx.stop: return ctx.dicts + ctx.tools + ctx.notes # 普通的搜索行为 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.trace("SearchHandler", func.modfunc, int(cost_time0 * 1000)) if results is not None: files += results except Exception as e: xutils.print_exc() cost_time = int((time.time() - start_time) * 1000) xutils.trace("SearchTime", key, cost_time) xconfig.search_history.add(key, cost_time) log_search_history(user_name, key) if ctx.stop: return ctx.dicts + ctx.tools + files # 慢搜索 xmanager.fire("search.slow", ctx) xmanager.fire("search.after", ctx) return ctx.dicts + ctx.tools + files
def POST(self, method='POST'): name = xutils.get_argument("name", "") tags = xutils.get_argument("tags", "") key = xutils.get_argument("key", "") content = xutils.get_argument("content", "") type = xutils.get_argument("type", "md") format = xutils.get_argument("_format", "") parent_id = xutils.get_argument("parent_id", "0") if key == "": key = time.strftime("%Y.%m.%d") + dateutil.current_wday() creator = xauth.current_name() note = Storage(name=name) note.atime = xutils.format_datetime() note.mtime = xutils.format_datetime() note.ctime = xutils.format_datetime() note.creator = creator note.parent_id = parent_id note.type = type note.content = content note.data = "" note.size = len(content) note.is_public = 0 note.priority = 0 note.version = 0 note.is_deleted = 0 note.tags = textutil.split_words(tags) heading = T("创建笔记") code = "fail" error = "" try: if name == '': if method == 'POST': message = 'name is empty' raise Exception(message) else: f = NOTE_DAO.get_by_name(name) if f != None: key = name message = u"%s 已存在" % name raise Exception(message) inserted_id = NOTE_DAO.create(note) if format == "json": return dict(code="success", id=inserted_id) raise web.seeother("/note/{}".format(inserted_id)) except web.HTTPError as e1: xutils.print_exc() raise e1 except Exception as e: xutils.print_exc() error = str(e) if format == 'json': return dict(code='fail', message=error) heading = get_heading_by_type(type) return xtemplate.render("note/add.html", show_search=False, heading=heading, key="", type=type, name=key, tags=tags, error=error, message=error, groups=NOTE_DAO.list_group(creator), code=code)