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() db = xtables.get_message_table() # 对消息进行语义分析处理,后期优化把所有规则统一管理起来 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 rdb_search_message(user_name, key, offset, limit): db = xtables.get_message_table() vars = dict(user=user_name) kw = "user = $user" start_time = time.time() for item in key.split(" "): if item == "": continue kw += " AND content LIKE " + fuzzy_item(item) # when find numbers, the sql printed is not correct # eg. LIKE '%1%' will be LIKE '%' chatlist = list( db.select(where=kw, vars=vars, order="ctime DESC", limit=limit, offset=offset)) end_time = time.time() cost_time = int((end_time - start_time) * 1000) xutils.trace("MessageSearch", key, cost_time) if xconfig.search_history is not None: xconfig.search_history.add(key, cost_time) amount = db.count(where=kw, vars=vars) return chatlist, amount
def GET(self): date = xutils.get_argument("date") db = xtables.get_message_table() data = db.select(where="ctime LIKE $date AND user=$user", vars=dict(date=date + '%', user=xauth.get_current_name())) return dict(code="success", data=list(data))
def rdb_list_message_page(user, status, offset, limit): db = xtables.get_message_table() kw = dict(user=user, status=status) chatlist = list( db.select(where=kw, order="ctime DESC", limit=limit, offset=offset)) amount = db.count(where=kw) return chatlist, amount
def POST(self): id = xutils.get_argument("id") content = xutils.get_argument("content") user_name = xauth.get_current_name() db = xtables.get_message_table() # 对消息进行语义分析处理,后期优化把所有规则统一管理起来 ctx = Storage(content=content, user=user_name, type="") for rule in rules: rule.match_execute(ctx, content) xmanager.fire('message.update', ctx) if id == "" or id is None: ctime = xutils.get_argument("date", xutils.format_datetime()) inserted_id = db.insert(content=content, user=user_name, ctime=ctime, type=ctx.get("type", "")) return dict(code="success", data=dict(id=inserted_id, content=content, ctime=ctime)) db.update(content=content, mtime=xutils.format_datetime(), type=ctx.get("type", ""), where=dict(id=id, user=user_name)) return dict(code="success")
def GET(self): pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) page = xutils.get_argument("page", 1, type=int) status = xutils.get_argument("status") offset = (page - 1) * pagesize db = xtables.get_message_table() user_name = xauth.get_current_name() kw = "1=1" if status == "created": kw = "status = 0" if status == "done": kw = "status = 100" kw += " AND user = $user" vars = dict(user=xauth.get_current_name()) chatlist = list( db.select(where=kw, vars=vars, order="ctime DESC", limit=pagesize, offset=offset)) chatlist.reverse() amount = db.count(where=kw, vars=vars) page_max = math.ceil(amount / pagesize) return dict(code="success", message="", data=chatlist, amount=amount, page_max=page_max, current_user=xauth.get_current_name())
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 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 GET(self): pagesize = xutils.get_argument("pagesize", xconfig.PAGE_SIZE, type=int) page = xutils.get_argument("page", 1, type=int) status = xutils.get_argument("status") key = xutils.get_argument("key") offset = (page - 1) * pagesize db = xtables.get_message_table() user_name = xauth.get_current_name() kw = "1=1" if status == "created": kw = "status = 0" if status == "done": kw = "status = 100" if status == "suspended": kw = "status = 50" kw += " AND user = $user" vars = dict(user=xauth.get_current_name()) if key != "" and key != None: start_time = time.time() for item in key.split(" "): if item == "": continue kw += " AND content LIKE " + fuzzy_item(item) # when find numbers, the sql printed is not correct # eg. LIKE '%1%' will be LIKE '%' # print(kw) chatlist = list( db.select(where=kw, vars=vars, order="ctime DESC", limit=pagesize, offset=offset)) end_time = time.time() cost_time = int((end_time - start_time) * 1000) xutils.log("message search [%s] time %d ms" % (key, cost_time)) if xconfig.search_history is not None: xconfig.search_history.put( Storage(name="#message# %s - %sms" % (key, cost_time), link=web.ctx.fullpath)) else: chatlist = list( db.select(where=kw, vars=vars, order="ctime DESC", limit=pagesize, offset=offset)) chatlist.reverse() amount = db.count(where=kw, vars=vars) page_max = math.ceil(amount / pagesize) chatlist = list(map(process_message, chatlist)) return dict(code="success", message="", data=chatlist, amount=amount, page_max=page_max, current_user=xauth.get_current_name())
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 POST(self): content = xutils.get_argument("content") user = xauth.get_current_user() # chatlist.append(content) db = xtables.get_message_table() ctime = xutils.format_time() db.insert(user=user.get("name"), ctime=ctime, content=content) return dict(code="success", message="", data=dict(user=user.get("name"), content=content, ctime=ctime))
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)) return dict(code="success")
def GET(self): pagesize = xutils.get_argument("pagesize", 20, type=int) page = xutils.get_argument("page", 1, type=int) offset = (page - 1) * pagesize db = xtables.get_message_table() chatlist = list( db.select(order="ctime DESC", limit=pagesize, offset=offset)) chatlist.reverse() page_max = math.ceil(db.count() / pagesize) return dict(code="success", message="", data=chatlist, page_max=page_max, current_user=xauth.get_current_name())
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 search(ctx, text): if not ctx.search_message: return db = xtables.get_message_table() msg_list = list( db.select(where="user=$user AND content like $key", order="ctime DESC", vars=dict(key='%' + text + '%', user=ctx.user_name), limit=1000)) def convert(x): r = SearchResult() r.name = x.ctime r.url = '#' r.command = None r.raw = x.content return r return map(convert, msg_list)
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 get_message_count(user): if user is None: return 0 return xtables.get_message_table().count(where="status=0 AND user=$user", vars=dict(user=user))
def rdb_count_message(user, status): count = xtables.get_message_table().count( where="user=$user AND status=$status", vars=dict(user=user, status=status)) return count