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 for item in dbutil.prefix_iter("note_tiny"): note_id = str(item.id) note_tag_key = "note_tags:%s:%s" % (item.creator, note_id) note_tag = dbutil.get(note_tag_key) if note_tag is None and item.tags != None: dbutil.put(note_tag_key, Storage(note_id=note_id, tags=item.tags)) count += 1 return "迁移完成,标签数量: %s" % count
def POST(self): id = xutils.get_argument("file_id", type=int) tags_str = xutils.get_argument("tags") tag_db = xtables.get_file_tag_table() if tags_str is None or tags_str == "": tag_db.delete(where=dict(file_id=id)) return dict(code="success") new_tags = set(tags_str.split(" ")) file = dao.get_by_id(id) db = dao.get_file_db() file_db = xtables.get_file_table() # 求出两个差集进行运算 old_tags = tag_db.select(where=dict(file_id=id)) old_tags = set([v.name for v in old_tags]) to_delete = old_tags - new_tags to_add = new_tags - old_tags for item in to_delete: tag_db.delete(where=dict(name=item, file_id=id)) for item in to_add: if item == "": continue tag_db.insert(name=item, file_id=id) file_db.update(related=tags_str, where=dict(id=id)) return dict(code="", message="", data="OK")
def rdb_list_tag(user_name): db = xtables.get_file_tag_table() sql = """SELECT LOWER(name) AS name, COUNT(*) AS amount FROM file_tag WHERE (user=$user OR is_public=1) GROUP BY LOWER(name) ORDER BY amount DESC, name ASC""" tag_list = list(db.query(sql, vars=dict(user=user_name))) return tag_list
def GET(self, id): id = int(id) db = xtables.get_file_tag_table() user_name = xauth.get_current_name() sql = "SELECT * FROM file_tag WHERE file_id=$file_id AND (user=$user OR is_public=1)" file_tags = db.query(sql, vars=dict(file_id=id, user=user_name)) return dict(code="", message="", data=list(file_tags))
def GET(self): id = xutils.get_argument("id", type=int) db = xtables.get_file_table() db.update(is_public=0, where=dict(id=id, creator=xauth.get_current_name())) tag = xtables.get_file_tag_table() tag.update(is_public=0, where=dict(file_id=id, user=xauth.get_current_name())) raise web.seeother("/note/view?id=%s" % id)
def POST(self): id = xutils.get_argument("file_id") tags_str = xutils.get_argument("tags") tag_db = xtables.get_file_tag_table() user_name = xauth.get_current_name() note = xutils.call("note.get_by_id", id) if tags_str is None or tags_str == "": # tag_db.delete(where=dict(file_id=id, user=user_name)) xutils.call("note.update", dict(id=id, creator=user_name), tags=[]) return dict(code="success") new_tags = tags_str.split(" ") xutils.call("note.update", dict(id=id, creator=user_name), tags=new_tags) return dict(code="success", message="", data="OK")
def list_tag(user_name): t = Timer() t.start() cache_key = "%s@tag_list" % user_name tag_list = xutils.cache_get(cache_key) tag_list = None if tag_list is None: db = xtables.get_file_tag_table() sql = """SELECT LOWER(name) AS name, COUNT(*) AS amount FROM file_tag WHERE (user=$user OR is_public=1) GROUP BY LOWER(name) ORDER BY amount DESC, name ASC""" tag_list = list(db.query(sql, vars=dict(user=user_name))) xutils.cache_put(cache_key, tag_list, 60 * 10) t.stop() xutils.trace("NoteDao.ListTag", "", t.cost_millis()) return tag_list
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至少一个不为空") t_file = xtables.get_file_table() t_content = xtables.get_note_content_table() if id != "": file = t_file.select_one(where=dict(id=int(id), is_deleted=0)) elif name != "": file = get_by_name(t_file, 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 = t_file.count( where="parent_id=%s AND is_deleted=0" % id) if children_count > 0: return dict(code="fail", message="分组不为空") t_file.update(is_deleted=1, mtime=dateutil.format_time(), where=dict(id=int(id))) outdated = t_file.select( where="is_deleted=1 AND mtime < $date", vars=dict(date=dateutil.before(days=30, format=True))) for item in outdated: t_file.delete(where=dict(id=item['id'])) t_content.delete(where=dict(id=item['id'])) # 删除标签 t_tag = xtables.get_file_tag_table() t_tag.delete(where=dict(file_id=id)) xmanager.fire("note.remove", dict(id=id)) return dict(code="success")
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 GET(self, id): id = int(id) db = xtables.get_file_tag_table() file_tags = db.select(where=dict(file_id=id)) return dict(code="", message="", data=list(file_tags))