def POST(self,cid,abid,method=None): ''' '/bfdrest/client/(cid)/ABtest/(abid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) stop = False if method == "stop": stop = True data = web.data() ret = {} rv = json.loads(data) if not SetBannerABtest(cid,abid,rv,stop): ret["success"] = False ret["error_info"] = "can't save banner abtest." return pub.callback(json.dumps(ret),myuuid) rlist = cache.get('%s:ABtest:_all_'%cid) if rlist: rlist = json.loads(rlist) else: rlist = {} rlist[abid] = time.time() rstr = cache.set('%s:ABtest:_all_'%(cid),json.dumps(rlist)) rstr = cache.set('%s:ABtest:%s'%(cid,abid),data) if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't save abtest rule." return pub.callback(json.dumps(ret),myuuid)
def POST(self, cid, abid, method=None): ''' '/bfdrest/client/(cid)/ABtest/(abid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) stop = False if method == "stop": stop = True data = web.data() ret = {} rv = json.loads(data) if not SetBannerABtest(cid, abid, rv, stop): ret["success"] = False ret["error_info"] = "can't save banner abtest." return pub.callback(json.dumps(ret), myuuid) rlist = cache.get('%s:ABtest:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} rlist[abid] = time.time() rstr = cache.set('%s:ABtest:_all_' % (cid), json.dumps(rlist)) rstr = cache.set('%s:ABtest:%s' % (cid, abid), data) if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't save abtest rule." return pub.callback(json.dumps(ret), myuuid)
def GET(self,cid,cat,method = None): ''' /bfdrest/client/(cid)/category/(cat)/all for get all children; /bfdrest/client/(cid)/category/(cat) for get category's child; /bfdrest/client/Czouxiu/category/_all_(/all) for example; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) #print('cid:%s,cat:%s,method;%s' %(cid,cat,method)) def faild(info): ret = copy.deepcopy(GET_ret_template) ret['success'] = False ret['error_info'] = info return json.dumps(ret) tree_path = cat.split(':') if tree_path[0] != "_all_": return pub.callback(faild('No such category'),myuuid) if cid not in client_property_tree: try: client_property_tree[cid] = cPickle.load(open('./data/%s.ptree.pkl'%cid,'rb')) except: client_property_tree[cid] = {"nodes":{}} path_str = 'client_property_tree["%s"]' % cid for node in tree_path[1:]: path_str += '["nodes"][u"%s"]' % node try: #print path_str cur_node = eval(path_str) except Exception,e: err_info = 'No such category. key error: %s' % unicode(e.message).encode("utf-8") return pub.callback(faild(err_info),myuuid)
def DELETE(self, cid, abid): ''' '/bfdrest/client/(cid)/ABtest/(abid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.input() rv = data ret = {} if not SetBannerABtest(cid, abid, rv, False, True): ret["success"] = False ret["error_info"] = "can't delete banner abtest." return pub.callback(json.dumps(ret), myuuid) rlist = cache.get('%s:ABtest:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} if abid in rlist: del rlist[abid] rstr = cache.set('%s:ABtest:_all_' % (cid), json.dumps(rlist)) rstr = cache.delete('%s:ABtest:%s' % (cid, abid)) rstr = cache.delete('%s:ABtestRule:%s' % (cid, abid)) if rstr: ret["success"] = True else: ret["success"] = True ret["error_info"] = "can't delete abtest rule." return pub.callback(json.dumps(ret), myuuid)
def DELETE(self,cid,abid): ''' '/bfdrest/client/(cid)/ABtest/(abid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) data = web.input() rv = data ret = {} if not SetBannerABtest(cid,abid,rv,False,True): ret["success"] = False ret["error_info"] = "can't delete banner abtest." return pub.callback(json.dumps(ret),myuuid) rlist = cache.get('%s:ABtest:_all_'%cid) if rlist: rlist = json.loads(rlist) else: rlist = {} if abid in rlist: del rlist[abid] rstr = cache.set('%s:ABtest:_all_'%(cid),json.dumps(rlist)) rstr = cache.delete('%s:ABtest:%s'%(cid,abid)) rstr = cache.delete('%s:ABtestRule:%s'%(cid,abid)) if rstr: ret["success"] = True else: ret["success"] = True ret["error_info"] = "can't delete abtest rule." return pub.callback(json.dumps(ret),myuuid)
def GET(self, cid, cat, method=None): ''' /bfdrest/client/(cid)/category/(cat)/all for get all children; /bfdrest/client/(cid)/category/(cat) for get category's child; /bfdrest/client/Czouxiu/category/_all_(/all) for example; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) #print('cid:%s,cat:%s,method;%s' %(cid,cat,method)) def faild(info): ret = copy.deepcopy(GET_ret_template) ret['success'] = False ret['error_info'] = info return json.dumps(ret) tree_path = cat.split(':') if tree_path[0] != "_all_": return pub.callback(faild('No such category'), myuuid) if cid not in client_property_tree: try: client_property_tree[cid] = cPickle.load( open('./data/%s.ptree.pkl' % cid, 'rb')) except: client_property_tree[cid] = {"nodes": {}} path_str = 'client_property_tree["%s"]' % cid for node in tree_path[1:]: path_str += '["nodes"][u"%s"]' % node try: #print path_str cur_node = eval(path_str) except Exception, e: err_info = 'No such category. key error: %s' % unicode( e.message).encode("utf-8") return pub.callback(faild(err_info), myuuid)
def GET(self,host,table,key,family,column,proto=None): ''' /bfdrest/bfdhabase2/host/table/key/family/column(/proto) for get key; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) v = libhbase.get_column(key,family,column,host,table) if not v: return pub.callback(v,myuuid) if proto: ib = eval("%s()"%proto) try: ib.ParseFromString(v) except Exception,e: return pub.callback(str(e),myuuid) return pub.callback(json.dumps(proto2json(ib)),myuuid)
def GET(self, host, table, key, family, column, proto=None): ''' /bfdrest/bfdhabase2/host/table/key/family/column(/proto) for get key; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) v = libhbase.get_column(key, family, column, host, table) if not v: return pub.callback(v, myuuid) if proto: ib = eval("%s()" % proto) try: ib.ParseFromString(v) except Exception, e: return pub.callback(str(e), myuuid) return pub.callback(json.dumps(proto2json(ib)), myuuid)
def GET(self,cid,rid=None): ''' '/bfdrest/client/(cid)/operationrules','/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) if rid == None: return pub.callback(json.dumps(GET_ret),myuuid) else: rstr = cache.get('%s:OperatorRules:%s'%(cid,rid)) ret = {} if rstr: ret = json.loads(rstr) ret["success"] = True else: ret["success"] = False ret["error_info"] = "no such operation rule." return pub.callback(json.dumps(ret),myuuid)
def POST(self,cid,rid): ''' '/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) data = web.data() rv = json.loads(data) range_iids = {} #range_iids = goods.generalize_ranges(cid,rv["rec_ranges"]) riids = [] for key in range_iids: riids.extend(range_iids[key]) operation_rule = ParseFromJson(rv) operation_rule.operation_rule_id = rid rlist = cache.get('%s:OperatorRules:_all_'%cid) if rlist: rlist = json.loads(rlist) else: rlist = {} rlist[rid] = time.time() cache.set('%s:OperatorRules:_all_'%(cid),json.dumps(rlist)) cache.set('%s:OperationRule:%s'%(cid,rid),operation_rule.SerializeToString()) rstr = cache.set('%s:OperatorRules:%s'%(cid,rid),data) ret = {} if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't save operation rule." return pub.callback(json.dumps(ret),myuuid)
def DELETE(self,cid,rid): ''' '/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) data = web.input() jdata = data rstr = cache.delete('%s:OperationRule:%s'%(cid,rid)) rstr = cache.delete('%s:OperatorRules:%s'%(cid,rid)) rlist = cache.get('%s:OperatorRules:_all_'%cid) if rlist: rlist = json.loads(rlist) else: rlist = {} if rid in rlist: del rlist[rid] cache.delete('%s:OperatorRulesItems:%s'%(cid,rid)) rstr = cache.set('%s:OperatorRules:_all_'%(cid),json.dumps(rlist)) for p_bid in json.loads(jdata["banner_id"]): p_bid_str = cache.get("%s:RecBanner:%s"%(cid,p_bid)) p_banner = RecBanner_pb2.RecBanner() if p_bid_str: p_banner.ParseFromString(p_bid_str) for i in xrange(len(p_banner.logic_rule.operation_rules)): if p_banner.logic_rule.operation_rules[i].operation_rule == rid: del p_banner.logic_rule.operation_rules[i] break cache.set("%s:RecBanner:%s"%(cid,p_bid),p_banner.SerializeToString()) ret = {} if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't delete operation rule." return pub.callback(json.dumps(ret),myuuid)
def GET(self, cid, bid=None): ''' '/bfdrest/client/(cid)/banners','/bfdrest/client/(cid)/banners/(bid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) if bid == None: return pub.callback(json.dumps(GET_ret), myuuid) else: rstr = cache.get('%s:Banners:%s' % (cid, bid)) ret = {} if rstr: ret = json.loads(rstr) ret["success"] = True else: ret["success"] = False ret["error_info"] = "no such banner id." return pub.callback(json.dumps(ret), myuuid)
def GET(self, cid, rid=None): ''' '/bfdrest/client/(cid)/operationrules','/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) if rid == None: return pub.callback(json.dumps(GET_ret), myuuid) else: rstr = cache.get('%s:OperatorRules:%s' % (cid, rid)) ret = {} if rstr: ret = json.loads(rstr) ret["success"] = True else: ret["success"] = False ret["error_info"] = "no such operation rule." return pub.callback(json.dumps(ret), myuuid)
def POST(self, cid, rid): ''' '/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.data() rv = json.loads(data) range_iids = {} #range_iids = goods.generalize_ranges(cid,rv["rec_ranges"]) riids = [] for key in range_iids: riids.extend(range_iids[key]) operation_rule = ParseFromJson(rv) operation_rule.operation_rule_id = rid rlist = cache.get('%s:OperatorRules:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} rlist[rid] = time.time() cache.set('%s:OperatorRules:_all_' % (cid), json.dumps(rlist)) cache.set('%s:OperationRule:%s' % (cid, rid), operation_rule.SerializeToString()) rstr = cache.set('%s:OperatorRules:%s' % (cid, rid), data) ret = {} if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't save operation rule." return pub.callback(json.dumps(ret), myuuid)
def DELETE(self, cid, goodsid): ''' /bfdrest/client/(cid)/goods/(goodsid) ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.input() jdata = data ret = {} g_str = cache.get('%s:Goods:%s' % (cid, goodsid)) pgs = RecBanner_pb2.Goods() if g_str: pgs.ParseFromString(g_str) if not pgs.is_ranges: rm_goods_solr(cid, goodsid) cache.delete('%s:Goods:%s' % (cid, goodsid)) remove_goods_relation(cid, goodsid, jdata) goods_list = cache.get('%s:Goods:_all_' % cid) if goods_list: goods_list = json.loads(goods_list) else: goods_list = {} if goodsid in goods_list: del goods_list[goodsid] cache.set('%s:Goods:_all_' % cid, json.dumps(goods_list)) ret['success'] = True return pub.callback(json.dumps(ret), myuuid)
def GET(self,cid,abid=None): ''' '/bfdrest/client/(cid)/ABtest','/bfdrest/client/(cid)/ABtest/(ABid)' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) if abid == None: return pub.callback(json.dumps(GET_ret),myuuid) else: rstr = cache.get('%s:ABtest:%s'%(cid,abid)) if abid == "_all_": return rstr ret = {} if rstr: ret = json.loads(rstr) ret["success"] = True else: ret["success"] = False ret["error_info"] = "no such ABtest id." return pub.callback(json.dumps(ret),myuuid)
def GET(self,n,b,k,proto=None): ''' /bfdrest/bfdcache/namesapce/business/key for get key; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) v = cache.get(k,n,b) # print '%s\t%s\t%s' % (n,b,k) if proto: if proto == 'DELETE': return pub.callback(json.dumps(cache.delete(k,n,b)),myuuid) if v == None or v == 'None': return pub.callback(json.dumps({}),myuuid) ib = eval("%s()"%proto) try: ib.ParseFromString(v) except Exception,e: return pub.callback(str(e),myuuid) return pub.callback(json.dumps(proto2json(ib)),myuuid)
def GET(self, cid, abid=None): ''' '/bfdrest/client/(cid)/ABtest','/bfdrest/client/(cid)/ABtest/(ABid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) if abid == None: return pub.callback(json.dumps(GET_ret), myuuid) else: rstr = cache.get('%s:ABtest:%s' % (cid, abid)) if abid == "_all_": return rstr ret = {} if rstr: ret = json.loads(rstr) ret["success"] = True else: ret["success"] = False ret["error_info"] = "no such ABtest id." return pub.callback(json.dumps(ret), myuuid)
def GET(self, n, b, k, proto=None): ''' /bfdrest/bfdcache/namesapce/business/key for get key; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) v = cache.get(k, n, b) # print '%s\t%s\t%s' % (n,b,k) if proto: if proto == 'DELETE': return pub.callback(json.dumps(cache.delete(k, n, b)), myuuid) if v == None or v == 'None': return pub.callback(json.dumps({}), myuuid) ib = eval("%s()" % proto) try: ib.ParseFromString(v) except Exception, e: return pub.callback(str(e), myuuid) return pub.callback(json.dumps(proto2json(ib)), myuuid)
def GET(self,cid): ''' /bfdrest/client/(cid)/property like /bfdrest/client/Czouxiu/property ''' ret = { "success" : True, "error_info" : "", "main_property" : {"pid":"分类"}, "properties" : {"pid":"分类","brand":"品牌"} } #data = web.input() #callback = "" # if 'callback' in data: # callback = data['callback'] if cid == "Czouxiu" or cid == "Ctest_ifec": return pub.callback(json.dumps(ret)) else: ret = {} ret["success"] = False ret["error_info"] = "No such clent. client: " + cid return pub.callback(json.dumps(ret))
def GET(self, cid, goodsid=None, range=None): ''' /bfdrest/client/(cid)/goods/(goodsid)/(items) ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) if goodsid == None: if cid in goods_fields_conf: return pub.callback(json.dumps(goods_fields_conf[cid]), myuuid) return pub.callback(json.dumps(goods_fields), myuuid) elif goodsid == "type": return pub.callback(json.dumps(type_define), myuuid) else: gv = cache.get('%s:Goods:%s' % (cid, goodsid)) if gv: ret = json.loads(gv) ret['success'] = True if range == None: if 'keys' in ret: del ret['keys'] pass return pub.callback(json.dumps(ret), myuuid) else: iids = [] # 获取范围的item for key in [goodsid]: ids_str = cache.get('%s:GoodsItems:%s' % (cid, key)) if ids_str: iids.extend(json.loads(ids_str)) return pub.callback(json.dumps(iids), myuuid) else: ret = {} ret['success'] = False ret['error_info'] = 'no such goods id.' return pub.callback(json.dumps(ret), myuuid)
def GET(self, method=None, num=10): ''' '/error/(log)/(nums)','/error' ''' if method == 'log': num = int(num) log = [] log.extend( urllib2.urlopen('http://192.168.49.90:9090/error/read/%d' % num).readlines()) log.extend( urllib2.urlopen('http://192.168.49.91:9090/error/read/%d' % num).readlines()) return ''.join(sorted(log)[-num:]) if method == 'read': num = int(num) log = open('bfdrest_error.log', 'r').readlines()[-num:] return ''.join(log) args = web.input() env = web.ctx['env'] def parsedict(d): if isinstance(d, dict): r = {} for key in d: r[key] = parsedict(d[key]) return r else: return str(d) ret = { 'args': args, 'HTTP_COOKIE': parsedict(env['HTTP_COOKIE']), 'HTTP_USER_AGENT': parsedict(env['HTTP_USER_AGENT']), 'HTTP_X_REAL_IP': parsedict(env['HTTP_X_REAL_IP']), #'HTTP_X_REAL_IP':parsedict(env['REMOTE_ADDR']), } cookies = ret['HTTP_COOKIE'].split(';') gid = '' for i in xrange(len(cookies)): cookie = cookies[i].strip() if cookie.startswith('bfdid='): gid = cookie[6:] ip = ret['HTTP_X_REAL_IP'] user_agent = ret['HTTP_USER_AGENT'] msg = ret['args'].get('msg') error_logger.info(json.dumps(ret)) sql = "insert into error_msg (gid,ip,user_agent,msg) values ('%s','%s','%s','%s')" % ( gid, ip, user_agent, msg) print sql mysql.execute(sql) return callback('[0,"OK"]')
def GET(self,path): ''' /bfdrest/bfdzk/zkpath; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) ret = {} try: data = zk.get(path) try: data = json.loads(data) except: pass ret['data'] = data ret['children'] = zk.get_children(path) except: pass if len(ret) == 0: ret = None return pub.callback(json.dumps(ret),myuuid)
def GET(self, path): ''' /bfdrest/bfdzk/zkpath; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) ret = {} try: data = zk.get(path) try: data = json.loads(data) except: pass ret['data'] = data ret['children'] = zk.get_children(path) except: pass if len(ret) == 0: ret = None return pub.callback(json.dumps(ret), myuuid)
def POST(self, cid, goodsid): ''' /bfdrest/client/(cid)/goods/(goodsid) ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) value = web.data() put_value = {} ret = {} data = {} pgs = RecBanner_pb2.Goods() g_str = cache.get('%s:Goods:%s' % (cid, goodsid)) if g_str: pgs.ParseFromString(g_str) pgs.goods_id = goodsid try: put_value = json.loads(value) except Exception, e: ret["success"] = False ret["error_info"] = "wrong json format: %s" % str(e) return pub.callback(json.dumps(ret), myuuid)
def DELETE(self, cid, bid): ''' '/bfdrest/client/(cid)/banners/(bid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) rlist = cache.get('%s:Banners:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} if bid in rlist: del rlist[bid] cache.set('%s:Banners:_all_' % (cid), json.dumps(rlist)) rstr = cache.delete('%s:Banners:%s' % (cid, bid)) rstr = cache.delete('%s:RecBanner:%s' % (cid, bid)) ret = {} if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't delete banner info." return pub.callback(json.dumps(ret), myuuid)
def POST(self, cid, bid): ''' '/bfdrest/client/(cid)/banners/(bid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.data() ret = {} rv = json.loads(data) rec_banner = ParseFromJson(cid, bid, rv) rlist = cache.get('%s:Banners:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} rlist[bid] = time.time() rstr = cache.set('%s:Banners:_all_' % (cid), json.dumps(rlist)) rstr = cache.set('%s:RecBanner:%s' % (cid, bid), rec_banner.SerializeToString()) rstr = cache.set('%s:Banners:%s' % (cid, bid), data) if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't save banner info." return pub.callback(json.dumps(ret), myuuid)
def DELETE(self, cid, rid): ''' '/bfdrest/client/(cid)/operationrules/(rid)' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.input() jdata = data rstr = cache.delete('%s:OperationRule:%s' % (cid, rid)) rstr = cache.delete('%s:OperatorRules:%s' % (cid, rid)) rlist = cache.get('%s:OperatorRules:_all_' % cid) if rlist: rlist = json.loads(rlist) else: rlist = {} if rid in rlist: del rlist[rid] cache.delete('%s:OperatorRulesItems:%s' % (cid, rid)) rstr = cache.set('%s:OperatorRules:_all_' % (cid), json.dumps(rlist)) for p_bid in json.loads(jdata["banner_id"]): p_bid_str = cache.get("%s:RecBanner:%s" % (cid, p_bid)) p_banner = RecBanner_pb2.RecBanner() if p_bid_str: p_banner.ParseFromString(p_bid_str) for i in xrange(len(p_banner.logic_rule.operation_rules)): if p_banner.logic_rule.operation_rules[ i].operation_rule == rid: del p_banner.logic_rule.operation_rules[i] break cache.set("%s:RecBanner:%s" % (cid, p_bid), p_banner.SerializeToString()) ret = {} if rstr: ret["success"] = True else: ret["success"] = False ret["error_info"] = "can't delete operation rule." return pub.callback(json.dumps(ret), myuuid)
def DELETE(self,n,b,k): myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) return pub.callback(cache.delete(k,n,b),myuuid)
def POST(self,n,b,k): myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) data = web.data() return pub.callback(cache.set(k,data,n,b),myuuid)
''' /bfdrest/bfdcache/namesapce/business/key for get key; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) v = cache.get(k,n,b) # print '%s\t%s\t%s' % (n,b,k) if proto: if proto == 'DELETE': return pub.callback(json.dumps(cache.delete(k,n,b)),myuuid) if v == None or v == 'None': return pub.callback(json.dumps({}),myuuid) ib = eval("%s()"%proto) try: ib.ParseFromString(v) except Exception,e: return pub.callback(str(e),myuuid) return pub.callback(json.dumps(proto2json(ib)),myuuid) else: if v == None or v == 'None': return pub.callback(json.dumps(None),myuuid) return pub.callback(v,myuuid) def POST(self,n,b,k): myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) data = web.data() return pub.callback(cache.set(k,data,n,b),myuuid) def DELETE(self,n,b,k): myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) return pub.callback(cache.delete(k,n,b),myuuid)
class handler: def GET(self, cid, goodsid=None, range=None): ''' /bfdrest/client/(cid)/goods/(goodsid)/(items) ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) if goodsid == None: if cid in goods_fields_conf: return pub.callback(json.dumps(goods_fields_conf[cid]), myuuid) return pub.callback(json.dumps(goods_fields), myuuid) elif goodsid == "type": return pub.callback(json.dumps(type_define), myuuid) else: gv = cache.get('%s:Goods:%s' % (cid, goodsid)) if gv: ret = json.loads(gv) ret['success'] = True if range == None: if 'keys' in ret: del ret['keys'] pass return pub.callback(json.dumps(ret), myuuid) else: iids = [] # 获取范围的item for key in [goodsid]: ids_str = cache.get('%s:GoodsItems:%s' % (cid, key)) if ids_str: iids.extend(json.loads(ids_str)) return pub.callback(json.dumps(iids), myuuid) else: ret = {} ret['success'] = False ret['error_info'] = 'no such goods id.' return pub.callback(json.dumps(ret), myuuid) def POST(self, cid, goodsid): ''' /bfdrest/client/(cid)/goods/(goodsid) ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) value = web.data() put_value = {} ret = {} data = {} pgs = RecBanner_pb2.Goods() g_str = cache.get('%s:Goods:%s' % (cid, goodsid)) if g_str: pgs.ParseFromString(g_str) pgs.goods_id = goodsid try: put_value = json.loads(value) except Exception, e: ret["success"] = False ret["error_info"] = "wrong json format: %s" % str(e) return pub.callback(json.dumps(ret), myuuid) if put_value.has_key('name'): data['name'] = put_value['name'] pgs.name = put_value['name'] else: data['name'] = "" if put_value.has_key('description'): data['description'] = put_value['description'] pgs.description = put_value['description'] else: data['description'] = "" if "user_level" in put_value: data["user_level"] = put_value["user_level"] pgs.user_level = put_value["user_level"] pgs.is_ranges = False if put_value.has_key('items') and len(put_value['items']) > 0: ret["success"] = True # save goods data['items'] = put_value['items'] # 待添加具体功能,更新ItemBase,更新商品库商品 rm_goods_solr(cid, goodsid, data['items']) iids, info = generalize_items(cid, goodsid, data['items']) data['keys'] = [goodsid] del data['items'] goods_list = cache.get('%s:Goods:_all_' % cid) if goods_list: goods_list = json.loads(goods_list) else: goods_list = {} goods_list[goodsid] = time.time() cache.set('%s:Goods:_all_' % cid, json.dumps(goods_list)) cache.set('%s:GoodsItems:%s' % (cid, goodsid), json.dumps(iids)) cache.set('%s:Goods:%s' % (cid, goodsid), pgs.SerializeToString()) return pub.callback(json.dumps(info), myuuid) elif put_value.has_key('ranges') and len(put_value['ranges']) > 0: pgs.is_ranges = True for i in xrange(len(pgs.ranges)): del pgs.ranges[0] ret["success"] = True # save goods data['ranges'] = put_value['ranges'] def parse_item_range(jir, has_ratio=False): ir = RecBanner_pb2.ItemRange() if len(jir) > 0: ir.type = jir[0][0] for pv in jir: if len(pv) > 1 and pv[1] != "_all_": eval("ir.%s.append(pv[1])" % pv[0]) return ir for range in put_value['ranges']: pgs.ranges.add() pgs.ranges[len(pgs.ranges) - 1].CopyFrom( parse_item_range(range)) #rm_goods_solr(cid,goodsid) range_iids = generalize_ranges(cid, data['ranges']) data["keys"] = range_iids.keys() iids = [] for key in range_iids: iids.extend(range_iids[key]) goods_list = cache.get('%s:Goods:_all_' % cid) if goods_list: goods_list = json.loads(goods_list) else: goods_list = {} goods_list[goodsid] = time.time() cache.set('%s:Goods:_all_' % cid, json.dumps(goods_list)) cache.set('%s:GoodsItems:%s' % (cid, goodsid), json.dumps(iids)) cache.set('%s:Goods:%s' % (cid, goodsid), pgs.SerializeToString()) # 待添加具体功能,更新商品库商品 return pub.callback(json.dumps(ret), myuuid) else: ret["success"] = True cache.set('%s:Goods:%s' % (cid, goodsid), pgs.SerializeToString()) return pub.callback(json.dumps(ret), myuuid)
def GET(self, h, p, n, k): ''' /bfdrest/bfdredis/redis-server/port/db/key for get key; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) return pub.callback(get(h, p, n, k), myuuid)
def GET(self): ''' '/wx/qr' ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) args = web.input() if "method" in args: method = args["method"] if method == "getQrTicket": web.header('Content-Type', 'application/javascript') data = args["data"] ret = urllib2.urlopen( "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=FoYOQEtQ-QHjnSv9tPDqpGOA49AFUZNqnQtPhhlRq_8i9XfxII3ofQGVtYpB6rjv", data) retstr = ret.read() return pub.callback(retstr, myuuid) elif method == "getQr": gid = web.cookies().get("bfdid") print gid param_index = int(cache.get("G:weixin:paramindex")) web.header('Content-Type', 'image/png') if param_index == None: param_index = 0 cache.set("G:weixin:paramindex", "0") else: param_index = (int(param_index) + 1) % 100 cache.set("G:weixin:paramindex", str(param_index)) access_token = cache.get("G:weixin:access_token") data = { "expire_seconds": "1800", "action_name": "QR_SCENE", "action_info": { "scene": { "scene_id": str(param_index) } } } def getTicket(access_token): ret = urllib2.urlopen( "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + access_token, json.dumps(data)) retstr = ret.read() ticket = json.loads(retstr)["ticket"] return ticket try: ticket = getTicket(access_token) except: appid = "wx68cc9091ef2be695" secret = "c27b72cd1d51114cda5e816bbb95ab6d" get_access_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % ( appid, secret) access_token_ret = urllib2.urlopen(get_access_url) access_token = json.loads( access_token_ret.read())["access_token"] cache.set("G:weixin:access_token", access_token) ticket = getTicket(access_token) qrurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s" % ticket qrimg = urllib2.urlopen(qrurl) img = qrimg.read() cache.set("G:weixin:scene:%d" % param_index, gid) return img elif method == "getgid": web.header('Content-Type', 'application/javascript') open_id = args["open_id"] cid = args["cid"] if "scene_id" in args: scene_id = args["scene_id"] gid = cache.get("G:weixin:scene:%s" % scene_id) cache.set("G:weixin:openid:%s:%s" % (cid, open_id), gid) return pub.callback('"%s"' % gid, myuuid) else: gid = cache.get("G:weixin:openid:%s:%s" % (cid, open_id)) return pub.callback('"%s"' % gid, myuuid) elif method == "getopenid": web.header('Content-Type', 'application/javascript') code = args['code'] access_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx68cc9091ef2be695&secret=c27b72cd1d51114cda5e816bbb95ab6d&code=%s&grant_type=authorization_code' % code ret = urllib2.urlopen(access_url) return pub.callback(ret.read(), myuuid) else: web.header('Content-Type', 'application/javascript') return pub.callback(json.dumps(None), myuuid)
def GET(self,h,p,n,k): ''' /bfdrest/bfdredis/redis-server/port/db/key for get key; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) return pub.callback(get(h,p,n,k),myuuid)
else: rj[field[0].name] = field[1].encode('utf-8') elif field[0].label == field[0].LABEL_REPEATED: rj[field[0].name] = [] for msg in field[1]: rj[field[0].name].append(proto2json(msg)) else: rj[field[0].name] = field[1] print rj return rj class handler: def GET(self, host, table, key, family, column, proto=None): ''' /bfdrest/bfdhabase2/host/table/key/family/column(/proto) for get key; ''' myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) v = libhbase.get_column(key, family, column, host, table) if not v: return pub.callback(v, myuuid) if proto: ib = eval("%s()" % proto) try: ib.ParseFromString(v) except Exception, e: return pub.callback(str(e), myuuid) return pub.callback(json.dumps(proto2json(ib)), myuuid) else: return pub.callback(v, myuuid)
rj[field[0].name].append(msg.encode('utf-8')) else: rj[field[0].name] = field[1].encode('utf-8') elif field[0].label == field[0].LABEL_REPEATED: rj[field[0].name] = [] for msg in field[1]: rj[field[0].name].append(proto2json(msg)) else: rj[field[0].name] = field[1] print rj return rj class handler: def GET(self,host,table,key,family,column,proto=None): ''' /bfdrest/bfdhabase2/host/table/key/family/column(/proto) for get key; ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) v = libhbase.get_column(key,family,column,host,table) if not v: return pub.callback(v,myuuid) if proto: ib = eval("%s()"%proto) try: ib.ParseFromString(v) except Exception,e: return pub.callback(str(e),myuuid) return pub.callback(json.dumps(proto2json(ib)),myuuid) else: return pub.callback(v,myuuid)
myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) v = cache.get(k, n, b) # print '%s\t%s\t%s' % (n,b,k) if proto: if proto == 'DELETE': return pub.callback(json.dumps(cache.delete(k, n, b)), myuuid) if v == None or v == 'None': return pub.callback(json.dumps({}), myuuid) ib = eval("%s()" % proto) try: ib.ParseFromString(v) except Exception, e: return pub.callback(str(e), myuuid) return pub.callback(json.dumps(proto2json(ib)), myuuid) else: if v == None or v == 'None': return pub.callback(json.dumps(None), myuuid) return pub.callback(v, myuuid) def POST(self, n, b, k): myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.data() return pub.callback(cache.set(k, data, n, b), myuuid) def DELETE(self, n, b, k): myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) return pub.callback(cache.delete(k, n, b), myuuid)
def DELETE(self, n, b, k): myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) return pub.callback(cache.delete(k, n, b), myuuid)
def GET(self): ''' '/wx/qr' ''' myuuid = uuid.uuid1();LOG("info",_myuuid=myuuid) args = web.input() if "method" in args: method = args["method"] if method == "getQrTicket": web.header('Content-Type', 'application/javascript') data = args["data"] ret = urllib2.urlopen( "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=FoYOQEtQ-QHjnSv9tPDqpGOA49AFUZNqnQtPhhlRq_8i9XfxII3ofQGVtYpB6rjv", data) retstr = ret.read() return pub.callback(retstr,myuuid) elif method == "getQr": gid = web.cookies().get("bfdid") print gid param_index = int(cache.get("G:weixin:paramindex")) web.header('Content-Type', 'image/png') if param_index == None: param_index = 0 cache.set("G:weixin:paramindex","0") else: param_index = (int(param_index) + 1)%100 cache.set("G:weixin:paramindex",str(param_index)) access_token = cache.get("G:weixin:access_token") data = {"expire_seconds":"1800","action_name":"QR_SCENE","action_info":{"scene":{"scene_id":str(param_index)}}} def getTicket(access_token): ret = urllib2.urlopen( "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+access_token, json.dumps(data)) retstr = ret.read() ticket = json.loads(retstr)["ticket"] return ticket try: ticket = getTicket(access_token) except: appid = "wx68cc9091ef2be695" secret = "c27b72cd1d51114cda5e816bbb95ab6d" get_access_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (appid,secret) access_token_ret = urllib2.urlopen(get_access_url) access_token = json.loads(access_token_ret.read())["access_token"] cache.set("G:weixin:access_token",access_token) ticket = getTicket(access_token) qrurl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s"%ticket qrimg = urllib2.urlopen(qrurl) img = qrimg.read() cache.set("G:weixin:scene:%d"%param_index,gid) return img elif method == "getgid": web.header('Content-Type', 'application/javascript') open_id = args["open_id"] cid = args["cid"] if "scene_id" in args: scene_id = args["scene_id"] gid = cache.get("G:weixin:scene:%s"%scene_id) cache.set("G:weixin:openid:%s:%s"%(cid,open_id),gid) return pub.callback('"%s"'%gid,myuuid) else: gid = cache.get("G:weixin:openid:%s:%s"%(cid,open_id)) return pub.callback('"%s"'%gid,myuuid) elif method == "getopenid": web.header('Content-Type', 'application/javascript') code = args['code'] access_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx68cc9091ef2be695&secret=c27b72cd1d51114cda5e816bbb95ab6d&code=%s&grant_type=authorization_code' % code ret = urllib2.urlopen(access_url) return pub.callback(ret.read(),myuuid) else: web.header('Content-Type', 'application/javascript') return pub.callback(json.dumps(None),myuuid)
def POST(self, n, b, k): myuuid = uuid.uuid1() LOG("info", _myuuid=myuuid) data = web.data() return pub.callback(cache.set(k, data, n, b), myuuid)