def new_entity(request): if request.method == 'GET': return render_to_response('entity/new.html', {'active_division': 'entity'}, context_instance=RequestContext(request)) else: _cand_url = request.POST.get("url", None) _hostname = urlparse(_cand_url).hostname if re.search(r"\b(jd|360buy)\.com$", _hostname) != None: return new_jd_item(request) if re.search(r"\b(tmall|taobao)\.com$", _hostname) != None: _taobao_id = parse_taobao_id_from_url(_cand_url) _item = Item.get_item_by_taobao_id(_taobao_id) if _item == None: _taobao_item_info = load_taobao_item_info(_taobao_id) _brand = '' _title = '' _selected_category_id = Category.get_category_by_taobao_cid( _taobao_item_info['cid']) _users = _get_special_names(request.user.id) return render_to_response( 'entity/create.html', { 'active_division': 'entity', 'taobao_id': _taobao_id, 'cid': _taobao_item_info['cid'], 'taobao_title': _taobao_item_info['title'], 'shop_nick': _taobao_item_info['nick'], 'shop_link': _taobao_item_info['shop_link'], 'price': _taobao_item_info['price'], 'thumb_images': _taobao_item_info["thumb_images"], 'selected_category_id': _selected_category_id, 'category_list': Category.find(), 'brand': _brand, 'title': _title, 'users': _users }, context_instance=RequestContext(request)) elif _item.get_entity_id() == -1: #TODO: bind an exist item to entity pass else: return HttpResponseRedirect( reverse('management_edit_entity', kwargs={"entity_id": _item.get_entity_id()}) + '?code=1')
def get_guoku_plus_item_context(taobao_url, shop_nick, user_id): if not is_taobao_url(taobao_url): raise NotTaobaoUrl() taobao_id = parse_taobao_id_from_url(taobao_url) if not taobao_id: raise InvalidUrl() item_inst = Item.get_item_by_taobao_id(taobao_id) if not item_inst: taobao_item_info = TaobaoExtractor.fetch_item(taobao_id) print taobao_item_info _category_id = Category.get_category_by_taobao_cid( taobao_item_info['cid']) _chief_image = None _detail_image_urls = [] imgs_len = len(taobao_item_info['imgs']) if imgs_len > 0: _chief_image = taobao_item_info['imgs'][0] for i in range(1, imgs_len): _detail_image_urls.append(taobao_item_info['imgs'][i]) _title = HTMLParser.HTMLParser().unescape(taobao_item_info['desc']) Entity.create_by_taobao_item(creator_id=user_id, category_id=_category_id, chief_image_url=_chief_image, taobao_item_info={ 'taobao_id': taobao_id, 'cid': taobao_item_info['cid'], 'title': _title, 'shop_nick': unicode(taobao_item_info['nick'], 'utf-8'), 'price': taobao_item_info['price'], 'soldout': False, }, brand="", title=_title, detail_image_urls=_detail_image_urls) item_inst = Item.get_item_by_taobao_id(taobao_id) item_context = item_inst.read() if item_context['shop_nick'] != shop_nick: raise NotSellerOwnProduct() return item_context
def load_item_info(request): if request.method == 'POST': _cand_url = request.POST.get("cand_url", None) _hostname = urlparse(_cand_url).hostname if re.search(r"\b(jd|360buy)\.com$", _hostname) != None: return jd_info(request, _cand_url) if re.search(r"\b(tmall|taobao)\.com$", _hostname) is not None: _taobao_id = parse_taobao_id_from_url(_cand_url) _item = Item.get_item_by_taobao_id(_taobao_id) if _item is None: _taobao_item_info = load_taobao_item_info(_taobao_id) _chief_image_url = _taobao_item_info["thumb_images"][0] _selected_category_id = Category.get_category_by_taobao_cid(_taobao_item_info['cid']) _data = { 'user_context' : User(request.user.id).read(), 'cand_url' : _cand_url, 'taobao_id': _taobao_id, 'cid': _taobao_item_info['cid'], 'taobao_title': _taobao_item_info['title'], 'shop_nick': _taobao_item_info['shop_nick'], 'shop_link': _taobao_item_info['shop_link'], 'price': _taobao_item_info['price'], 'chief_image_url' : _chief_image_url, 'thumb_images': _taobao_item_info["thumb_images"], 'selected_category_id': _selected_category_id, } _rslt = { 'status' : 'SUCCESS', 'data' : _data } elif _item.get_entity_id() == -1: _rslt = { 'status' : 'OTHER' } else: _entity_id = _item.get_entity_id() _entity_context = Entity(_entity_id).read() _rslt = { 'status' : 'EXIST', 'data' : { 'entity_hash' : _entity_context['entity_hash'] } } return HttpResponse(json.dumps(_rslt))
def create_entity_from_offline(request): if request.method == 'POST': _taobao_id = request.POST.get("taobao_id", None) _cid = request.POST.get("cid", None) _taobao_shop_nick = request.POST.get("taobao_shop_nick", None) _taobao_title = request.POST.get("taobao_title", None) _taobao_price = request.POST.get("taobao_price", None) _taobao_soldout = request.POST.get("taobao_soldout", '0') if _taobao_soldout == '0': _taobao_soldout = False else: _taobao_soldout = True _rank_score = int(request.POST.get("item_score", '0')) _chief_image_url = request.POST.get("chief_image_url", None) _chief_image_url = re.sub('_\d+x\d+\.jpg|_b.jpg', '', _chief_image_url) _brand = request.POST.get("brand", "") _title = request.POST.get("title", "") _intro = request.POST.get("intro", "") _category_id = Category.get_category_by_taobao_cid(_cid) _origin_detail_image_urls = request.POST.getlist("image_url") _detail_image_urls = [] for _url in _origin_detail_image_urls: _detail_image_urls.append(re.sub('_\d+x\d+\.jpg|_b.jpg', '', _url)) if _chief_image_url in _detail_image_urls: _detail_image_urls.remove(_chief_image_url) _item = Item.get_item_by_taobao_id(_taobao_id) if _item == None: _entity = Entity.create_by_taobao_item( creator_id=request.user.id, category_id=_category_id, chief_image_url=_chief_image_url, taobao_item_info={ 'taobao_id': _taobao_id, 'cid': _cid, 'title': _taobao_title, 'shop_nick': _taobao_shop_nick, 'price': _taobao_price, 'soldout': _taobao_soldout, }, brand=_brand, title=_title, intro=_intro, detail_image_urls=_detail_image_urls, weight=-1, rank_score=_rank_score) _rslt = { 'entity_id': _entity.entity_id, 'item_id': _entity.read()['item_id_list'][0], 'status': 'success' } return SuccessJsonResponse(_rslt) else: _item.update(cid=_cid, title=_taobao_title, shop_nick=_taobao_shop_nick, price=_taobao_price, soldout=_taobao_soldout) _rslt = { 'message': 'item_exist', 'item_id': _item.item_id, 'status': 'updated' } return SuccessJsonResponse(_rslt)