def _send_response(self, status_code, data): """ write result to the client """ self.set_status(status_code) self.set_header("Content-Type", "application/json; charset=UTF-8") self.finish(tool.json_stringify(data))
def do(self, req_data): assert req_data.get("type"), "type cannot be empty" assert req_data.get("to_id"), "to_id cannot be empty" assert req_data.get("text"), "text cannot be empty" request_body = { "type": req_data["type"], "to_id": req_data["to_id"], "text": req_data["text"], } request_body = tool.json_stringify(request_body) print "request_body:", request_body request = tornado.httpclient.HTTPRequest( url=config.API["send_any_message"], method="POST", headers={ "Content-Type": "application/json; charset=UTF-8", }, body=request_body, connect_timeout=15, request_timeout=15) response = yield async_client.fetch(request, raise_error=False) if response.code != 200: raise Exception("http status code %s, %s" % (response.code, response.error)) raise tornado.gen.Return(("success", 1))
def get_wx_content(item): return tool.json_stringify({ "商品名称": item["商品名称"], "min_price": item["min_price"], "current_store": item["current_store"], "promote": item["promote"], "quan": item["quan"], }, indent=2, sort_keys=False)
def get_wx_content(item): return tool.json_stringify({ "name": item["name"], "promote": item["promote"], "quan": item["quan"], "price": item["price"], "store": item["store"], }, indent=2, sort_keys=False)
def get_wx_content(item): return tool.json_stringify( { "name": item["name"], "price": item["price"], "vender": item["vender"], "stock": item["stock"], "promote": item["promote"], "gift": item["gift"], "quan": item["quan"], "feedback": item["feedback"], "ads": item["ads"], "calc_price": item.get("calc_price"), "calc_advice": item.get("calc_advice"), "presale_info": item.get("presale_info"), }, indent=2, sort_keys=False)
def create(self, req_data): # print "create req_data:", req_data assert req_data.get("content"), "content cannot be empty" user_local_datetime = time.strftime(r"%Y-%m-%d %H:%M:%S", time.localtime((req_data["ts"] / 1000))) record_content = { "content": req_data["content"], "width": req_data.get("width"), "height": req_data.get("height"), } record_content = tool.json_stringify(record_content) record_id = yield self.user.record.create(content=record_content, title=req_data["title"], content_type="note", create_datetime=user_local_datetime) self.user.log.create("CREATE_TEXT", record_id, user_local_datetime) raise tornado.gen.Return( "Note created succeed at %s." % user_local_datetime)
def edit(self, req_data): # print "edit req_data:", req_data assert req_data.get("id"), "id cannot be empty" assert req_data.get("content"), "content cannot be empty" user_local_datetime = time.strftime(r"%Y-%m-%d %H:%M:%S", time.localtime((req_data["ts"] / 1000))) record_content = { "content": req_data["content"], "width": req_data.get("width"), "height": req_data.get("height"), } record_content = tool.json_stringify(record_content) yield self.user.record.update(_id=req_data["id"], title=req_data["title"], content=record_content, update_datetime=user_local_datetime) self.user.log.create("EDIT_TEXT", req_data["id"], user_local_datetime) raise tornado.gen.Return( "Note editd succeed at %s." % user_local_datetime)
def send_response(self, status_code, data): self.set_status(status_code) self.set_header("Content-Type", "application/json; charset=UTF-8") self.finish(tool.json_stringify(data))
def on_message(self, message): print "on_message:", type(message), message try: if message is None: self.conf["db_wechai"]["any_message"].insert_one({ "bg-ts": time.time(), "errno": 1, "type": "connection-closed" }) self.on_connection_close("detected.") else: try: json_message = tool.json_load(message) assert isinstance(json_message.get("data"), dict) except Exception: json_message = { "errno": 2, "type": "message", "text": message, "data": {}, } json_message["bg-ts"] = time.time() try: yield self.conf["db_wechai"]["any_message"].insert_one( json_message) except Exception: print traceback.format_exc() return # do nothing (two ai will dead lock this produring message) try: if json_message["data"].get("self") is False: message_type = json_message["data"].get( "room") and "room-message" or "message" to_id = json_message["data"].get( "room") or json_message["data"]["from"] res_message = { "type": message_type, "to_id": to_id, "text": "%s (%s) said %s at %s" % (json_message["data"].get("from_nick"), json_message["data"]["from"], json_message["data"]["text"], json_message["data"]["date"]), } print "res_message:", res_message self.conf["ws_conn"].write_message( tool.json_stringify(res_message)) except Exception: print traceback.format_exc() except Exception: print traceback.format_exc()
def write_message(self, message): return self.conf["ws_conn"].write_message(tool.json_stringify(message))