def GET(self): path = xutils.get_argument("path") length = 1000 read = xutils.get_argument("read", "false") direction = xutils.get_argument("direction", "forward") encoding = "utf-8" page = 0 if not path: return dict(code="fail", message="parameter path is empty") print("path:", path) path = xutils.get_real_path(path) print("real path:", path) if not os.path.exists(path): return dict(code="fail", message="file `%s` not exists" % path) basename, ext = os.path.splitext(path) key = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path)) bookmark = xutils.cache_get(key, {}) bookmark['path'] = path # bookmarkpath = '%s@%s.bookmark' % (xauth.get_current_name(), basename) # bookmark = dict() # if os.path.exists(bookmarkpath): # try: # bookmark = json.loads(xutils.readfile(bookmarkpath)) # if not isinstance(bookmark, dict): # bookmark = dict() # except: # pass page = bookmark.get("page", 0) size = xutils.get_file_size(path, format=False) with open(path, encoding=encoding) as fp: text = "dummy" if direction == "backward": page = page - 1 if direction == "forward": page = page + 1 if page < 0: page = 0 bookmark["page"] = page self.seek_page(fp, bookmark, length) current = fp.tell() text = fp.read(length) if read == "true": xutils.say(text) if direction in ("forward", "backward"): # xutils.writefile(bookmarkpath, json.dumps(bookmark)) xutils.cache_put(key, bookmark) return dict(code="success", data=text, page=page, current=current, size=size)
def check_get_note(id): if xauth.is_admin(): note = NOTE_DAO.get_by_id(id) else: note = NOTE_DAO.get_by_id_creator(id, xauth.current_name()) if note is None: raise NoteException("404", "笔记不存在") return note
def GET(self): id = xutils.get_argument("id", "") filetype = xutils.get_argument("filetype", "") data = xutils.call("note.list_group", xauth.current_name()) web.header("Content-Type", "text/html; charset=utf-8") return xtemplate.render("note/group_select.html", id=id, filelist=data, file_type="group")
def __init__(self, name, url): super(SystemGroup, self).__init__() self.user = xauth.current_name() self.type = 'system' self.icon = "fa fa-gear" self.name = name self.url = url self.priority = 1 self.size = 0
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 GET(self): user_name = xauth.current_name() return xtemplate.render("search/page/search_history.html", show_aside=False, recent=list_search_history(user_name), html_title="Search", files=[], search_action="/note/timeline", search_placeholder=T(u"搜索笔记"))
def GET(self): id = xutils.get_argument("id") file = xutils.call("note.get_by_id", id) user_name = xauth.current_name() if file.is_public != 1 and user_name != "admin" and user_name != file.creator: raise web.seeother("/unauthorized") return xtemplate.render("note/tools/print.html", show_menu=False, note=file)
def find_note_for_view(token, id, name): if token != "": return NOTE_DAO.get_by_token(token) if id != "": return NOTE_DAO.get_by_id(id) if name != "": return NOTE_DAO.get_by_name(xauth.current_name(), name) raise HTTPError(504)
def GET(self, action=""): if action == "get_by_id": id = xutils.get_argument("id") return dict(code="success", data=NOTE_DAO.get_by_id(id)) if action == "get_by_name": name = xutils.get_argument("name") return dict(code="success", data=NOTE_DAO.get_by_name(xauth.current_name(), name)) return dict(code="fail", message="unknown action")
def trace(scene, message, cost=0): import xauth # print(" ", fmt.format(*argv)) fpath = get_log_path() full_message = "%s|%s|%s|%sms|%s" % (format_time(), xauth.current_name(), scene, cost, message) print(full_message) with open(fpath, "ab") as fp: fp.write((full_message + "\n").encode("utf-8"))
def GET(self): id = xutils.get_argument("id", "") parent_id = xutils.get_argument("parent_id", "") file = NOTE_DAO.get_by_id_creator(id, xauth.current_name()) if file is None: return dict(code="fail", message="file not exists") NOTE_DAO.update(dict(id=id), parent_id=parent_id) return dict(code="success")
def batch_query(id_list): creator = xauth.current_name() result = dict() for id in id_list: note = dbutil.get("note_index:%s" % id) if note: result[id] = note build_note_info(note) return result
def GET(self): user = xauth.current_name() files = NOTE_DAO.list_archived(user) return xtemplate.render(VIEW_TPL, pathlist = [PathNode("归档分组", "/note/archived")], file_type = "group", dir_type = "archived", files = files, show_mdate = True)
def GET(self): date = xutils.get_argument("date") user_name = xauth.current_name() if date != None: msg_list = MSG_DAO.list_by_date(user_name, date) else: msg_list = [] process_message_list(msg_list) return dict(code="success", data=msg_list)
def POST(self): user_name = xauth.current_name() part_file = True chunksize = 5 * 1024 * 1024 chunk = xutils.get_argument("chunk", 0, type=int) chunks = xutils.get_argument("chunks", 1, type=int) file = xutils.get_argument("file", {}) prefix = xutils.get_argument("prefix", "") dirname = xutils.get_argument("dirname", xconfig.DATA_DIR) dirname = dirname.replace("$DATA", xconfig.DATA_DIR) # 不能访问上级目录 if ".." in dirname: return dict(code="fail", message="can not access parent directory") filename = None webpath = "" origin_name = "" if hasattr(file, "filename"): origin_name = file.filename xutils.trace("UploadFile", file.filename) filename = os.path.basename(file.filename) filename = xutils.get_real_path(filename) if dirname == "auto": filename = generate_filename(filename, prefix) filepath, webpath = xutils.get_upload_file_path( user_name, filename, replace_exists=True) dirname = os.path.dirname(filepath) filename = os.path.basename(filepath) else: # TODO check permission. pass if part_file: tmp_name = "%s_%d.part" % (filename, chunk) seek = 0 else: tmp_name = filename seek = chunk * chunksize xutils.makedirs(dirname) tmp_path = os.path.join(dirname, tmp_name) with open(tmp_path, "wb") as fp: fp.seek(seek) if seek != 0: xutils.log("seek to {}", seek) for file_chunk in file.file: fp.write(file_chunk) else: return dict(code="fail", message="require file") if part_file and chunk + 1 == chunks: self.merge_files(dirname, filename, chunks) return dict(code="success", webpath=webpath, link=get_link(origin_name, webpath))
def GET(self): user = xauth.current_name() stat = MSG_DAO.get_message_stat(user) stat = format_message_stat(stat) return xtemplate.render("message/calendar.html", show_aside=False, message_stat=stat, search_action="/message", search_placeholder=T("搜索待办事项"))
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() list_func = LIST_FUNC_DICT.get(type, default_list_func) return list_func(locals())
def POST(self): id = xutils.get_argument("id") msg = MSG_DAO.get_by_id(id) if msg is None: return failure(message="message not found, id:%s" % id) if msg.user != xauth.current_name(): return failure(message="not authorized") msg.mtime = xutils.format_datetime() MSG_DAO.update(msg) return success()
def GET(self): # 刷新提醒,上下文为空 user_name = xauth.current_name() offset = 0 limit = 200 orderby = "ctime_desc" xmanager.fire("notice.update") # files = NOTE_DAO.list_by_type(user_name, "list", offset, limit, orderby) return xtemplate.render("note/notice.html")
def POST(self): comment_id = xutils.get_argument("comment_id") user = xauth.current_name() comment = NOTE_DAO.get_comment(comment_id) if comment is None: return dict(success=False, message="comment not found") if user != comment.user: return dict(success=False, message="unauthorized") NOTE_DAO.delete_comment(comment_id) return dict(success=True)
def GET_old(self): user = xauth.current_name() files = xutils.call("note.list_sticky", user) return xtemplate.render(VIEW_TPL, pathlist=[PathNode("置顶笔记", "/note/sticky")], file_type="group", dir_type="sticky", files=files, show_aside=True, show_mdate=True)
def _write_log(level, metric, message, cost): import xauth fpath = get_log_path() user_name = xauth.current_name() if user_name is None: user_name = "-" full_message = "%s|%s|%s|%s|%sms|%s" % (format_time(), level, user_name, metric, cost, message) print(full_message) # 同步写在SAE上面有巨大的性能损耗 log_async(fpath, full_message)
def GET(self): path = xutils.get_argument("path") length = 1000 read = xutils.get_argument("read", "false") direction = xutils.get_argument("direction", "forward") page = 0 if not path: return dict(code = "fail", message = "parameter path is empty") debug_info("path:", path) path = xutils.get_real_path(path) debug_info("real path:", path) if not os.path.exists(path): return dict(code = "fail", message = "file `%s` not exists" % path) basename, ext = os.path.splitext(path) key = "bookmark@%s@%s" % (xauth.current_name(), xutils.md5_hex(path)) bookmark = xutils.cache_get(key, {}) bookmark['path'] = path page = bookmark.get("page", 0) size = xutils.get_file_size(path, format=False) debug_info("bookmark info:", bookmark) encoding = fsutil.detect_encoding(path) debug_info("detected encoding:", encoding) with open(path, encoding = encoding) as fp: text = "dummy" if direction == "backward": page = page - 1 if direction == "forward": page = page + 1 if page < 0: page = 0 try: bookmark["page"] = page seek_page(fp, bookmark, length) current = fp.tell() text = fp.read(length) except UnicodeDecodeError as e: # xutils.print_exc() bookmark['page'] = 0 seek_page(fp, bookmark, length); current = fp.tell() text = fp.read() if read == "true": xutils.say(text) if direction in ("forward", "backward"): # xutils.writefile(bookmarkpath, json.dumps(bookmark)) xutils.cache_put(key, bookmark) return dict(code="success", data=text, page=page, current=current, size=size)
def __init__(self, name = "待办任务"): super(TaskGroup, self).__init__() self.user = xauth.current_name() self.type = 'system' self.name = name self.icon = "fa-calendar-check-o" self.ctime = dateutil.format_time() self.mtime = dateutil.format_time() self.url = "/message?tag=task" self.priority = 1 self.size = MSG_DAO.get_message_stat(self.user).task_count
def batch_query(id_list): if xconfig.DB_ENGINE == "sqlite": return batch_query_sqlite(id_list) creator = xauth.current_name() result = dict() for id in id_list: note = dbutil.get("note_tiny:%s:%020d" % (creator, int(id))) if note: result[id] = note return result
def GET(self): id = xutils.get_argument("id", "") parent_id = xutils.get_argument("parent_id", "") file = NOTE_DAO.get_by_id_creator(id, xauth.current_name()) if file is None: return dict(code="fail", message="笔记不存在") if str(id) == str(parent_id): return dict(code="fail", message="不能移动到自身目录") NOTE_DAO.move(file, parent_id) return dict(code="success")
def GET(self): user_name = xauth.current_name() plugins = PLUGIN.find_plugins("note") template = "note/page/note_tools.html" xmanager.add_visit_log(user_name, "/note/tools") return xtemplate.render(template, html_title=T("笔记工具"), plugins=plugins, search_type="plugin")
def GET(self): id = xutils.get_argument("id", "", type=int) data = xutils.call("note.list_group", xauth.current_name()) return xtemplate.render("note/group_list.html", ungrouped_count=0, file_type="group_list", pseudo_groups=True, show_search_div=True, show_add_group=True, show_aside=True, files=data)
def POST(self): note_id = xutils.get_argument("note_id") content = xutils.get_argument("content") user = xauth.current_name() NOTE_DAO.save_comment( dict(note_id=note_id, ctime=xutils.format_time(), user=user, content=content)) return dict(success=True)
def handle(self, input): self.rows = 0 user_name = xauth.current_name() stat_list = [] admin_stat_list = [] stat_list.append( ["我的笔记", dbutil.count_table("note_tiny:%s" % user_name)]) stat_list.append( ["笔记本", xutils.call("note.count_by_type", user_name, "group")]) stat_list.append( ["待办事项", dbutil.count_table("message:%s" % user_name)]) stat_list.append( ["搜索记录", dbutil.count_table("search_history:%s" % user_name)]) if xauth.is_admin(): admin_stat_list.append( ["note_full", dbutil.count_table("note_full")]) admin_stat_list.append( ["note_tiny", dbutil.count_table("note_tiny")]) admin_stat_list.append( ["note_index", dbutil.count_table("note_index")]) admin_stat_list.append( ["note_history", dbutil.count_table("note_history")]) admin_stat_list.append( ["note_comment", dbutil.count_table("note_comment")]) admin_stat_list.append( ["notebook", dbutil.count_table("notebook")]) admin_stat_list.append( ["search_history", dbutil.count_table("search_history")]) admin_stat_list.append(["message", dbutil.count_table("message")]) admin_stat_list.append([ "msg_search_history", dbutil.count_table("msg_search_history") ]) admin_stat_list.append( ["msg_history", dbutil.count_table("msg_history")]) admin_stat_list.append(["msg_key", dbutil.count_table("msg_key")]) admin_stat_list.append( ["user_stat", dbutil.count_table("user_stat")]) admin_stat_list.append( ["schedule", dbutil.count_table("schedule")]) admin_stat_list.append(["user", dbutil.count_table("user")]) admin_stat_list.append(["record", dbutil.count_table("record")]) self.writetemplate(HTML, stat_list=stat_list, admin_stat_list=admin_stat_list)