def get(self): shop_id = self.get_argument('shop_id') product_id = self.get_argument('product_id') product_name = self.get_argument('product_name') shop = self.db.get('select name from supplier_shop where id = %s', shop_id) taobao_sku_list = build_taobao_sku(self.db, shop_id, product_id) title = shop.name + '-' + product_name.encode('utf-8') app_info = json.loads(self.db.get('select taobao_api_info from distributor_shop where id = %s', options.shop_id_taobao).taobao_api_info, object_hook=json_hook) attribute_request = Taobao('taobao.itemprops.get') attribute_request.set_app_info(app_info.app_key, app_info.app_secret_key) attribute_request.set_session(app_info.session) response = yield attribute_request(cid=50644003, fields='pid,name,must,multi,prop_values,is_input_prop,features') attribute_request.parse_response(response.body) if attribute_request.is_ok() and 'item_props' in attribute_request.message: if 'item_prop' in attribute_request.message.item_props: attributes = attribute_request.message.item_props.item_prop else: attributes = [] else: attributes = [] self.render('ktv/manage/publish.html', taobao_sku_list=taobao_sku_list, title=title, shop_id=shop_id, product_id=product_id, attributes=json_dumps(attributes))
def get(self): shop_id = self.get_argument('shop_id') product_id = self.get_argument('product_id') product_name = self.get_argument('product_name') shop = self.db.get('select name from supplier_shop where id = %s', shop_id) taobao_sku_list = build_taobao_sku(self.db, shop_id, product_id) title = shop.name + '-' + product_name.encode('utf-8') app_info = json.loads(self.db.get( 'select taobao_api_info from distributor_shop where id = %s', options.shop_id_taobao).taobao_api_info, object_hook=json_hook) attribute_request = Taobao('taobao.itemprops.get') attribute_request.set_app_info(app_info.app_key, app_info.app_secret_key) attribute_request.set_session(app_info.session) response = yield attribute_request( cid=50644003, fields='pid,name,must,multi,prop_values,is_input_prop,features') attribute_request.parse_response(response.body) if attribute_request.is_ok( ) and 'item_props' in attribute_request.message: if 'item_prop' in attribute_request.message.item_props: attributes = attribute_request.message.item_props.item_prop else: attributes = [] else: attributes = [] self.render('ktv/manage/publish.html', taobao_sku_list=taobao_sku_list, title=title, shop_id=shop_id, product_id=product_id, attributes=json_dumps(attributes))
def post(self): params = dict([(name, self.get_argument(name).encode('utf-8')) for name in self.request.arguments]) #判断是否存在对应的商品,如果没有则添加 goods = self.db.get('select goods_id from ktv_product_goods where product_id = %s and shop_id = %s', params['product_id'], params['shop_id']) shop = self.db.get('select * from supplier_shop where id = %s', params['shop_id']) product = self.db.get('select * from ktv_product where id = %s', params['product_id']) taobao_sku_list = build_taobao_sku(self.db, shop.id, product.id) props = '' if 'attr-name' in self.request.arguments: for pid in self.request.arguments['attr-name']: if pid in self.request.arguments: for i in self.request.arguments[pid]: props += pid + ':' + i + ';' if not goods: shop_name = self.db.get('select name from supplier_shop where id = %s', params['shop_id'])['name'] sql = """insert into goods(supplier_id, sales_price, face_value, purchase_price, category_id, name, short_name, sms_name, created_at, expire_at, type, detail, created_by, generate_type, all_shop) values(%s, 1, 1, 1, 1021, %s, %s, %s, NOW(), NOW(), 'E', 'ktv产品', %s, 'GENERATE', 0)""" combo = shop_name + product['name'] param = [self.current_user.supplier_id, combo, combo, combo, self.current_user.name] goods_id = self.db.execute(sql, *param) self.db.execute('insert into goods_property(goods_id, name, value) values(%s, "ktv", 1)', goods_id) self.db.execute('insert into goods_property(goods_id, name, value) values(%s, "hidden", 1)', goods_id) self.db.execute('insert into ktv_product_goods(shop_id, product_id, goods_id, created_at) ' 'values(%s, %s, %s, NOW())', params['shop_id'], params['product_id'], goods_id) self.db.execute('insert into goods_supplier_shop(goods_id,supplier_shop_id) values(%s, %s)', goods, shop.id) else: goods_id = goods.goods_id # shop_id 如果是银乐迪,指定为31, 否则默认是券生活8的id(13) shop_id = {629: 31}.get(self.current_user.supplier_id, options.shop_id_taobao) dg = alloc_distributor_goods(self.db, goods_id, shop_id) outer_id = str(dg.goods_link_id) sku_properties = [] sku_quantities = [] sku_prices = [] sku_outer_ids = [] props_set = [] goods_number = 0 min_price = 100000 max_price = 0 room_type_taobao_info = {'MINI': '27426219:6312905', 'SMALL': '27426219:3442354', 'MIDDLE': '27426219:6769368', 'LARGE': '27426219:3374388', 'DELUXE': '27426219:40867986'} for taobao_sku in taobao_sku_list: sku_properties.append(get_taobao_propertities(taobao_sku)) sku_quantities.append(taobao_sku.quantity) sku_prices.append(taobao_sku.price) sku_outer_ids.append(get_taobao_outer_id(taobao_sku)) for i in sku_quantities: goods_number += i props_set = remove_repeat(props_set, room_type_taobao_info[taobao_sku.room_type]) if taobao_sku.price < min_price: min_price = taobao_sku.price if taobao_sku.price > max_price: max_price = taobao_sku.price props += ';'.join(props_set) sku_properties_str = ','.join(sku_properties) # sku_properties_str = sku_properties_str.encode('utf-8') sku_quantities_str = ','.join([str(i) for i in sku_quantities]).encode('utf-8') sku_prices_str = ','.join([str(i) for i in sku_prices]).encode('utf-8') sku_outer_ids_str = ','.join(sku_outer_ids).encode('utf-8') input_pid = [] input_s = [] # props += self.get_argument('face_value') + ':' + str(int(max_price) * 1.5) + ';' # 增加品牌、省份、城市属性 props += ';' + params['ktv_brand'] + ':' + params['brand'] + ';' ktv_province = self.request.arguments['ktv_provinces'] for province_item in ktv_province: props += params['ktv_province'] + ':' + province_item + ';' ktv_cities = self.request.arguments['ktv_cities'] for city_item in ktv_cities: props += params['ktv_city'] + ':' + city_item + ';' merchant = params['merchant'] input_pid.append(self.get_argument('face_value')) input_s.append(str(int(int(max_price) * 1.5))) input_pids = ','.join(input_pid).encode('utf-8') input_str = ','.join(input_s).encode('utf-8') app_info = json.loads(self.db.get('select taobao_api_info from distributor_shop where id = %s', options.shop_id_taobao).taobao_api_info, object_hook=json_hook) goods_push = Taobao('taobao.item.add') goods_push.set_app_info(app_info.app_key, app_info.app_secret_key) goods_push.set_session(app_info.session) publish = self.get_argument('publish', 0) image = img_url(params['img_url']) # image = 'http://img0.bdstatic.com/img/image/9196a600c338744ebf8e350016bdbf9d72a6059a745.jpg' if publish: approve_status = 'onsale' else: approve_status = 'instock' args = { 'num': goods_number, 'price': min_price, 'type': 'fixed', 'image': image, 'stuff_status': 'news', 'title': params['title'], 'location__state': params['location_state'], 'location__city': params['location_city'], 'cid': 50644003, 'approve_status': approve_status, 'desc': params['desc'], 'props': props, 'sku_properties': sku_properties_str, 'sku_quantities': sku_quantities_str, 'sku_prices': sku_prices_str, 'sku_outer_ids': sku_outer_ids_str, 'input_pids': input_pids, 'input_str': input_str, 'outer_id': outer_id, 'locality_life__merchant': merchant, 'locality_life__choose_logis': '0', 'locality_life__expirydate': '30', 'locality_life__onsale_auto_refund_ratio': 100 } response = yield goods_push(**args) goods_push.parse_response(response.body) if goods_push.is_ok(): message = '发布成功' self.db.execute('update goods_distributor_shop set status="PENDING", created_by=%s, created_at=NOW(), ' 'distributor_goods_id = %s where goods_id=%s and distributor_shop_id=%s and ' 'goods_link_id=%s', self.current_user.name, goods_push.message.item.num_iid, goods_id, shop_id, dg.goods_link_id) else: err_msg = goods_push.error.sub_msg if 'sub_msg' in goods_push.error else goods_push.error.msg message = '发布失败:' + err_msg.encode('utf-8') self.render('ktv/manage/result.html', message=message)
def sync(db, products): for product in products: if not product.taobao_api_info: logging.error('sync_ktv_sku: invalid taobao_api_info. product_id: %s', product.product_id) continue taobao_api_info = json.loads(product.taobao_api_info, object_hook=json_hook) iid = product.distributor_goods_id taobao = Taobao('taobao.item.get') taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key) response = taobao.sync_fetch(fields='sku', num_iid=iid) taobao.parse_response(response) if not taobao.is_ok(): logging.error('sync_ktv_sku: get taobao item info failed. goods_distributor_shop_id: %s', product.gds_id) if taobao.error.sub_code == 'isv.item-get-service-error:ITEM_NOT_FOUND': # 商品在淘宝不存在,于是在我们系统中设置为逻辑删除 logging.error('sync_ktv_sku: product not exist. goods_distributor_shop_id: %s', product.gds_id) db.execute('update goods_distributor_shop set deleted=1 where id=%s', product.gds_id) continue local_sku_list = build_taobao_sku(db, product.shop_id, product.product_id) remote_sku_list = [] invalid_skus = [] if 'skus' in taobao.message.item and 'sku' in taobao.message.item.skus: for sku in taobao.message.item.skus.sku: tmp = sku_split_re.split(sku.properties_name) # 例: 27426219:3442354:包厢房型:小包;-1:-1:欢唱时间:17点至20点;-2:-45:日期:12月11日(周三) tb_sku = TaobaoSku( room_type='', dt=None, price=Decimal(sku.price), quantity=sku.quantity, start_time=0, duration=0, sku_id=sku.sku_id ) if len(tmp) != 12: invalid_skus.append(tb_sku) continue tb_sku.parse_taobao_property(tmp[3].encode('utf-8'), tmp[7].encode('utf-8'), tmp[11].encode('utf-8')) remote_sku_list.append(tb_sku) add_sku_list, delete_sku_list, update_price_sku_list, update_quantity_sku_list = diff_local_and_remote( local_sku_list, remote_sku_list ) delete_sku_list.extend(invalid_skus) remote_price_set = set([sku.price for sku in remote_sku_list]) for sku in delete_sku_list: if sku.price in remote_price_set: remote_price_set.remove(sku.price) taobao = Taobao('taobao.item.sku.delete') taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key) taobao.set_session(taobao_api_info.session) response = taobao.sync_fetch( num_iid=iid, properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date), item_price=str(min(remote_price_set) if remote_price_set else sku.price) ) taobao.parse_response(response) logging.info('sync_ktv_sku: %s DELETE[%s] %s %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL', sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8')) for sku in add_sku_list: remote_price_set.add(sku.price) taobao = Taobao('taobao.item.sku.add') taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key) taobao.set_session(taobao_api_info.session) end = sku.start_time + sku.duration end = end - 24 if end >= 24 else end response = taobao.sync_fetch( num_iid=iid, properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date), quantity=sku.quantity, price=sku.price, outer_id=sku.room_type+sku.date.strftime('%Y%m%d')+str(sku.start_time*100+end), item_price=str(min(remote_price_set)) ) taobao.parse_response(response) logging.info('sync_ktv_sku: %s ADD[%s] %s %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL', sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8')) if update_quantity_sku_list: taobao = Taobao('taobao.skus.quantity.update') taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key) taobao.set_session(taobao_api_info.session) response = taobao.sync_fetch( num_iid=iid, skuid_quantities=';'.join(['%s:%s' % (sku.sku_id, sku.quantity) for sku in update_quantity_sku_list]) ) taobao.parse_response(response) logging.info('sync_ktv_sku: UPDATE_QUANTITY[%s] %s', 'OK' if taobao.is_ok() else 'FAIL', taobao.get_field('skuid_quantities')) for sku in update_price_sku_list: taobao = Taobao('taobao.item.sku.price.update') taobao.set_app_info(taobao_api_info.app_key, taobao_api_info.app_secret_key) taobao.set_session(taobao_api_info.session) response = taobao.sync_fetch( num_iid=iid, properties='%s;$欢唱时间:%s;$日期:%s' % (sku.room_key, sku.human_time_range, sku.human_date), quantity=sku.quantity, price=sku.price, item_price=sku.price, # 此处不好确定此时的该商品所有SKU的最低价格,所以就填了当前价格 ) taobao.parse_response(response) logging.info('sync_ktv_sku: %s UPDATE_PRICE[%s] %s', product.gds_id, 'OK' if taobao.is_ok() else 'FAIL', sku, '' if taobao.is_ok() else taobao.error.sub_code.encode('utf-8'))
def post(self): params = dict([(name, self.get_argument(name).encode('utf-8')) for name in self.request.arguments]) #判断是否存在对应的商品,如果没有则添加 goods = self.db.get( 'select goods_id from ktv_product_goods where product_id = %s and shop_id = %s', params['product_id'], params['shop_id']) shop = self.db.get('select * from supplier_shop where id = %s', params['shop_id']) product = self.db.get('select * from ktv_product where id = %s', params['product_id']) taobao_sku_list = build_taobao_sku(self.db, shop.id, product.id) props = '' if 'attr-name' in self.request.arguments: for pid in self.request.arguments['attr-name']: if pid in self.request.arguments: for i in self.request.arguments[pid]: props += pid + ':' + i + ';' if not goods: shop_name = self.db.get( 'select name from supplier_shop where id = %s', params['shop_id'])['name'] sql = """insert into goods(supplier_id, sales_price, face_value, purchase_price, category_id, name, short_name, sms_name, created_at, expire_at, type, detail, created_by, generate_type, all_shop) values(%s, 1, 1, 1, 1021, %s, %s, %s, NOW(), NOW(), 'E', 'ktv产品', %s, 'GENERATE', 0)""" combo = shop_name + product['name'] param = [ self.current_user.supplier_id, combo, combo, combo, self.current_user.name ] goods_id = self.db.execute(sql, *param) self.db.execute( 'insert into goods_property(goods_id, name, value) values(%s, "ktv", 1)', goods_id) self.db.execute( 'insert into goods_property(goods_id, name, value) values(%s, "hidden", 1)', goods_id) self.db.execute( 'insert into ktv_product_goods(shop_id, product_id, goods_id, created_at) ' 'values(%s, %s, %s, NOW())', params['shop_id'], params['product_id'], goods_id) self.db.execute( 'insert into goods_supplier_shop(goods_id,supplier_shop_id) values(%s, %s)', goods, shop.id) else: goods_id = goods.goods_id # shop_id 如果是银乐迪,指定为31, 否则默认是券生活8的id(13) shop_id = { 629: 31 }.get(self.current_user.supplier_id, options.shop_id_taobao) dg = alloc_distributor_goods(self.db, goods_id, shop_id) outer_id = str(dg.goods_link_id) sku_properties = [] sku_quantities = [] sku_prices = [] sku_outer_ids = [] props_set = [] goods_number = 0 min_price = 100000 max_price = 0 room_type_taobao_info = { 'MINI': '27426219:6312905', 'SMALL': '27426219:3442354', 'MIDDLE': '27426219:6769368', 'LARGE': '27426219:3374388', 'DELUXE': '27426219:40867986' } for taobao_sku in taobao_sku_list: sku_properties.append(get_taobao_propertities(taobao_sku)) sku_quantities.append(taobao_sku.quantity) sku_prices.append(taobao_sku.price) sku_outer_ids.append(get_taobao_outer_id(taobao_sku)) for i in sku_quantities: goods_number += i props_set = remove_repeat( props_set, room_type_taobao_info[taobao_sku.room_type]) if taobao_sku.price < min_price: min_price = taobao_sku.price if taobao_sku.price > max_price: max_price = taobao_sku.price props += ';'.join(props_set) sku_properties_str = ','.join(sku_properties) # sku_properties_str = sku_properties_str.encode('utf-8') sku_quantities_str = ','.join([str(i) for i in sku_quantities ]).encode('utf-8') sku_prices_str = ','.join([str(i) for i in sku_prices]).encode('utf-8') sku_outer_ids_str = ','.join(sku_outer_ids).encode('utf-8') input_pid = [] input_s = [] # props += self.get_argument('face_value') + ':' + str(int(max_price) * 1.5) + ';' # 增加品牌、省份、城市属性 props += ';' + params['ktv_brand'] + ':' + params['brand'] + ';' ktv_province = self.request.arguments['ktv_provinces'] for province_item in ktv_province: props += params['ktv_province'] + ':' + province_item + ';' ktv_cities = self.request.arguments['ktv_cities'] for city_item in ktv_cities: props += params['ktv_city'] + ':' + city_item + ';' merchant = params['merchant'] input_pid.append(self.get_argument('face_value')) input_s.append(str(int(int(max_price) * 1.5))) input_pids = ','.join(input_pid).encode('utf-8') input_str = ','.join(input_s).encode('utf-8') app_info = json.loads(self.db.get( 'select taobao_api_info from distributor_shop where id = %s', options.shop_id_taobao).taobao_api_info, object_hook=json_hook) goods_push = Taobao('taobao.item.add') goods_push.set_app_info(app_info.app_key, app_info.app_secret_key) goods_push.set_session(app_info.session) publish = self.get_argument('publish', 0) image = img_url(params['img_url']) # image = 'http://img0.bdstatic.com/img/image/9196a600c338744ebf8e350016bdbf9d72a6059a745.jpg' if publish: approve_status = 'onsale' else: approve_status = 'instock' args = { 'num': goods_number, 'price': min_price, 'type': 'fixed', 'image': image, 'stuff_status': 'news', 'title': params['title'], 'location__state': params['location_state'], 'location__city': params['location_city'], 'cid': 50644003, 'approve_status': approve_status, 'desc': params['desc'], 'props': props, 'sku_properties': sku_properties_str, 'sku_quantities': sku_quantities_str, 'sku_prices': sku_prices_str, 'sku_outer_ids': sku_outer_ids_str, 'input_pids': input_pids, 'input_str': input_str, 'outer_id': outer_id, 'locality_life__merchant': merchant, 'locality_life__choose_logis': '0', 'locality_life__expirydate': '30', 'locality_life__onsale_auto_refund_ratio': 100 } response = yield goods_push(**args) goods_push.parse_response(response.body) if goods_push.is_ok(): message = '发布成功' self.db.execute( 'update goods_distributor_shop set status="PENDING", created_by=%s, created_at=NOW(), ' 'distributor_goods_id = %s where goods_id=%s and distributor_shop_id=%s and ' 'goods_link_id=%s', self.current_user.name, goods_push.message.item.num_iid, goods_id, shop_id, dg.goods_link_id) else: err_msg = goods_push.error.sub_msg if 'sub_msg' in goods_push.error else goods_push.error.msg message = '发布失败:' + err_msg.encode('utf-8') self.render('ktv/manage/result.html', message=message)