def POST(self): dirname = xutils.get_argument("dirname") old_name = xutils.get_argument("old_name", "") new_name = xutils.get_argument("new_name", "") user_name = xauth.current_name() if old_name == "": return dict(code="fail", message="old_name is blank") if ".." in new_name: return dict(code="fail", message="invalid new name") if new_name == "": new_name = os.path.basename(old_name) if xconfig.USE_URLENCODE: old_name = xutils.quote_unicode(old_name) new_name = xutils.quote_unicode(new_name) old_path = os.path.join(dirname, old_name) new_path = os.path.join(dirname, new_name) if not xauth.is_admin() and not check_file_auth(old_path, user_name): return dict(code="fail", message="unauthorized") if not os.path.exists(old_path): return dict(code="fail", message="源文件 `%s` 不存在" % old_name) if os.path.exists(new_path): return dict(code="fail", message="目标文件 `%s` 已存在" % new_name) os.rename(old_path, new_path) xmanager.fire( "fs.rename", Storage(user=user_name, path=new_path, new_path=new_path, old_path=old_path)) return dict(code="success")
def POST(self): id = xutils.get_argument("id") content = xutils.get_argument("content") status = xutils.get_argument("status") location = xutils.get_argument("location", "") user_name = xauth.get_current_name() # 对消息进行语义分析处理,后期优化把所有规则统一管理起来 ctx = Storage(id=id, content=content, user=user_name, type="") for rule in rules: rule.match_execute(ctx, content) ip = get_remote_ip() if id == "" or id is None: ctime = xutils.get_argument("date", xutils.format_datetime()) inserted_id = xutils.call("message.create", content=content, user=user_name, status=get_status_by_code(status), ip=ip, mtime=ctime, ctime=ctime) id = inserted_id xmanager.fire( 'message.add', dict(id=id, user=user_name, content=content, ctime=ctime)) return dict(code="success", data=dict(id=inserted_id, content=content, ctime=ctime)) else: update_message_content(id, user_name, content) return dict(code="success", data=dict(id=id))
def POST(self): id = xutils.get_argument("id") content = xutils.get_argument("content") tag = xutils.get_argument("tag", DEFAULT_TAG) location = xutils.get_argument("location", "") user_name = xauth.get_current_name() # 对消息进行语义分析处理,后期优化把所有规则统一管理起来 ctx = Storage(id=id, content=content, user=user_name, type="") for rule in rules: rule.match_execute(ctx, content) ip = get_remote_ip() if id == "" or id is None: ctime = xutils.get_argument("date", xutils.format_datetime()) inserted_id = MSG_DAO.create(content=content, user=user_name, tag=tag, ip=ip, mtime=ctime, ctime=ctime) id = inserted_id MSG_DAO.refresh_message_stat(user_name) xmanager.fire( 'message.add', dict(id=id, user=user_name, content=content, ctime=ctime)) return dict(code="success", data=dict(id=inserted_id, content=content, ctime=ctime)) else: update_message_content(id, user_name, content) return dict(code="success", data=dict(id=id))
def update_message_status(id, status): if xconfig.DB_ENGINE == "sqlite": db = xtables.get_message_table() msg = db.select_first(where=dict(id=id)) if msg is None: return dict(code="fail", message="data not exists") if msg.user != xauth.get_current_name(): return dict(code="fail", message="no permission") db.update(status=status, mtime=xutils.format_datetime(), where=dict(id=id)) xmanager.fire( "message.updated", Storage(id=id, status=status, user=msg.user, content=msg.content)) else: user_name = xauth.current_name() data = dbutil.get(id) if data and data.user == user_name: data.status = status data.mtime = xutils.format_datetime() dbutil.put(id, data) xmanager.fire( "message.updated", Storage(id=id, user=user_name, status=status, content=data.content)) return dict(code="success")
def POST(self): user = xauth.current_name() note_id = xutils.get_argument("note_id") content = xutils.get_argument("content") version = xutils.get_argument("version", 1, type=int) note = NOTE_DAO.get_by_id(note_id) if note == None: return dict(code="404", message="note not found") if note.creator != user: return dict(code="403", message="unauthorized") if note.list_items is None: note.list_items = [] note.list_items.append(content) NOTE_DAO.update0(note) xmanager.fire( 'note.updated', dict(id=note_id, name=note.name, mtime=dateutil.format_datetime(), content=content, version=version + 1)) return dict(code="success")
def create_note(note_dict): content = note_dict["content"] data = note_dict["data"] creator = note_dict["creator"] priority = note_dict["priority"] mtime = note_dict["mtime"] parent_id = note_dict.get("parent_id", "0") name = note_dict.get("name") note_id = dbutil.timeseq() note_dict["id"] = note_id kv_put_note(note_id, note_dict) # 更新分组下面页面的数量 TODO update_children_count(note_dict["parent_id"]) xmanager.fire("note.add", dict(name=name, type=type)) # 创建对应的文件夹 if type != "group": dirname = os.path.join(xconfig.UPLOAD_DIR, creator, str(parent_id), str(note_id)) else: dirname = os.path.join(xconfig.UPLOAD_DIR, creator, str(note_id)) xutils.makedirs(dirname) return note_id
def fire_update_event(note): event_body = dict(id=note.id, name=note.name, mtime=dateutil.format_datetime(), content=note.content, version=note.version + 1) xmanager.fire('note.updated', event_body)
def POST(self): id = xutils.get_argument("id") content = xutils.get_argument("content") status = xutils.get_argument("status") user_name = xauth.get_current_name() db = xtables.get_message_table() # 对消息进行语义分析处理,后期优化把所有规则统一管理起来 ctx = Storage(id = id, content = content, user = user_name, type = "") for rule in rules: rule.match_execute(ctx, content) if id == "" or id is None: ctime = xutils.get_argument("date", xutils.format_datetime()) inserted_id = db.insert(content = content, user = user_name, status = get_status_by_code(status), mtime = ctime, ctime = ctime) id = inserted_id xmanager.fire('message.add', dict(id=id, user=user_name, content=content, ctime=ctime)) return dict(code="success", data=dict(id=inserted_id, content=content, ctime=ctime)) else: db.update(content = content, mtime = xutils.format_datetime(), where = dict(id=id, user=user_name)) xmanager.fire("message.update", dict(id=id, user=user_name, content=content)) return dict(code="success", data=dict(id=id))
def POST(self): file = xutils.get_argument("file", {}) dirname = xutils.get_argument("dirname") prefix = xutils.get_argument("prefix") name = xutils.get_argument("name") note_id = xutils.get_argument("note_id") user_name = xauth.current_name() if file.filename != None: filename = file.filename if file.filename == "": return dict(code="fail", message="filename is empty") basename, ext = os.path.splitext(filename) if name == "auto": # filename = str(uuid.uuid1()) + ext filename = generate_filename(None, prefix, ext) # xutils.makedirs(dirname) filepath, webpath = xutils.get_upload_file_path( user_name, filename) # filename = xutils.quote(os.path.basename(x.file.filename)) with open(filepath, "wb") as fout: # fout.write(x.file.file.read()) for chunk in file.file: fout.write(chunk) xmanager.fire("fs.upload", dict(user=user_name, path=filepath)) try_touch_note(note_id) return dict(code="success", webpath=webpath, link=get_link(filename, webpath))
def create_note(note_dict, date_str=None): content = note_dict["content"] data = note_dict["data"] creator = note_dict["creator"] priority = note_dict["priority"] mtime = note_dict["mtime"] parent_id = note_dict.get("parent_id", "0") name = note_dict.get("name") # 创建笔记的基础信息 note_id = create_note_base(note_dict, date_str) # 更新分组下面页面的数量 update_children_count(note_dict["parent_id"]) xmanager.fire("note.add", dict(name=name, type=type, id=note_id)) # 创建对应的文件夹 if type == "gallery": dirname = os.path.join(xconfig.UPLOAD_DIR, creator, str(note_id)) xutils.makedirs(dirname) # 更新统计数量 refresh_note_stat(creator) # 更新目录修改时间 touch_note(parent_id) return note_id
def GET(self): id = xutils.get_argument("id", "") name = xutils.get_argument("name", "") file = None if id == "" and name == "": return dict(code="fail", message="id,name至少一个不为空") db = xtables.get_file_table() if id != "": file = db.select_one(where=dict(id=int(id), is_deleted=0)) elif name != "": file = get_by_name(db, name) if file is None: return dict(code="fail", message="文件不存在") id = file.id if not xauth.is_admin() and file.creator != xauth.get_current_name(): return dict(code="fail", message="没有删除权限") if file.type == "group": children_count = db.count(where="parent_id=%s AND is_deleted=0"%id) if children_count > 0: return dict(code="fail", message="分组不为空") db.update(is_deleted=1, mtime=dateutil.format_time(), where=dict(id=int(id))) db.delete(where="is_deleted=1 AND mtime < $date", vars=dict(date=dateutil.before(days=30,format=True))) xmanager.fire("note.remove", dict(id=id)) return dict(code="success")
def POST(self): id = xutils.get_argument("id") name = xutils.get_argument("name") if name == "" or name is None: return dict(code="fail", message="名称为空") old = xutils.call("note.get_by_id", id) if old is None: return dict(code="fail", message="笔记不存在") if old.creator != xauth.get_current_name(): return dict(code="fail", message="没有权限") file = xutils.call("note.get_by_name", name) if file is not None and file.is_deleted == 0: return dict(code="fail", message="%r已存在" % name) xutils.call("note.update", dict(id=id), name=name) event_body = dict(action="rename", id=id, name=name, type=old.type) xmanager.fire("note.updated", event_body) xmanager.fire("note.rename", event_body) if old.type == "group": cacheutil.prefix_del("group.list") return dict(code="success")
def handle_note_recommend(kw, file, user_name): 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) kw.recommended_notes = ctx.result kw.next_note = NOTE_DAO.find_next_note(file, user_name) kw.prev_note = NOTE_DAO.find_prev_note(file, user_name)
def POST(self): 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", "post") format = xutils.get_argument("_format", "") parent_id = xutils.get_argument("parent_id", 0, type=int) if key == "": key = time.strftime("%Y.%m.%d") file = Storage(name=name) file.atime = xutils.format_datetime() file.mtime = xutils.format_datetime() file.ctime = xutils.format_datetime() file.creator = xauth.get_current_name() file.parent_id = parent_id file.type = type file.content = "" file.size = len(content) file.is_public = 0 code = "fail" error = "" try: db = xtables.get_file_table() if name != '': f = get_by_name(db, name) if f != None: key = name raise Exception(u"%s 已存在" % name) file_dict = dict(**file) del file_dict["default_value"] inserted_id = db.insert(**file_dict) update_note_content(inserted_id, content) # 更新分组下面页面的数量 update_children_count(parent_id, db=db) xmanager.fire("note.add", dict(name=name, type=type)) if format == "json": return dict(code="success", id=inserted_id) raise web.seeother("/note/view?id={}".format(inserted_id)) except web.HTTPError as e1: raise e1 except Exception as e: xutils.print_exc() error = str(e) return xtemplate.render("note/add.html", key="", name=key, tags=tags, error=error, pathlist=get_pathlist(db, parent_id), message=error, groups=xutils.call("note.list_group"), code=code)
def test_event_handler(self): ctx = Storage() xmanager.remove_handlers('test') @xmanager.listen("test") def my_handler(ctx): ctx.test = True xmanager.fire('test', ctx) self.assertEqual(True, ctx.test)
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 update_message(id, status): db = xtables.get_message_table() msg = db.select_first(where=dict(id=id)) if msg is None: return dict(code="fail", message="data not exists") if msg.user != xauth.get_current_name(): return dict(code="fail", message="no permission") db.update(status=status, mtime=xutils.format_datetime(), where=dict(id=id)) xmanager.fire("message.updated", Storage(id=id, status=status, user = msg.user, content=msg.content)) return dict(code="success")
def test_event_handler(self): ctx = Storage() def my_handler(ctx): ctx.test = True xmanager.remove_handlers('test') xmanager.add_handler('test', my_handler) xmanager.fire('test', ctx) self.assertEqual(True, ctx.test)
def merge_files(self, dirname, filename, chunks): dest_path = os.path.join(dirname, filename) with open(dest_path, "wb") as fp: for chunk in range(chunks): tmp_path = os.path.join(dirname, filename) tmp_path = "%s_%d.part" % (tmp_path, chunk) if not os.path.exists(tmp_path): raise Exception("upload file broken") with open(tmp_path, "rb") as tmp_fp: fp.write(tmp_fp.read()) xutils.remove(tmp_path, True) xmanager.fire("fs.upload", dict(path=dest_path))
def handle_signal(signum, frame): """处理系统消息 :arg int signum: :arg object frame, current stack frame: """ xutils.log("Signal received: %s" % signum) if signum == signal.SIGALRM: # 时钟信号 return # 优雅下线 xmanager.fire("sys.exit") exit(0)
def update_message_content(id, user_name, content): data = dbutil.get(id) if data and data.user == user_name: # 先保存历史 MSG_DAO.add_history(data) data.content = content data.mtime = xutils.format_datetime() data.version = data.get('version', 0) + 1 MSG_DAO.update(data) xmanager.fire("message.update", dict(id=id, user=user_name, content=content))
def POST(self): is_public = xutils.get_argument("public", "") id = xutils.get_argument("id") content = xutils.get_argument("content") version = xutils.get_argument("version", 0, type=int) file_type = xutils.get_argument("type") name = xutils.get_argument("name", "") db = xtables.get_file_table() file = xutils.call("note.get_by_id", id) if file is None: return xtemplate.render("note/view.html", pathlist=[], file=file, content=content, error="笔记不存在") # 理论上一个人是不能改另一个用户的存档,但是可以拷贝成自己的 # 所以权限只能是创建者而不是修改者 update_kw = dict(content=content, type=file_type, size=len(content), version=version) if name != "" and name != None: update_kw["name"] = name # 不再处理文件,由JS提交 rowcount = xutils.call("note.update", where=dict(id=id, version=version), **update_kw) if rowcount > 0: xmanager.fire( 'note.updated', dict(id=id, name=file.name, mtime=dateutil.format_datetime(), content=content, version=version + 1)) raise web.seeother("/note/view?id=" + str(id)) else: # 传递旧的content cur_version = file.version file.content = content file.version = version return xtemplate.render( "note/view.html", pathlist=[], file=file, content=content, error="更新失败, 版本冲突,当前version={},最新version={}".format( version, cur_version))
def update_message_status(id, status): user_name = xauth.current_name() data = dbutil.get(id) if data and data.user == user_name: data.status = status data.mtime = xutils.format_datetime() MSG_DAO.update(data) MSG_DAO.refresh_message_stat(user_name) xmanager.fire("message.updated", Storage(id=id, user=user_name, status = status, content = data.content)) return dict(code="success")
def update_message_tag(id, tag): user_name = xauth.current_name() data = dbutil.get(id) if data and data.user == user_name: # 修复status数据,全部采用tag if 'status' in data: del data['status'] data.tag = tag data.mtime = xutils.format_datetime() dbutil.put(id, data) MSG_DAO.refresh_message_stat(user_name) xmanager.fire("message.updated", Storage(id=id, user=user_name, tag = tag, content = data.content)) return dict(code="success")
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 POST(self): id = xutils.get_argument("id") if id == "": return db = xtables.get_message_table() msg = db.select_one(where=dict(id=id)) if msg is None: return dict(code="fail", message="data not exists") if msg.user != xauth.get_current_name(): return dict(code="fail", message="no permission") db.delete(where=dict(id=id)) xmanager.fire("message.update", Storage()) return dict(code="success")
def POST(self): path = xutils.get_argument("path") user_name = xauth.current_name() if not xauth.is_admin() and not check_file_auth(path, user_name): return dict(code="fail", message="unauthorized") try: if not os.path.exists(path): basename = os.path.basename(path) return dict(code="fail", message="源文件`%s`不存在" % basename) xutils.remove(path) xmanager.fire("fs.remove", Storage(user=user_name, path=path)) return dict(code="success") except Exception as e: xutils.print_exc() return dict(code="fail", message=str(e))
def create_message(user_name, tag, content, ip): content = content.strip() ctime = xutils.get_argument("date", xutils.format_datetime()) message = dict(content=content, user=user_name, tag=tag, ip=ip, mtime=ctime, ctime=ctime) id = MSG_DAO.create(**message) message['id'] = id MSG_DAO.refresh_message_stat(user_name) xmanager.fire('message.add', dict(id=id, user=user_name, content=content, ctime=ctime)) return message
def POST(self): args = web.input(file={}, dirname=None) x = args dirname = args.dirname if 'file' in x: if x.file.filename == "": raise web.seeother("//fs/%s" % quote(dirname)) xutils.makedirs(dirname) filename = xutils.quote(os.path.basename(x.file.filename)) filepath = os.path.join(dirname, filename) with open(filepath, "wb") as fout: # fout.write(x.file.file.read()) for chunk in x.file.file: fout.write(chunk) xmanager.fire("fs.upload", filepath) raise web.seeother("/fs/%s" % quote(dirname))
def update_message_content(id, user_name, content): if xconfig.DB_ENGINE == "sqlite": db = xtables.get_message_table() db.update(content=content, mtime=xutils.format_datetime(), where=dict(id=id, user=user_name)) xmanager.fire("message.update", dict(id=id, user=user_name, content=content)) else: data = dbutil.get(id) if data and data.user == user_name: data.content = content data.mtime = xutils.format_datetime() dbutil.put(id, data) xmanager.fire("message.update", dict(id=id, user=user_name, content=content))