def generate_product_item(db, _product): _product['productMaster'] = str(_product['productMaster'].id) _product['id'] = str(_product['_id']) del _product['_id'] _product = convert_datetime(_product) _product['price'] = convert_price(_product['price']) return _product
def add_product(request): new_product = request.validated['product'] db = request.db new_product['productMaster'] = DBRef('product_master', ObjectId(new_product['productMaster'])) new_product['pubDate'] = new_product['modifiedDate'] = datetime.now() db['product'].insert(new_product) new_product['productMaster'] = str(new_product['productMaster'].id) new_product['id'] = str(new_product['_id']) del new_product['_id'] new_product = convert_datetime(new_product) new_product['price'] = convert_price(new_product['price']) return {'product': new_product}
def update_product(request): product_id = ObjectId(request.matchdict['product_id']) new_product = request.validated['product'] new_product['productMaster'] = DBRef('product_master', ObjectId(new_product['productMaster'])) db = request.db new_product['modifiedDate'] = datetime.now() result = db['product'].find_and_modify(query={"_id": product_id}, update={'$set': new_product}, new=True) result['productMaster'] = str(result['productMaster'].id) result['id'] = str(result['_id']) del result['_id'] result = convert_datetime(result) result['price'] = convert_price(result['price']) return {'product': result}
def add_product_master(request): new_product_master = request.validated["productMaster"] for i in range(len(new_product_master["tags"])): new_product_master["tags"][i] = DBRef("product_tag", ObjectId(new_product_master["tags"][i])) db = request.db new_product_master["pubDate"] = new_product_master["modifiedDate"] = datetime.now() db["product_master"].insert(new_product_master) for i in range(len(new_product_master["tags"])): new_product_master["tags"][i] = str(new_product_master["tags"][i].id) new_product_master["id"] = str(new_product_master["_id"]) del new_product_master["_id"] new_product_master = convert_datetime(new_product_master) return {"productMaster": new_product_master}
def generate_master_item(db, _master): # 标签ID列表 _master["tags"] = map(get_id_from_ref, _master["tags"]) # 产品ID列表 product_id_list = [] for _product in db[PRODUCT_COLLECTION].find({"productMaster.$id": _master["_id"]}): product_id_list.append(str(_product["_id"])) _master["products"] = product_id_list # 产品图片列表 image_id_list = [] for _image in db[PRODUCT_IMAGE_COLLECTION].find({"productMaster.$id": _master["_id"]}): image_id_list.append(str(_image["_id"])) _master["images"] = image_id_list # ID变换 _master["id"] = str(_master["_id"]) del _master["_id"] # 变更时间 _master = convert_datetime(_master) return _master
def update_product_master(request): master_id = ObjectId(request.matchdict["master_id"]) new_product_master = request.validated["productMaster"] for i in range(len(new_product_master["tags"])): new_product_master["tags"][i] = DBRef("product_tag", ObjectId(new_product_master["tags"][i])) db = request.db new_product_master["modifiedDate"] = datetime.now() result = db["product_master"].find_and_modify( query={"_id": master_id}, update={"$set": new_product_master}, new=True ) for i in range(len(result["tags"])): result["tags"][i] = str(result["tags"][i].id) result["products"] = [] result["images"] = [] for _product in db["product"].find({"productMaster.$id": result["_id"]}): result["products"].append(str(_product["_id"])) for _image in db["product_image"].find({"productMaster.$id": result["_id"]}): result["images"].append(str(_image["_id"])) result["id"] = str(result["_id"]) del result["_id"] result = convert_datetime(result) return {"productMaster": result}