Commit 8c29e159 authored by 王浩's avatar 王浩

Merge branch 'test' into 'master'

pin

See merge request alpha/saturn!78
parents fd8fd189 51ece362
......@@ -5,6 +5,7 @@ from .views import user
from .views import topic
from .views import tag
from .views import reply
from .views import product
urlpatterns = [
......@@ -27,4 +28,8 @@ urlpatterns = [
# reply
url(r'^v1/reply/create_for_inner$', reply.CreateReplyForInner.as_view(), name='create_reply_for_inner'),
# product
url(r'^v1/product/batch_create$', product.ProductBatchCreate.as_view(), name='product_batch_create'),
url(r'^v1/brand/batch_create$', product.BrandBatchCreate.as_view(), name='brand_batch_create'),
]
import json
from api.views.base_view import BaseView
from libs.user import get_user_by_ids
from alpha_types.neptune import ERROR
from alpha_types.neptune import PRODUCT_FLATFORM
from engine.logger import info_logger, error_logger
class ProductBatchCreate(BaseView):
'''批量创建商品 爬虫入口'''
def post(self, request):
product_info = json.loads(request.POST.get('data', '[]'))
if not product_info:
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
result = []
for info in product_info:
error, ret = self.process(info)
if error:
return self.error(error)
result.append(ret)
return self.ok(data=result)
def process(self, info):
if not info:
return None, None
brand_info = info.pop('brand', {})
composition_infos = info.pop('composition', [])
category_info = info.pop('category', {})
effect_info = info.pop('effect', {})
product_id = None
if info:
error, result = self.create_product(info)
if error:
return error, None
product_id = result.get('id')
if not product_id:
return error, None # 创建商品失败 未获取到商品ID
if brand_info:
self.add_parallel_rpc_call_info('create_brand', 'neptune/commodity/brand/create',
cn_name=brand_info.get('cnName'), icon=brand_info.get('imgSrc'),
description=brand_info.get('description'), en_name=brand_info.get('en_name'),
origin_brand_id=brand_info.get('id'), platform=PRODUCT_FLATFORM.BEVOL, product_id=product_id)
if composition_infos:
self.add_parallel_rpc_call_info('batch_create_composition', 'neptune/commodity/composition/batch_create',
infos=composition_infos, platform=PRODUCT_FLATFORM.BEVOL, product_id=product_id)
if category_info:
self.add_parallel_rpc_call_info('create_category', 'neptune/commodity/category/create',
cn_name=category_info.get('name'), platform=PRODUCT_FLATFORM.BEVOL,
origin_category_id=category_info.get('id'), product_id=product_id)
if effect_info:
self.add_parallel_rpc_call_info('create_effect', 'neptune/commodity/effect/create',
cn_name=effect_info.get('name'), origin_effect_id=effect_info.get('id'),
platform=PRODUCT_FLATFORM.BEVOL, product_id=product_id)
self.shoot_rpc_calls_in_parallel()
if brand_info:
error, _ = self.parallel_rpc_call_result['create_brand']
if error:
return error, None
if composition_infos:
error, _ = self.parallel_rpc_call_result['batch_create_composition']
if error:
return error, None
if category_info:
error, _ = self.parallel_rpc_call_result['create_category']
if error:
return error, None
if effect_info:
error, _ = self.parallel_rpc_call_result['create_effect']
if error:
return error, None
return None, result
def create_product(self, product_info):
error, ret = self.call_rpc('neptune/commodity/product/create',
cn_name=product_info.get('cn_name'), en_name=product_info.get('en_name'),
image=product_info.get('image', ''), norms=product_info.get('norms'),
grade=product_info.get('grade'), price=product_info.get('price'),
country=product_info.get('country'),
comment_nums=product_info.get('comment_nums'),
origin_product_id=product_info.get('id'), platform=PRODUCT_FLATFORM.BEVOL)
return error, ret
class BrandBatchCreate(BaseView):
'''批量创建品牌'''
def post(self, request):
brand_infos = json.loads(request.POST.get('data', '[]'))
if not brand_infos:
return self.error(self.get_ErrorInfo(ERROR.PARAMS_INCOMPLETE))
infos = [{
'id': item.get('origin_brand_id'),
'cn_name': item.get('cn_name'),
'en_name': item.get('en_name'),
'icon': item.get('icon'),
'description': item.get('description')
} for item in brand_infos]
error, _ = self.call_rpc('neptune/commodity/brand/batch_create',
infos=infos, platform=PRODUCT_FLATFORM.BEVOL)
if error:
return self.error(error)
return self.ok()
......@@ -123,7 +123,8 @@ class CreateTopicForBatch(BaseView):
topic_list = json.loads(request.POST.get("topic_list", '[]'))
# pictorial_name = request.POST.get('pictorial_name', '')
# pictorial_alias = request.POST.get('pictorial_alias', '')
pictorial_tag_ids = json.loads(request.POST.get("tag_ids", '[]'))
pictorial_tag_ids = json.loads(request.POST.get("pictorial_tag_ids", '[]'))
pictorial_url = request.POST.get("pictorial_url", '')
is_app = request.POST.get('is_app', None)
......@@ -212,6 +213,7 @@ class CreateTopicForBatch(BaseView):
create_err, result = self.call_rpc(
"venus/community/topic/batch_create_for_inner",
topic_list=topic_list, pictorial_tag_ids=pictorial_tag_ids, pictorial_name=pictorial_name, pictorial_alias=pictorial_alias,
pictorial_url=pictorial_url
)
if create_err:
return self.error(create_err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment