def migrate_schedule(): db = xtables.get_schedule_table() for item in db.select(): key = "schedule:%s" % item.id data = dbutil.get(key) if data is None: dbutil.put(key, item) return "迁移完成!"
def update_tags(creator, note_id, tags): key = "note_tags:%s:%s" % (creator, note_id) dbutil.put(key, Storage(note_id=note_id, tags=tags)) note = get_by_id(note_id) if note != None: note.tags = tags update_index(note)
def create_message(**kw): tag = kw['tag'] if tag == 'key': key = "msg_key:%s:%s" % (kw['user'], dbutil.timeseq()) else: key = "message:%s:%s" % (kw['user'], dbutil.timeseq()) kw['id'] = key dbutil.put(key, kw) return key
def update_user(name, user): if name == "" or name == None: return name = name.lower() mem_user = _users[name] mem_user.update(user) dbutil.put("user:%s" % name, mem_user) xutils.trace("UserUpdate", mem_user)
def create_message(**kw): if xconfig.DB_ENGINE == "sqlite": db = xtables.get_message_table() return db.insert(**kw) else: key = "message:%s:%s" % (kw['user'], dbutil.timeseq()) kw['id'] = key dbutil.put(key, kw) return key
def add_edit_log(note): creator = note.creator note_id = note.id delete_old_edit_log(creator, note_id) key = "note_edit_log:%s:%s" % (creator, dbutil.timeseq()) edit_log = Storage(id=note_id) dbutil.put(key, edit_log)
def save_comment(comment): timeseq = dbutil.timeseq() comment["timeseq"] = timeseq key = "note_comment:%s:%s" % (comment["note_id"], timeseq) dbutil.put(key, comment) key2 = "comment_index:%s:%s" % (comment["user"], timeseq) comment_index = comment.copy() dbutil.put(key2, comment_index)
def add_visit_log(user_name, note): note_id = note.id # 先删除历史的浏览记录,只保留最新的 delete_old_visit_log(user_name, note_id) key = "note_visit_log:%s:%s" % (user_name, dbutil.timeseq()) visit_log = Storage(id=note_id) dbutil.put(key, visit_log)
def migrate_user(): count = 0 db = xtables.get_user_table() for item in db.select(): name = item.name.lower() key = "user:%s" % name data = dbutil.get(key) if data is None: dbutil.put(key, item) count += 1 return "迁移%s条数据" % count
def refresh_message_stat(user): task_count = count_by_tag(user, "task") log_count = count_by_tag(user, "log") done_count = count_by_tag(user, "done") cron_count = count_by_tag(user, "cron") key_count = count_by_tag(user, "key") stat = get_message_stat(user) stat.task_count = task_count stat.log_count = log_count stat.done_count = done_count stat.cron_count = cron_count stat.key_count = key_count dbutil.put("user_stat:%s:message" % user, stat)
def add_cron_task(url, mtime, ctime, tm_wday, tm_hour, tm_min, name = "", message="", sound=0, webpage=0): id = dbutil.timeseq() key = "schedule:%s" % id data = dict(id = id, name=name, url=url, mtime=xutils.format_datetime(), ctime = xutils.format_datetime(), tm_wday = tm_wday, tm_hour = tm_hour, tm_min = tm_min, message = message, sound = sound, webpage = webpage) dbutil.put(key, data) return id
def migrate_note_from_db(): db = get_note_table() content_db = xtables.get_note_content_table() for item in db.select(): note_key = build_note_full_key(item.id) ldb_value = dbutil.get(note_key) # 如果存在需要比较修改时间 if ldb_value and item.mtime >= ldb_value.mtime: build_full_note(item, content_db) dbutil.put(note_key, item) if ldb_value is None: build_full_note(item, content_db) dbutil.put(note_key, item)
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 migrate_note_full(): # sqlite to leveldb db = xtables.get_note_table() content_db = xtables.get_note_content_table() for item in db.select(): note_key = build_note_full_key(item.id) ldb_value = dbutil.get(note_key) # 如果存在需要比较修改时间 if ldb_value and item.mtime >= ldb_value.mtime: build_full_note(item, content_db) dbutil.put(note_key, item) if ldb_value is None: build_full_note(item, content_db) dbutil.put(note_key, item) # old key to new key for item in dbutil.prefix_iter("note.full:"): new_key = build_note_full_key(item.id) new_value = dbutil.get(new_key) if new_value and item.mtime >= new_value.mtime: dbutil.put(new_key, item) if new_value is None: dbutil.put(new_key, item) return "迁移完成!"
def kv_put_note(note_id, note): priority = note.priority mtime = note.mtime creator = note.creator atime = note.atime # 删除不需要持久化的数据 del_dict_key(note, "path") del_dict_key(note, "url") del_dict_key(note, "icon") dbutil.put("note_full:%s" % note_id, note) # 更新索引 update_index(note)
def update_user(name, user): if name == "" or name == None: return name = name.lower() mem_user = find_by_name(name) if mem_user is None: raise Exception("user not found") mem_user.update(user) dbutil.put("user:%s" % name, mem_user) xutils.trace("UserUpdate", mem_user) refresh_users()
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))
def refresh_note_stat(user_name): stat = Storage() if user_name is None: return stat stat.total = count_by_creator(user_name) stat.group_count = count_group(user_name) stat.doc_count = count_by_type(user_name, "doc") stat.gallery_count = count_by_type(user_name, "gallery") stat.list_count = count_by_type(user_name, "list") stat.table_count = count_by_type(user_name, "csv") stat.sticky_count = count_sticky(user_name) dbutil.put("user_stat:%s:note" % user_name, stat) return stat
def update_index(note): id = note['id'] note_index = Storage(**note) del_dict_key(note_index, 'path') del_dict_key(note_index, 'url') del_dict_key(note_index, 'icon') del_dict_key(note_index, 'data') del_dict_key(note_index, 'content') dbutil.put('note_index:%s' % id, note_index) # 更新笔记的排序 update_note_rank(note) # 更新用户索引 dbutil.put("note_tiny:%s:%020d" % (note.creator, int(id)), note)
def migrate_note_full(): # sqlite to leveldb db = get_note_table() if db: migrate_note_from_db() # old key to new key for item in dbutil.prefix_iter("note.full:"): new_key = build_note_full_key(item.id) new_value = dbutil.get(new_key) if new_value and item.mtime >= new_value.mtime: dbutil.put(new_key, item) if new_value is None: dbutil.put(new_key, item) return "迁移完成!"
def POST(self): id = xutils.get_argument("id") name = xutils.get_argument("name") url = xutils.get_argument("url") tm_wday = xutils.get_argument("tm_wday") tm_hour = xutils.get_argument("tm_hour") tm_min = xutils.get_argument("tm_min") message = xutils.get_argument("message") sound_value = xutils.get_argument("sound") webpage_value = xutils.get_argument("webpage") sound = 1 if sound_value == "on" else 0 webpage = 1 if webpage_value == "on" else 0 # db = xtables.get_schedule_table() if id == "" or id is None: id = dbutil.timeseq() key = "schedule:%s" % id data = dict(id=id, name=name, url=url, mtime=xutils.format_datetime(), ctime=xutils.format_datetime(), tm_wday=tm_wday, tm_hour=tm_hour, tm_min=tm_min, message=message, sound=sound, webpage=webpage) dbutil.put(key, data) else: key = "schedule:%s" % id data = dbutil.get(key) if data is not None: data.mtime = xutils.format_datetime() data.name = name data.url = url data.tm_wday = tm_wday data.tm_hour = tm_hour data.tm_min = tm_min data.message = message data.sound = sound data.webpage = webpage dbutil.put(key, data) xmanager.load_tasks() raise web.seeother("/system/crontab")
def add_alarm(tm_hour, tm_min, message): url = "/api/alarm" tm_wday = "no-repeat" name = "[提醒] %s" % message id = dbutil.timeseq() key = "schedule:%s" % id data = dict(id=id, name=name, url=url, mtime=xutils.format_datetime(), ctime=xutils.format_datetime(), tm_wday=tm_wday, tm_hour=tm_hour, tm_min=tm_min, message=message) dbutil.put(key, data) xmanager.load_tasks()
def kv_put_note(note_id, note): priority = note.priority mtime = note.mtime creator = note.creator atime = note.atime dbutil.put("note_full:%s" % note_id, note) score = "%02d:%s" % (priority, mtime) if note.is_deleted: dbutil.zrem("note_recent:%s" % creator, note_id) dbutil.zrem("note_visit:%s" % creator, note_id) else: dbutil.zadd("note_recent:%s" % creator, score, note_id) dbutil.zadd("note_visit:%s" % creator, atime, note_id) del note['content'] del note['data'] dbutil.put("note_tiny:%s:%020d" % (note.creator, int(note_id)), note)
def put_note_to_db(note_id, note): priority = note.priority mtime = note.mtime creator = note.creator atime = note.atime # 删除不需要持久化的数据 del_dict_key(note, "path") del_dict_key(note, "url") del_dict_key(note, "icon") del_dict_key(note, "show_edit") dbutil.put("note_full:%s" % note_id, note) # 更新索引 update_index(note) # 增加编辑日志 add_edit_log(note)
def migrate_note_tags(): db = xtables.get_file_tag_table() count = 0 for item in db.select(): note_id = item.file_id creator = item.user note_key = build_note_full_key(note_id) note = dbutil.get(note_key) if note != None: tag_set = set() if note.tags != None: tag_set.update(note.tags) tag_set.add(item.name) note.tags = list(tag_set) # 更新入库 dbutil.put(note_key, note) put_note_tiny(note) count += 1 return "迁移完成,标签数量: %s" % count
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"] if xconfig.DB_ENGINE == "sqlite": db = xtables.get_file_table() id = db.insert(**note_dict) update_note_content(id, content, data) return id else: id = dbutil.timeseq() key = "note_full:%s" % id note_dict["id"] = id dbutil.put(key, note_dict) score = "%02d:%s" % (priority, mtime) dbutil.zadd("note_recent:%s" % creator, score, id) update_children_count(note_dict["parent_id"]) return id
def kv_put_note(note_id, note): priority = note.priority mtime = note.mtime creator = note.creator atime = note.atime # 删除不需要持久化的数据 del_dict_key(note, "path") del_dict_key(note, "url") del_dict_key(note, "icon") dbutil.put("note_full:%s" % note_id, note) # 更新索引 update_index(note) del_dict_key(note, "content") del_dict_key(note, "data") if note.type == "group": # 笔记本的索引 dbutil.put("notebook:%s:%020d" % (note.creator, int(note_id)), note)
def add_user(name, password): if name == "" or name == None: return dict(code="PARAM_ERROR", message="name为空") if password == "" or password == None: return dict(code="PARAM_ERROR", message="password为空") if not is_valid_username(name): return dict(code="INVALID_NAME", message="非法的用户名") name = name.lower() found = find_by_name(name) if found is not None: return dict(code="fail", message="用户已存在") else: user = Storage(name=name, password=password, token=gen_new_token(), ctime=xutils.format_time(), salt=textutil.random_string(6), mtime=xutils.format_time()) dbutil.put("user:%s" % name, user) xutils.trace("UserAdd", name) refresh_users() return dict(code="success", message="create success")
def migrate_message(): db = xtables.get_message_table() for item in db.select(): try: unix_timestamp = xutils.parse_time(item.ctime) except: unix_timestamp = xutils.parse_time(item.ctime, "%Y-%m-%d") timestamp = "%020d" % (unix_timestamp * 1000) key = "message:%s:%s" % (item.user, timestamp) item["old_id"] = item.id item["id"] = key ldb_value = dbutil.get(key) put_to_db = False if ldb_value and item.mtime >= ldb_value.mtime: put_to_db = True if ldb_value is None: put_to_db = True if put_to_db: dbutil.put(key, item) return "迁移完成!"
def update_index(note): """更新索引的时候也会更新用户维度的索引(note_tiny)""" id = note['id'] note_index = convert_to_index(note) dbutil.put('note_index:%s' % id, note_index) # 更新用户索引 dbutil.put("note_tiny:%s:%s" % (note.creator, format_note_id(id)), note_index) if note.type == "group": dbutil.put("notebook:%s:%s" % (note.creator, format_note_id(id)), note) if note.is_public: dbutil.put("note_public:%s" % format_note_id(id), note_index)